Friday 31 August 2012

insert at beggining,Display,Delete at beggining,Exit ,insert at end,Delete at end in single linked list by SANYOG KESHRI

// sanyog keshri

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
    {
                int n;
                struct node *ptr,*temp,*start=NULL;
        do
            {
cout<<"******************************************"<<endl;
cout<<"Press 1  for insert at beggining:-       *"<<endl;
cout<<"Press 2 for Display:-                    *"<<endl;
cout<<"Press 3  for Delete at beggining         *"<<endl;
cout<<"Press 4  for Exit :-                     *"<<endl;
cout<<"Press 5  for insert at end:-             *"<<endl;
cout<<"Press 6  for Delete at end:-             *"<<endl;

cout<<"******************************************"<<endl;
cin>>n ;
    switch(n)
    {
case 1:
            {


        temp=new node;
        temp->data=10;
        temp->link=NULL;
        start=temp;
        cout<<"first number allready enterd:--"<<endl ;
        for(int i=1;i<=4;i++)
            {
                int num;
                cout<<"Enter number in link:--"<<endl ;
                cin>>num;
                temp=new node;
                temp->data=num;
                temp->link=start;
                start=temp;
            }

        break;
             }


case 2:
        {
        if(start==NULL)
        {
        cout<<"No element in link "<<endl  ;
        }
        else
        {
         cout<<"Data is :--"<<endl;
        for(int i=1;i<=5;i++)
        {
        cout<<temp->data<<endl;
        temp=temp->link;
        }
        }
            break;
          }


case 3:
        {
        if(start==NULL)
        {
        cout<<"No element in link list.."<<endl;
        }
        else
        {
        cout<<"Link list after deletion..."<<endl;
        start=start->link;
        for(temp=start;temp!=NULL;temp=temp->link)
        {
        cout<<temp->data<<endl;
        }
        }

        break;
        }

case 4:
          {
             cout<<"Exited......";

        break;
        }
case 5:
        {

         if(start==NULL)
         {
         cout<<"No element in link..."<<endl;
         }
         else
         {
            int n;
            ptr=new node;
            cout<<"Enter data :--"<<endl;
            cin>>n;
            ptr->data=n;
            temp=start;
            while(temp->link!=NULL)
            {
            temp=temp->link;
            }

            temp->link=ptr;
            ptr->link=NULL;
            cout<<"after adding data at last:--"<<endl;

         for(temp=start;temp!=NULL;temp=temp->link)
         {
         cout<<temp->data<<endl;
         }

          }
        break;
             }
case 6:
        {
             if(start==NULL)
             {
             cout<<"No element in link";
             }
             else
             {
             temp=start;
                  while(temp->link!=NULL)
                  {
                  ptr=temp;
                  temp=temp->link;
                  }

                  ptr->link=NULL;
                  cout<<"After deletion of link at the end.."<<endl;
         for(temp=start;temp!=NULL;temp=temp->link)
         {
         cout<<temp->data<<endl;
         }
         break;

             }

        }
        default:
        {
        cout<<"Enter number is invalid..."<<endl;
        break;
        }
        }
}
while(n<=6);




getch();
}

No comments:

Post a Comment