Saturday, January 12, 2013

REMOVE DUPLICATES FROM THE LINK LIST

Q.) REMOVE THE DUPLICATES FROM THE LINK LIST WHICH IS IN ASCENDING ORDER.

SOLUTION--


#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* next;
}*head;
int  main()
{
    struct node *temp;
    int num,i,item;
    scanf("%d",&num);
    //creation of link list//
    for(i=0;i<num;i++)
    {
        scanf("%d",&item);
        if(i==0)
        {
            head=malloc(sizeof(struct node));
            head->data=item;
            temp=head;
        }
        else
        {
            temp->next=malloc(sizeof(struct node));
            temp->next->data=item;
            temp=temp->next;
        }
    }
    temp->next=NULL;
    if(head==NULL)
        printf("no link list exist\n");
    else
    {
        temp=head;
        while(temp->next!=NULL)
        {
            if(temp->data==temp->next->data)
            {

                struct node *t;
                t=temp->next;
                temp->next=t->next;
                free(t);
            }
            else
                temp=temp->next;
        }
        temp=head;
        while(temp!=NULL)
        {
            printf("%d ",temp->data);
            temp=temp->next;
        }
    }
    return 0;
}

No comments:

Post a Comment