Reverse a Linked List in DSA C++

Program 1

#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
    int data;
    Node *add;

    public:
    void create();
    void display();
    void insert_first();
    void insert_middle();
    void insert_last();
    void delete_first();
    void delete_middle();
    void delete_last();
    void searchData();
    void reverseDisplay();

};Node *start=NULL,*new1,*temp,*prev1,*next1;

void Node::reverseDisplay()
{
    Node *ar[500];
    if(start==NULL)
    cout<<"List not found.....";
    else
    {
        int i=0;
        temp=start;
        while(temp!=NULL)
        {
            ar[i]=temp;
            i++;
           temp=temp->add;   
        }
        i--;
        cout<<"Reverse order Elements: ";
        while(i>=0)
        {
           cout<<ar[i]->data<<"   ";  
           i--; 
        }

    }
}
void Node::create()
{

    int n;
    cout<<"\nEnter an element: ";
    cin>>n;
    start=new Node;
    start->data=n;
    start->add=NULL;
    temp=start;
    char ch;
    cout<<"\nWant to coninue(Y/y): ";
    cin>>ch;
    while(ch=='y'|| ch=='Y')
    {
        cout<<"\nEnter an element: ";
        cin>>n;
       new1=new Node;
       new1->data=n;
       new1->add=NULL;
       temp->add=new1;
       temp=temp->add;   
       cout<<"\nWant to coninue(Y/y): ";
       cin>>ch;
    }


}
void Node::display()
{
    if(start==NULL)
     cout<<"\n List not found";
   else
   {
      temp=start;
      cout<<"\nElements of List: ";
      while(temp!=NULL)
      {
         cout<<temp->data<<"   ";
         temp=temp->add;
      }
   }  
}
void Node::insert_first()
{
       int n;
      if(start==NULL)
      cout<<"\n List not found";
      else
      {
           cout<<"\n Enter an element for insert";
           cin>>n;
           new1=new Node;
           new1->data=n;
           new1->add=NULL;
           new1->add=start;
           start=new1;
      }
}
void Node::insert_middle()
{
    int n;
      if(start==NULL)
      cout<<"\n List not found";
      else
      {
          int pos,i;
          cout<<"\n Enter an element for insert";
           cin>>n;
           new1=new Node;
           new1->data=n;
           new1->add=NULL;
           cout<<"\nEnter poisition: ";
           cin>>pos;
           i=1;
           next1=start;
           while(i<pos)
           {
                prev1=next1;
                next1=next1->add;
                i++;
           }
           prev1->add=new1;
           new1->add=next1;
      }
}

void Node::insert_last()
{
    int n;
      if(start==NULL)
      cout<<"\n List not found";
      else
      {
            cout<<"\n Enter an element for insert";
           cin>>n;
           new1=new Node;
           new1->data=n;
           new1->add=NULL;

          temp=start;
          while(temp->add!=NULL)           
          {
              temp=temp->add;
          }
          temp->add=new1;
      }
}
void Node::delete_first()
{
       int n;
      if(start==NULL)
      cout<<"\n List not found";
      else
      {
         temp=start;
         cout<<"\nDeleted node is : "<<temp->data;
         start=start->add;
         delete temp;

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

void Node::searchData()
{
    int s,flag=0;
      if(start==NULL)
      cout<<"\n List not found";
      else
      {
          cout<<"Enter an element for search: ";
          cin>>s;
          temp=start;
          while(temp!=NULL)
          {
             if(temp->data==s)
             {
                  flag=1;
                  break;
             }
             temp=temp->add;
          }
         if(flag==1) 
         cout<<"\n Searching success: ";
        else
         cout<<"\n Searching not success: "; 
    }
}
int main()
{
    system("cls");
    int choice;
    Node obj;
  do
  {  
    cout<<"\n------------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 *