Remove Duplicate Entry from Linked List in C++ (Complete Solution with Code)
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
}*first=NULL,*curr,*new1,*prev;
void insert()
{
int val;
cout<<"Enter Any Value :";
cin>>val;
new1=new node;
new1->data=val;
new1->next=NULL;
if(first==NULL)
{
first=new1;
return;
}
curr=first;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new1;
}
void display()
{
if(first==NULL)
{
cout<<"Linked list is empty";
return;
}
curr=first;
while(curr!=NULL)
{
cout<<curr->data<<"-->";
curr=curr->next;
}
}
void duplicate()
{
node *i,*j;
int temp,dup=0;
if(first==NULL)
{
cout<<"Linked list is empty";
return;
}
i=first;
while(i->next!=NULL)
{
j=i->next;
while(j!=NULL)
{
if(i->data==j->data)
{
dup++;
temp=i->data;
}
j=j->next;
}
i=i->next;
}
if(dup>0)
{
cout<<"Duplicate Number is :"<<temp<<endl;
curr=first;
if(first->data==temp)
{
first=first->next;
delete curr;
return;
}
while(curr->data!=temp && curr!=NULL)
{
prev=curr;
curr=curr->next;
}
prev->next=curr->next;
delete curr;
cout<<"Number Deleted Successfully";
}
else
cout<<"No duplicate Number Found";
}
void main()
{
int ch;
do
{
clrscr();
cout<<"Options for Program\n";
cout<<"====================\n";
cout<<"0. Exit"<<endl;
cout<<"1. Insert in List"<<endl;
cout<<"2. Display List "<<endl;
cout<<"3. Delete Duplicate"<<endl;
cout<<"Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 0: exit(0);
case 1: insert(); break;
case 2: display(); break;
case 3: duplicate(); break;
default:
cout<<"Wrong Input";
}
getch();
}while(ch>0);
}
#include<conio.h>
#include<process.h>
struct node
{
int data;
node *next;
}*first=NULL,*curr,*new1,*prev;
void insert()
{
int val;
cout<<"Enter Any Value :";
cin>>val;
new1=new node;
new1->data=val;
new1->next=NULL;
if(first==NULL)
{
first=new1;
return;
}
curr=first;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new1;
}
void display()
{
if(first==NULL)
{
cout<<"Linked list is empty";
return;
}
curr=first;
while(curr!=NULL)
{
cout<<curr->data<<"-->";
curr=curr->next;
}
}
void duplicate()
{
node *i,*j;
int temp,dup=0;
if(first==NULL)
{
cout<<"Linked list is empty";
return;
}
i=first;
while(i->next!=NULL)
{
j=i->next;
while(j!=NULL)
{
if(i->data==j->data)
{
dup++;
temp=i->data;
}
j=j->next;
}
i=i->next;
}
if(dup>0)
{
cout<<"Duplicate Number is :"<<temp<<endl;
curr=first;
if(first->data==temp)
{
first=first->next;
delete curr;
return;
}
while(curr->data!=temp && curr!=NULL)
{
prev=curr;
curr=curr->next;
}
prev->next=curr->next;
delete curr;
cout<<"Number Deleted Successfully";
}
else
cout<<"No duplicate Number Found";
}
void main()
{
int ch;
do
{
clrscr();
cout<<"Options for Program\n";
cout<<"====================\n";
cout<<"0. Exit"<<endl;
cout<<"1. Insert in List"<<endl;
cout<<"2. Display List "<<endl;
cout<<"3. Delete Duplicate"<<endl;
cout<<"Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 0: exit(0);
case 1: insert(); break;
case 2: display(); break;
case 3: duplicate(); break;
default:
cout<<"Wrong Input";
}
getch();
}while(ch>0);
}
No comments
Post your comments