Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node {
  6. int data;
  7. struct Node * prev;
  8. struct Node * next;
  9. }Node;
  10. struct Node* head;//global variable
  11. struct Node* newNode(int value){
  12.  
  13. struct Node* element = (struct Node*) malloc(sizeof(struct Node));
  14. element -> data = value;
  15. element -> prev = NULL;
  16. element -> next = NULL;
  17. return element;
  18. };
  19. void print_dll(Node* head){
  20. Node* temp = head;
  21. while(temp != NULL){
  22. printf("%d\n", temp->data);
  23. temp = temp->next;
  24. }
  25. printf("\n");
  26. }
  27.  
  28. void insert_after(Node* head, int valueToInsertAfter, int valueToBeInserted){
  29. Node* temp = head;
  30. Node* prevTemp;
  31. Node* insertedNode = newNode(valueToBeInserted);
  32. head = (head == NULL)?insertedNode:head;
  33. while(temp!= NULL && temp -> data != valueToInsertAfter){
  34. temp = temp -> next;
  35. }
  36.  
  37. if(temp == NULL){
  38. temp= insertedNode;
  39. }else{
  40. insertedNode -> next = temp -> next;
  41. insertedNode -> prev = temp;
  42. temp -> next -> prev = insertedNode;
  43. temp -> next = insertedNode;
  44. }
  45.  
  46.  
  47. }
  48. void delete_element(Node* head, int valueTobeDeleted){
  49. Node* temp = head;
  50. while(temp!= NULL && temp-> data != valueTobeDeleted){
  51. temp = temp -> next;
  52. }
  53. if(temp != NULL){
  54. temp -> prev -> next = temp -> next;
  55. temp -> next -> prev = temp -> prev;
  56. free(temp);
  57. }
  58. printf("delete");
  59. }
  60. void sort_dll(Node* head){
  61. if(head == NULL && head->next == NULL){
  62. return ; //need two elements to sort
  63. }
  64. Node* first = head;
  65. Node* second = head -> next;
  66. int swapped; //count for the elements in dll
  67. //If swapping occurred, swapped will be incremented
  68. do{
  69. swapped = 0; //Re-zero swapped at the beginning of do-while loop
  70. while(first != NULL && second != NULL){
  71.  
  72. if(first -> data > second -> data){
  73. printf("sort");
  74.  
  75. second -> prev = first -> prev;
  76. first -> prev = second;
  77. first -> next = second -> next;
  78. second -> next -> prev = first;
  79. second -> next = first;
  80. first = second;
  81. second = second-> next;
  82. swapped += 1;
  83. }
  84. first = first-> next;
  85. second = second -> next;
  86.  
  87. }
  88. //If one of them was NULL, point them back to head and head.next.
  89. }while(swapped != 0);//If no swapping occurred, terminate the loop.
  90.  
  91.  
  92. }
  93. void freeDll(Node* head){
  94. Node* temp = head;
  95. Node* delete = head;
  96. while(temp != NULL){
  97. free(temp);
  98. temp = delete -> next;
  99. }
  100. printf("free");
  101.  
  102. }
  103. Node* create_dll_from_array(int array[], int size){
  104. Node* dummy = (struct Node*) malloc(sizeof(struct Node));
  105. Node* temp = dummy;
  106. for(int i= 0; i< size; i++){
  107. temp -> next = newNode(array[i]);
  108. temp -> prev = temp;
  109. temp = temp-> next;
  110. }
  111. return dummy->next;
  112.  
  113. }
  114. int main(){
  115. int array[5] = {11,2,7,22,4};
  116. Node* head;
  117. head = create_dll_from_array(array,5);
  118. // print_dll(head);
  119. //
  120. // insert_after(head,7,13);
  121. // insert_after(head,21,29);
  122. //
  123. // print_dll(head);
  124. //
  125. // delete_element(head,22);
  126. // print_dll(head);
  127.  
  128. sort_dll(head);
  129. printf("\n sort test\n");
  130. print_dll(head);
  131. printf("\n print test\n");
  132.  
  133. //freeDll(head);
  134. printf("xx");
  135.  
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement