C Program to Remove Duplicates from a Linked List

This C Program to remove duplicate elements from a linked list.

Here is source code of the C Program to remove duplicate elements from a linked list. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Remove Duplicates from a Linked List 
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. struct node
  8. {
  9.     int num;
  10.     struct node *next;
  11. };
  12.  
  13. void create(struct node **);
  14. void dup_delete(struct node **);
  15. void release(struct node **);
  16. void display(struct node *);
  17.  
  18. int main()
  19. {
  20.     struct node *p = NULL;
  21.     struct node_occur *head = NULL;
  22.     int n;
  23.  
  24.     printf("Enter data into the list\n");
  25.     create(&p);
  26.     printf("Displaying the nodes in the list:\n");
  27.     display(p);
  28.     printf("Deleting duplicate elements in the list...\n");
  29.     dup_delete(&p);
  30.     printf("Displaying non-deleted nodes in the list:\n");
  31.     display(p);
  32.     release(&p);
  33.  
  34.     return 0;
  35. }
  36.  
  37. void dup_delete(struct node **head)
  38. {
  39.     struct node *p, *q, *prev, *temp;
  40.  
  41.     p = q = prev = *head;
  42.     q = q->next;
  43.     while (p != NULL)
  44.     {
  45.         while (q != NULL && q->num != p->num)
  46.         {
  47.             prev = q;
  48.             q = q->next;
  49.         }
  50.         if (q == NULL)
  51.         {
  52.             p = p->next;
  53.             if (p != NULL)
  54.             {
  55.                 q = p->next;
  56.             }
  57.         }
  58.         else if (q->num == p->num)
  59.         {
  60.             prev->next = q->next;
  61.             temp = q;
  62.             q = q->next;
  63.             free(temp);
  64.         }
  65.     }
  66. }
  67.  
  68. void create(struct node **head)
  69. {
  70.     int c, ch;
  71.     struct node *temp, *rear;
  72.  
  73.     do
  74.     {
  75.         printf("Enter number: ");
  76.         scanf("%d", &c);
  77.         temp = (struct node *)malloc(sizeof(struct node));
  78.         temp->num = c;
  79.         temp->next = NULL;
  80.         if (*head == NULL)
  81.         {
  82.             *head = temp;
  83.         }
  84.         else
  85.         {
  86.             rear->next = temp;
  87.         }
  88.         rear = temp;
  89.         printf("Do you wish to continue [1/0]: ");
  90.         scanf("%d", &ch);
  91.     } while (ch != 0);
  92.     printf("\n");
  93. }
  94.  
  95. void display(struct node *p)
  96. {
  97.     while (p != NULL)
  98.     {
  99.         printf("%d\t", p->num);
  100.         p = p->next;
  101.     }
  102.     printf("\n");
  103. }
  104.  
  105. void release(struct node **head)
  106. {
  107.     struct node *temp = *head;
  108.     *head = (*head)->next;
  109.     while ((*head) != NULL)
  110.     {
  111.         free(temp);
  112.         temp = *head;
  113.         (*head) = (*head)->next;
  114.     }
  115. }

$ cc duplicate.c 
$ ./a.out
Enter data into the list
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 1
Do you wish to continue [1/0]: 1
Enter number: 3
Do you wish to continue [1/0]: 1
Enter number: 2
Do you wish to continue [1/0]: 1
Enter number: 4
Do you wish to continue [1/0]: 0
 
Displaying the nodes in the list:
1	2	1	3	2	4	
Deleting duplicate elements in the list...
Displaying non-deleted nodes in the list:
1	2	3	4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.