Sunday 16 December 2018

Doubly Linklist (insertion,deletion of a node)



#include <iostream>
using namespace std;
struct node2
{
    int data2;
    struct node2 *previous;
    struct node2 *next2;
};
int main()
{
    node2 *head2;
    node2 *last2;
    node2 *temp2;
    last2=0;
    int choice;
    do
    {
    cout<<"Enter data in Doubly node:"<<endl;
    temp2=new node2();
    cin>>temp2->data2;
    if(last2==0)
    {
        temp2->next2=0;
        temp2->previous=0;
        last2=head2=temp2;
    }
    else
    {
        temp2->next2=0;
        temp2->previous=last2;
        last2->next2=temp2;
        last2=temp2;
    }
    cout<<"\nDo you want to create a new node (1=yes,0=NO): ";
    cin>>choice;
    }while(choice==1);
    cout<<"Printing from start"<<endl;
    temp2=head2;
    while(temp2!=0)
    {
      cout<<temp2->data2<<" ";
     temp2=temp2->next2;
    }
    cout<<"\n Printing from end"<<endl;
    temp2=last2;
    while(temp2!=0)
    {
      cout<<temp2->data2<<" ";
     temp2=temp2->previous;
    }
   /******************************************/
       do
    {
    cout<<"Enter data"<<endl;
    temp2=new node2();
    cin>>temp2->data2;
    if(last2==0)
    {
        temp2->next2=0;
        temp2->previous=0;
        last2=head2=temp2;
    }
    else
    {
        temp2->next2=head2;
        temp2->previous=0;
        head2->previous=temp2;
        head2=temp2;
    }
    cout<<"\nDo you want to create a new node (1=yes,0=NO): ";
    cin>>choice;
    }while(choice==1);
    cout<<"\n Printing from start"<<endl;
    temp2=head2;
    while(temp2!=0)
    {
      cout<<temp2->data2<<" ";
     temp2=temp2->next2;
    }
    return 0;
}


0 comments:

Post a Comment