Tuesday 22 January 2013

link list implementation


/*===================link list implementation=============*/
                   #include <iostream>
                   #include <conio>
                   #include <stdlib>
                   /***************/

      struct node
    {
       int data;

       node *link;
    };

           node *head=NULL;


     void main()
{
   void append();
   void display();
   void insert();
   void dele();
    int opt;
   while(1)
 {
    cout<<"*********************************************\n";
    cout<<"=============================================\n";
    cout<<"      MAIN MENU\n";
    cout<<"                    1:append                    \n";
    cout<<"                    2:Display                  \n";
     cout<<"                   3:Insert                  \n";
      cout<<"                  4:Delete                  \n";
      cout<<"                  5:EXIT                 \n";
    cout<<"=============================================\n";
    cout<<"**********************************************\n";
    cout<<endl;
    cin>>opt;
    switch(opt)
  {
    case 1:
    append();
    break;

    case 2:
    display();
   break;
   case 3:
     insert();
     break;

     case 4:

     dele();

     break;

     case 5:

     exit(0);

     break;
 }}                   getch();
                                }



             /**************************************/
                  void append()
                {
                   node *t,*n=head;
                 t=new node;

                    cout<<"               Enter element in link list\t\t";

                  cin>>t->data;
                  cout<<endl;

               t->link=NULL;

                 if(head==NULL)
                   head=t;

                   else
                 {

               while(n->link!=NULL)

                { n=n->link; }
                 n->link=t;

                   }
                           }
             /**************************************************/
                void display()
              {  node *t,*n=head;
                   if(n==NULL)

                 cout<<"link list is empty:";
              else
            {
              while(n!=NULL)

               { cout<<"    Value :                                 \t"<<n->data<<endl;
                n=n->link;}
            }

              }           void insert()
                  {
                     node *t,*n=head;
                    int num;

                    cout<<"Enter integer value:\t\t";
                    cin>>num;
                  cout<<endl;
                  while( (n->data!=num) && (n!=NULL) )

                   n=n->link;

                   if(n==NULL)

                cout<<"           Element not found                             \n\n ";


                else

              {
                         t=new node ;


                         cout<<"Enter element\t\t";
                         cin>>t->data;

                         cout<<endl;
                         t->link=n->link;
                         n->link=t;
               }                             }


                          /*================================================*/
                                     void dele()
                                  {
                                       node *t,*n=head;
                                       int num;
                                  if(head==NULL)
                                  {
                                    cout<<"List is Empty\n\n";
                                    return;
                                  }
                                        cout<<"        Enter element to be deleted \t\t";
                                        cin>>num;
                                        cout<<endl;
                                     while(n->data!=num)

                                     {
                                        t=n;
                                        n->link;

                                        if(n==NULL)
                                      {
                                         cout<<"            Element not found\n\n";

                                       }
                                          return ;}
                                                    if(n==head)
                                                  {
                                                     head=n->link;
                                                     delete n;
                                                   }

                                                         else

                                                     {
                                                        t->link=n->link;
                                                        delete n;
                                                     }

                                               }








0 comments:

Post a Comment