My photo
Bangalore, Karnataka, India
Extending one hand to help someone has more value rather than joining two hands for prayer

Archives

program to remove a duplicate node from link list

Tuesday, September 1, 2009

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
struct node
{ int info;
  node *link;
};
class linklist
{ node *start;
  public:
  linklist()
  { start=NULL; }
  void insert(int);
  void duplicate();
  void show();
 };

 void linklist::insert(int a)
 {  node *p=new node;
    p->info=a;
    if(start==NULL)
    { start=p;
      p->link=NULL;
    }
     else
     {  node *q=start;
    while(q->link!=NULL)
    { q=q->link;
    }
    q->link=p;
    p->link=NULL;
     }

  }

  void linklist::duplicate()
  {
     node *p1,*p2,*q1;
     p1=start;
     p2=p1->link;
     while(p2!=NULL)
     {  q1=start;
    while(q1!=p2)
    {  if(q1->info==p2->info)
       {   p1->link=p2->link;
           p2->link=NULL;
           delete p2;
           p2=p1->link;
           break;
        }
        q1=q1->link;
    }
       p2=p2->link;
       p1=p1->link;
     }
  }









   void linklist::show()
   {
      node *p=start;
      while(p!=NULL)
      {   cout<<p->info<<endl;
      p=p->link;
      }
    }
  void main()
  {
     clrscr();
     linklist l1;
     l1.insert(10);
     l1.insert(20);
     l1.insert(10);
     l1.insert(40);
     l1.insert(50);
     l1.insert(20);
     l1.show();
     getch();
     l1.duplicate();
     l1.show();
     getch();

   }

0 comments: