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();
}

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();
}

insert at after kth position in single link list (except first and last node)

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *link;
};
void main()
{
clrscr();
struct node *start=NULL,*temp,*ptr,*new1,*temp1;
int num,n,count=0,k,j;
temp=new node;
start=temp;
start->link=NULL;
start->data=10;
cout<<"\n how many number u want to enter";
cin>>n;
for(int i=0;i<n-1;i++)
{
cout<<"\n enter number";
cin>>num;
temp=new node;
temp->data=num;
temp->link=start;
start=temp;
}
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are="<<count;
cout<<"\n enter location where u want to insert";
cin>>k;
temp1=start;
for(j=1;j<=k;j++,temp1=temp1->link)
{
ptr=temp1;
}
cout<<"enter data part";
cin>>num;
new1=new node;
new1->data=num;
new1->link=temp1;
ptr->link=new1;
count=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are now="<<count;
getch();
}

insert at kth position in single link list (except first and last node)

#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *link;
};
void main()
{
clrscr();
struct node *start=NULL,*temp,*ptr,*new1,*temp1;
int num,n,count=0,k,j;
temp=new node;
start=temp;
start->link=NULL;
start->data=10;
cout<<"\n how many number u want to enter";
cin>>n;
for(int i=0;i<n-1;i++)
{
cout<<"\n enter number";
cin>>num;
temp=new node;
temp->data=num;
temp->link=start;
start=temp;
}
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are="<<count;
cout<<"\n enter location where u want to insert";
cin>>k;
temp1=start;
for(j=1;j<k;j++,temp1=temp1->link)
{
ptr=temp1;
}
cout<<"enter data part";
cin>>num;
new1=new node;
new1->data=num;
new1->link=temp1;
ptr->link=new1;
count=0;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<"\n element is="<<"\t"<<temp->data;
count++;
}
cout<<"\n total elements are now="<<count;
getch();
}