Doubly Linked List in DSA C++

Program 1

// Double Linked List
#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
    Node *ladd;
    int data;
    Node *radd;
    public:
    void create();
    void display();
    void reverseDisplay();
    void insert_first();
    void insert_middle();
    void insert_last();
    void delete_first();
    void delete_middle();
    void delete_last();
}; Node *start=NULL,*new1,*prev1,*next1,*temp,*last;
void Node::create()
{
       int n;
       char choice;
       cout<<"Enter an element: ";
       cin>>n;
       start=new Node();
       start->ladd=NULL;
       start->data=n;
       start->radd=NULL;
       temp=start;
       cout<<"Want to continue(y/Y): ";
       cin>>choice;
       while(choice=='y' || choice=='Y')
       {
           cout<<"Enter next element: ";
           cin>>n;
          new1=new Node();
          new1->ladd=NULL;
          new1->data=n;
          new1->radd=NULL;

          temp->radd=new1;
          new1->ladd=temp;

          temp=temp->radd;

          cout<<"Want to continue(y/Y): ";
          cin>>choice;

       }

}
void Node::display()
{
    if(start==NULL)
      cout<<"\n List is Empty.....";
     else
     {
        temp=start;
        while(temp!=NULL)
        {
            cout<<temp->data<<"    ";
            temp=temp->radd;
        }
     }
}
void Node::reverseDisplay()
{

    if(start==NULL)
      cout<<"\n List is Empty.....";
     else
     {
            temp=start;
            while(temp->radd!=NULL)
            {
                temp=temp->radd;
            }
            while(temp!=NULL)
            {
                cout<<temp->data<<"   ";
                temp=temp->ladd;
            }

     }
}
void Node::insert_first()
{
      if(start==NULL)
       cout<<"\n List is empty";
      else
      {
          int n;
          cout<<"\n Enter an element: ";
          cin>>n;
          new1=new Node();
          new1->data=n;
          new1->ladd=NULL;
          new1->radd=NULL;
         new1->radd=start;
         start->ladd=new1;
         start=new1;
      } 
}

void Node::insert_middle()
{

    if(start==NULL)
       cout<<"\n List is empty";
      else
      {
          int n,pos,i=1;
          cout<<"\n Enter an element: ";
          cin>>n;
          new1=new Node();
          new1->data=n;
          new1->ladd=NULL;
          new1->radd=NULL;
          cout<<"\n Enter poistion of Middle: ";
          cin>>pos;
          next1=start;
          while(i<pos)
          {
             prev1=next1;
             next1=next1->radd;
             i++;
          }
          prev1->radd=new1;
          new1->ladd=prev1;
          new1->radd=next1;
          next1->ladd=prev1;
      }   
}

void Node::insert_last()
{
    if(start==NULL)
       cout<<"\n List is empty";
      else
      {
          int n;
          cout<<"\n Enter an element: ";
          cin>>n;
          new1=new Node();
          new1->data=n;
          new1->ladd=NULL;
          new1->radd=NULL;
          last=start;
          while(last->radd!=NULL)
          {
            last=last->radd;
          }
          last->radd=new1;
          new1->ladd=last;
      }

}
void Node::delete_first()
{
    if(start==NULL)
       cout<<"\n List is empty";
      else
      {
          temp=start;
          start=start->radd;
          cout<<"\n Deleted node is: "<<temp->data;
          delete temp;
      }
}
void Node::delete_middle()
{
    if(start==NULL)
       cout<<"\n List is empty";
      else
      {
              int pos,i=1;
              cout<<"\n Enter the node poistion for delete: ";
              cin>>pos;
              next1=start;
              while(i<pos) 
              {
                  prev1=next1;
                  next1=next1->radd;
                  i++;
              }
              temp=next1;
              next1=next1->radd;
              prev1->radd=next1;
              next1->ladd=prev1;
              cout<<"\n Deleted node is : "<<temp->data;
              delete temp;
      }
}
void Node::delete_last()
{
     if(start==NULL)
       cout<<"\n List is empty";
      else
      {
           temp=start;
           while(temp->radd!=NULL)
           {
               prev1=temp;
               temp=temp->radd;
           }
           prev1->radd=NULL;
           temp->ladd=NULL;
           cout<<"\n Deleted node is: "<<temp->data;
          delete temp;
      }
}

int main()
{
    system("cls");
    int choice;
    Node obj;
  do
  {  
    cout<<"\n------------Double Linked List Menu-----------------------";
    cout<<"\n 1. Create";
    cout<<"\n 2. Display";
    cout<<"\n 3. Insert First";
    cout<<"\n 4. Insert Middle";
    cout<<"\n 5. Insert Last";
    cout<<"\n 6. Delete First";
    cout<<"\n 7. Delete Middle";
    cout<<"\n 8. Delete Last";
    cout<<"\n 9. Search";
    cout<<"\n 10.Reverse Display";
    cout<<"\n 11. Exit";
    cout<<"\n-------------------------------------------------------";
    cout<<"\n Enter your choice: ";
    cin>>choice;
    switch(choice)
    {
        case 1:obj.create();break;
        case 2:obj.display();break;
        case 3:obj.insert_first();break;
        case 4:obj.insert_middle();break;
        case 5:obj.insert_last();break;
        case 6:obj.delete_first();break;
        case 7:obj.delete_middle();break;
        case 8:obj.delete_last();break;
        //case 9:obj.searchData();break;
            case 10:obj.reverseDisplay();break;
            case 11:break;
            default:cout<<"\n Invalid choice";
    }
}while(choice!=11);
    return 0;
}

 

courses
Image

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *