Earn without any Investment!

Monday, October 25, 2010

Single Linked List in C++

#include<iostream.h>
#include<conio.h>
      class  list
         {
         struct node
         {
         int info;
         node *next;
         }*head;
          public:
         list()
           {
           head=NULL;
           }
          void append(int data)
            {
            node *temp,*temp1;
            if(head==NULL)
               {
               temp=new node;
               temp->info=data;
               temp->next=NULL;
               head=temp;
               }
           else
               {
            temp=head;
            while(temp->next!=NULL)
               temp=temp->next;
               temp1=new node;
               temp1->info=data;
               temp1->next=NULL;
               temp->next=temp1;
             }
              }
          void beg_insert(int data)
               {
                node *temp;
                 temp=new node;
                 temp->info=data;
                 temp->next=head;
                 head=temp;

                }
            void in_insert(int pos,int data)
                  {
                  node *temp,*t;
                  int a;
                  a=pos;
                  temp=head;
                  while(pos>1)
                {
                temp=temp->next;
                pos--;
                }
               if(a==1 && temp==NULL)
                append(data);

              else  if(temp==NULL)
                   cout<<"\n\tWrong position u given";

                else
                   {
                   t=new node;
                   t->info=data;
                   t->next=temp->next;
                   temp->next=t;
                   }
                }
            void del( )
            {
            node *temp,*t;
            temp=head;
            if(temp==NULL)
              cout<<"\n \tList not Exists ";
            else if(temp->next==NULL)
                {
                delete temp;
                head=NULL;
                }
            else
              {
              t=head->next;
              while(t->next!=NULL)
                 {
                 t=t->next;
                 temp=temp->next;
                 }
               // t=temp->next->next;
                temp->next=NULL;
                delete t;
                }
             }


              void beg_del()
             {
              node *temp;
              temp=head;
              if(head==NULL)
                cout<<"\n\t Link list has np nodes";
              else
               {
              head=temp->next;
              delete temp;
               }
              }
              void mid_del(int pos)
              {
              node *temp,*t;
                int a=pos;
                temp=head;
               while(pos>1)
                  {
                  temp=temp->next;
                   pos--;
                   }
             if(a==1 && head->next==NULL)
                   del();

             else  if(temp==NULL)
                 cout<<"\n\t List not Exist ..";
              else
               {
               t=temp->next;
               temp->next=temp->next->next;
               delete t;
               }
              }

            void show()
              {
              node *temp;
              temp=head;
              cout<<"\n\t";
              if(temp==NULL)
             cout<<"\n \tList not exist";
              while(temp!=NULL)
                 {
                 cout<<" - "<<temp->info;
                 temp=temp->next;
                 }
               }
             void search(int data)
              {
              node *temp;
              int a=1;
              temp=head;
              if(temp==NULL)
              cout<<"\n\t List not exist ";
              else
                  {
                while(temp!=NULL)
                   {
                    if(temp->info==data)
                       {
                       cout<<"\n\t Element fount at : "<<a<<" Position ";
                       break;
                       }
                    a++;
                    temp=temp->next;
                   }
                if(temp==NULL)
                  cout<<"\n\t Element is not in list ";
                  }

             }
             };
void main()
{
            list l;
            clrscr();
            int ch,pos;
            while(1)
              {
              cout<<"\n\t\tENter your Choice : ";
              cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
              cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
              cout<<"\n 7: To show\n 8: To exit\n 9: To Search   ";
              cin>>ch;
              if(ch==1)
              {
              cout<<"\n\t Enter data to append : ";
              cin>>ch;
              l.append(ch);
              }
             else if(ch==2)
               {
               cout<<"\n\t Enter element to insert at begining : " ;
               cin>>ch;
               l.beg_insert(ch);
               }
              else if(ch==3)
            {
            cout<<"\nEnter element to insert at specific position  :  ";
            cin>>ch;
            cout<<"\n Enter position  : ";
            cin>>pos;
               l.in_insert(pos,ch);
               }
          else if(ch==4)
             l.del();
          else if (ch==5)
            l.beg_del();
          else if(ch==6)
          {
            cout<<"\n Enter position where u have to delete";
            cin>>pos;
            l.mid_del(pos);
              }
           else if(ch==7)
               l.show();
            else if(ch==8)
              {
              cout<<"\n Press Enter to Exit";
              break;
              }
            else if(ch==9)
               {
               cout<<"\n Enter your number for searching in list : ";
               cin>>ch;
               l.search(ch);
               }
             else
               cout<<"\n\t Wrong Choice   ";
            }
        getch();
}

No comments:

Post a Comment