Monday, August 19, 2013

linked list C program to remove duplicates / repetition of numbers


/* linked list C program to remove duplicates / repetition of numbers */

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

struct node
{
int n;           
struct node *next;
};

struct node *start,*last,*temp,*temp1,*temp2;

void create()
{
int c=1;
start=NULL;
while(c==1)
{
if(start==NULL)
{
start=(struct node*)malloc(sizeof(struct node));
temp=start;
printf("Enter element ::");
scanf("%d",&temp->n);
start->next=NULL;
}
else
{
last=(struct node*)malloc(sizeof(struct node));
temp->next=last;
temp=last;
printf("Enter element ::");
scanf("%d",&temp->n);
last->next=NULL;
}
printf("to continue enter 1 :");
scanf("%d",&c);
}
}

void manipulate()
{
temp=start;
temp1=start;
if(start->next==NULL)
{
printf("No repitition of Numbers.");
}
else
{
while(temp!=NULL)
{
temp1=temp->next;
temp2=temp;
while(temp1!=NULL)
{
if((temp->n)==(temp1->n))
{
temp2->next=temp1->next;
free(temp1);
temp1=temp2->next;
}
else
{
temp2=temp1;
temp1=temp1->next;
}
}
temp=temp->next;
}
}
}

void display()
{
temp=start;
printf("The list is :: ");
while(temp!=NULL)
{
printf("\n%d\n",temp->n);
temp=temp->next;
}
}

void main()
{
create();
display();
manipulate();
display();
}

No comments: