Write a C Program to delete duplicates from a sorted linked list

By | 05.04.2017

C Program to delete duplicates from a sorted linked list


Write a C Program to delete duplicates from a sorted linked list. Here’s simple Program to delete duplicates from a sorted linked list in C Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C Program to delete duplicates from a sorted linked list which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* C Program to delete duplicates from a sorted linked list  */

#include<stdio.h>
#include<stdlib.h>

struct node
{
        int info;
        struct node *link;
};
struct node *insert_s(struct node *start,int data);
void display(struct node *start);
void deleteDuplicates(struct node *start);

int main()
{
        int i,n,data;
        struct node *start=NULL;

        printf("Enter the number of elements : ");
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
                printf("\nEnter the element to be inserted : ");
                scanf("%d",&data);
                start=insert_s(start,data);
        }
        display(start);
        deleteDuplicates(start);
        display(start);

        return 0;

}/*End of main()*/

void deleteDuplicates(struct node *start)
{
        struct node *p,*duplicate;

        if(start==NULL)
                return;
        p=start;
        while(p->link!=NULL)
        {
                if(p->info == p->link->info)
                {
                        duplicate = p->link;
                        p->link = p->link->link;
                        free(duplicate);
                }
                else
                        p=p->link;
        }
}/*End of deleteDuplicates()*/

struct node *insert_s(struct node *start,int data)
{
        struct node *p,*tmp;
        tmp=(struct node *)malloc(sizeof(struct node));
        tmp->info=data;
        /*list empty or new node to be added before first node*/
        if(start==NULL || data < start->info)
        {
                tmp->link=start;
                start=tmp;
                return start;
        }
        else
        {
                p=start;
                while(p->link!=NULL && p->link->info < data)
                        p=p->link;
                tmp->link=p->link;
                p->link=tmp;
        }
        return start;
}/*End of insert()*/

void display(struct node *start)
{
        struct node *q;
        if(start==NULL)
        {
                printf("\nList is empty\n");
                return;
        }
        q=start;
        printf("\nList is :\n");
        while(q!=NULL)
        {
                printf("%d ",q->info);
                q=q->link;
        }
        printf("\n");
}/*End of display() */

OUTPUT : :


/* C Program to delete duplicates from a sorted linked list  */

************** OUTPUT *****************

Enter the number of elements : 8

Enter the element to be inserted : 3

Enter the element to be inserted : 1

Enter the element to be inserted : 6

Enter the element to be inserted : 8

Enter the element to be inserted : 6

Enter the element to be inserted : 3

Enter the element to be inserted : 2

Enter the element to be inserted : 2

List is :
1 2 2 3 3 6 6 8

List is :
1 2 3 6 8

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

3 2 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments