Friday 31 August 2012

( DOUBLE LINKED LIST ) insert at beggining,Display,Delete at beggining,insert at end in double linked list by SANYOG KESHRI

#include<conio.h>
#include<iostream.h>
struct node
{
struct node *plink;
int data;
struct node *flink;
};

void main()
{

int num,n;
struct node *temp,*ptr,*start=NULL;

do
{

cout<<"------------Double Linked List------------"<<endl;
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 insert at end:-             *"<<endl;
cout<<"******************************************"<<endl;
cin>>n ;

switch(n)
 {
case 1:
        {
            temp=new node;
            temp->plink=NULL;
            temp->data=100;
            temp->flink=NULL;
            start=temp;

            for(int i=1;i<=4;i++)
                {
                 cout<<"Enter number in list:--"<<endl;
                 cin>>num;
                 temp=new node;
                 temp->data=num;
                 temp->flink=start;
                 start->plink=temp;
                 temp->plink=NULL;
                 start=temp;
                }
                break;
        }
case 2:
             {
                 if(start==NULL)
                     {
                        cout<<"No  elemnt In list:--"<<endl;
                     }
                 else
                     {
                      cout<<"Data in List:--"<<endl;
                      for(int i=1;i<=5;i++)
                            {
                                cout<<"----"<<endl;
                                cout<<temp->data<<endl;
                                cout<<"----"<<endl;
                            temp=temp->flink;

                            }

                    }
                    break;
             }
case 3:
            {
            if (start==NULL)
            {
            cout<<"No element in list..."<<endl;
            }
            else
            {
                 start=start->flink;
                 start->plink=NULL;
                cout<<"Data after Deleteing at the begging."<<endl;
                for(temp=start;temp!=NULL;temp=temp->flink)
                    {

                                cout<<"----"<<endl;
                                cout<<temp->data<<endl;
                                cout<<"----"<<endl;
                    }
            }
            break;
            }
case 4:
        {
        if (start==NULL)
            {
            cout<<"No element in list..."<<endl;
            }
            else
            {
            cout<<"Enter data at the end:-"<<endl;
            cin>>num;
             ptr=new node;
             ptr->plink=NULL;
             ptr->flink=NULL;
             ptr->data=num;
             temp=start;
             while(temp->flink!=NULL)
             {
             temp=temp->flink ;
             }
             ptr->plink=temp->flink;
             temp->flink=ptr;
             ptr->flink=NULL;

             cout<<"Data after insert at the end."<<endl;
                for(temp=start;temp!=NULL;temp=temp->flink)
                    {

                                cout<<"----"<<endl;
                                cout<<temp->data<<endl;
                                cout<<"----"<<endl;
                    }

            }
            break;

        }
default:
         {
         cout<<"Invalid  number in list"<<endl;
         break;
         }
      }
 }while(n<=4);



getch();
}

No comments:

Post a Comment