Friday, 7 December 2012

Linked List using cpp

Linked list implementation using  C++

Program that performs various operations on a linked list

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class linkedlist
{ private:
            struct node
            {      int data;
                    node *link;
           }*start;
  public:
          linkedlist();
           ~linkedlist();
   void create(void);
            void display(void);
            void insertbeg(int);
            void insertafter(int,int);
          void insertend(int);
           void del(int);
            int count(void);
          void sort(int);
            int search(int);
           void reverse(void);
};

linkedlist::linkedlist()
{        start=NULL; }
linkedlist::~linkedlist()
{        node *current;
                   while(start!=NULL)
                    {       current=start->link;
                              delete current;

                              start=current;
                   }
}

void linkedlist::create(void)
{        int val,n;
          char ch;
           node *current=start, *t;
          do
          {        cout<<"Enter a value:";
                   cin>>val;
                   for(current=start;current->link!=NULL;current=current->link);
                   current->link=new node;
                   current=current->link;
                    current->data=val;
                   current->link=NULL;
                   if(start==NULL)start=current;
                   cout<<"Any More Nodes(Y/N):";
                   cin>>ch;
          } while(ch=='y'||ch=='Y');
}

void linkedlist::display()
 {       node *current;
          for(current=start;current;current=current->link)
                   cout<<current->data<<"->";
                   cout<<"NULL";
}

void linkedlist::insertbeg(int num)
{        node *current;
            current=new node;
           current->data=num;
           current->link=start;
           start=current;
}

void linkedlist::insertafter(int loc,int num)
{        node *current, *r;
          current=start;
          for(int i=1;i<loc;i++)
          {        if(current==NULL)
                   {        cout<<"There are less than"<<loc<<"elements in list";
                              return;
                   }
                    current=current->link;
          }
          r=new node;
          r->data=num;
          r->link=current->link;
          current->link=r;
}

void linkedlist::insertend(int num)
{        node *current, *r;
          for(current=start;current->link!=NULL;current=current->link);
           r=new node;
           r->data=num;
          r->link=NULL;
          current->link=r;
}

void linkedlist::del(int num)
{        node *old, *current;
          for(current=start;current!=NULL;current=current->link)
                    if(current->data==num)
                   {        if(current==start)
                                      start=current->link;
                              else
                                      old->link=current->link;
                                      delete current;
                                      return;
                   }
                   else
                             old=current;
          cout<<num<<"not found."<<endl;
  }

int linkedlist::count(void)
{        node * current;
           int c=0;
          for(current=start;current!=NULL;current=current->link)
           c++;
          return c;
}

void linkedlist::sort(int n)
{        int i,j,k,current;
          node *q, *r;
          for(i=0;i<n-1;i++)
          {        q=start;
                   r=q->link;
                   for(int j=0;j<(n-i-1);j++)
                   {        if(q->data>r->data)
                             {        current=q->data;
                                      q->data=r->data;
                                      r->data=current;
                             }
                             q=q->link;
                             r=r->link;
                   }
          }
}

int linkedlist::search(int num)
{        node *current;
            int c=0;
                   for(current=start;current!=NULL;current=current->link)
                   {        c++;
                              if(current->data==num)return c;
                   }
           return-1;
}

void linkedlist::reverse(void)
{        node *q, *r, *s;
          q=start;
           r=NULL;
          while(q!=NULL)
          {        s=r;
                    r=q;
                   q=q->link;
                   r->link=s;
          }
          start=r;
}

void main()
{        int c,num,pos;
          char repeat;
          linkedlist l;
           while(1)
          {        clrscr();
                   cout<<"Perform the Following Operationson a Linked List\n";
                   cout<<"______________       ___________________________"<<endl;
                   cout<<"1.Create a Linked List\n";
                   cout<<"2.Display Linked List\n";
                   cout<<"3.Insert at Beginning\n";
                   cout<<"4.Insert After\n";
                   cout<<"5.Insert at End\n";
                   cout<<"6.Delete a node\n";
                   cout<<"7.Count nodes\n";
                   cout<<"8.Sort nodes\n";
                   cout<<"9.Search for a node\n";
                   cout<<"10.Reverse nodes\n";
                   cout<<"11.Exit from Program\n";
                   do
                   {        cout<<"Select Your Choice(1;11)";
                             cin>>c;
                             cout<<" ";
                   }
                    while((c<1)||(c>11));
                   if(c==11)exit(1);
                    clrscr();
          switch(c)
          {case 1:
                    l.create();
                     break;
          case 2:
                     cout<<" Elements in the linkedlist:";
                     l.display();
                    break;
case 3:
                    do
                    {        cout<<" Current Elements in thelinked list:";
                             l.display();
                             cout<<"\nElement to be insertedinto the linked list:";
                             cin>>num;
                             l.insertbeg(num);
                             cout<<"Insert MoreNodes(Y/N):";
                             cin>>repeat;
                    }while(repeat=='y'||repeat=='Y');
                    break;
          case 4:
                    do
                    {        cout<<"Current Elements in thelinked list:";
                             l.display();
                             cout<<"\nElement to be insertedinto the linked list:";
                             cin>>num;
                             cout<<"The node number after which the node is inserted:";
                             cin>>pos;
                              if(pos<1)
                                        cout<<"Invalid position.";
                              else
                                       l.insertafter(pos,num);
                             cout<<"Insert MoreNodes(Y/N):";
                             cin>>repeat;
                    }while(repeat=='y'||repeat=='Y');
                    break;
          case 5:
                    do
                    {        cout<<"Current Elements in thelinked list:";
                             l.display();
                              cout<<"\nElement to be inserted into the linked list:";
                             cin>>num;
                             l.insertend(num);
                             cout<<"Insert More Nodes(Y/N):";
                             cin>>repeat;
                    }while(repeat=='y'||repeat=='Y');
                   break;
          case 6:
                   do
                   {        cout<<"Current Elements in thelinked list:";
                             l.display();
                             cout<<"\nElement to be deleted:";
                             cin>>num;
                             l.del(num);
                             cout<<"Delete More Nodes(Y/N):";
                             cin>>repeat;
                   }while(repeat=='y'||repeat=='Y');
                   break;
          case 7:
cout<<"There are" <<l.count()<<"elements in thelist.They are"<<endl;
                   l.display();
                   break;
          case 8:
                   cout<<"Current Elements in the linkedlist:";
                   l.display();
                   l.sort(l.count());
                   cout<<"\nElements after sorting:";
                   l.display();
                   break;
          case 9:
                   cout<<"\nElement to search?";
                   cin>>num;
                   int loc=l.search(num);
                   if(loc>0)
                             cout<<num<<"exists in the list at location"<<loc;
                   else
                              cout<<num<<"does not exist in the list";
                   break;
          case 10:
                   cout<<"Current list is:";
                   l.display();
                   l.reverse();
                   cout<<"\nElements after reversing the list;";
                   l.display();
                   break;
    }
    cout<<endl<<"Press Any Key to Continue...";
    getch();
   }
}

No comments:

Post a Comment