Advertisement
Idanref

Untitled

Apr 7th, 2021
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Node
  6. {
  7. char data;
  8. struct Node* next;
  9. } node;
  10.  
  11. node* create_node(char data)
  12. {
  13. node* temp = (node*)malloc(sizeof(node));
  14.  
  15. temp->data = data;
  16. temp->next = NULL;
  17.  
  18. return temp;
  19. }
  20.  
  21. void insert(node** head, char data)
  22. {
  23. node* temp = *head;
  24. node* link = create_node(data);
  25.  
  26. if (*head == NULL)
  27. {
  28. *head = link;
  29. return;
  30. }
  31.  
  32. while (temp != NULL)
  33. {
  34. if (temp->next == NULL)
  35. {
  36. temp->next = link;
  37. return;
  38. }
  39.  
  40. else
  41. temp = temp->next;
  42. }
  43. }
  44.  
  45. void print_list(node* head)
  46. {
  47. if (head == NULL)
  48. {
  49. printf("List is empty\n");
  50. }
  51.  
  52. node* temp = head;
  53.  
  54. while (temp != NULL)
  55. {
  56. printf("%c->", temp->data);
  57. temp = temp->next;
  58. }
  59. }
  60.  
  61. void remove_duplications(node** head)
  62. {
  63. // a>b>d>b>c>c>N
  64.  
  65. node* i = *head;
  66. node* j, * temp;
  67.  
  68. while (i->next->next != NULL)
  69. {
  70. j = i;
  71.  
  72. while (j->next != NULL)
  73. {
  74. if (j->next->data == i->data)
  75. {
  76. temp = j->next;
  77. j->next = j->next->next;
  78. free(temp);
  79. }
  80.  
  81. j = j->next;
  82. }
  83.  
  84. i = i->next;
  85. }
  86.  
  87. if (i->data == i->next->data)
  88. {
  89. temp = i->next;
  90. i->next = NULL;
  91. free(temp);
  92. }
  93. }
  94.  
  95. void main()
  96. {
  97. node* head = NULL;
  98.  
  99. print_list(head);
  100.  
  101. /*insert(&head, 'a');
  102. insert(&head, 'b');
  103. insert(&head, 'd');
  104. insert(&head, 'b');
  105. insert(&head, 'c');
  106. insert(&head, 'c');*/
  107.  
  108. insert(&head, 'a');
  109. insert(&head, 'a');
  110. insert(&head, 'b');
  111. insert(&head, 'b');
  112. insert(&head, 'c');
  113. insert(&head, 'a');
  114.  
  115. remove_duplications(&head);
  116.  
  117. print_list(head);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement