Circular Linked List in DSA C++

Program 1

// Circular Linkedlist
#include<iostream>
#include<stdio.h>
using namespace std;
class Node
{
    int data;
    Node *add;
   public:
   void create();
   void insertFirst();
   void insertMiddle();
   void insertLast();
   void deleteFirst();
   void deleteMiddle();
   void deleteLast();
   void display();
};  
Node *start=NULL,*new1,*temp,*prev1,*next1,*last;  
void Node::create()
{
     int n;
     char ch;
     cout<<"Enter an element";
     cin>>n;
     start=new Node();
     start->data=n;
     start->add=NULL;
     temp=start;
     temp->add=start;
     input1: cout<<"\n Want to continue:(Y/y) ";
     cin>>ch;
    
     while(ch=='y' || ch=='Y')
     {
        cout<<"Enter an element: ";
        cin>>n;
        new1=new Node();
        new1->data=n;
        new1->add=NULL;
        temp->add=new1;
        new1->add=start;
        temp=temp->add;
        input: cout<<"\n Want to continue:(Y/y) ";
        cin>>ch;
     }
  }   

void Node::insertFirst()
{
     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;
        last=start;
        do
        {
             last=last->add;
        }while(last->add!=start);
        new1->add=start;
        start=new1;
        last->add=start;
    }

}

void Node::insertMiddle()
{
        int n,pos,i;
    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;
        cout<<"\n Enter poistion: ";
        cin>>pos;
        i=1;
        next1=start;
        while(i<pos)
        {
               prev1=next1;
               next1=next1->add;
               i++;
        }
        prev1->add=new1;
        new1->add=next1;
    }     
}
void Node::insertLast()
{
      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;
         last=start;
        do
        {
             last=last->add;
        }while(last->add!=start);
        last->add=new1;
        new1->add=start;
    }  
     
}
void Node::deleteFirst()
{
      int n;
    if(start==NULL)
        cout<<"\n List not found....";
    else
    {
         last=start;
        do
        {
             last=last->add;
        }while(last->add!=start);
        temp=start;
        start=start->add;
        last->add=start;
        cout<<"\n Deleted node is : "<<temp->data;
        delete temp;
    }
}
void Node::deleteMiddle()
{
      if(start==NULL)
        cout<<"\n List not found....";
    else
    {
        int pos,i=1;
        cout<<"Enter position for delete: ";
        cin>>pos;
        next1=start;
        while(i<pos)
        {
            prev1=next1;
            next1=next1->add;
            i++;
        }
        temp=next1;
        next1=next1->add;
        prev1->add=next1;
        cout<<"Deleted node is : "<<temp->data;
        delete temp;
    }
}
void Node::deleteLast()
{
       if(start==NULL)
        cout<<"\n List not found....";
    else
    {
         last=start;
         do
         {
               prev1=last;
               last=last->add;
         }while(last->add!=start);

         prev1->add=start;
         cout<<" Deleted node is : "<<last->data;
         delete last;
    }
}
void Node::display()
{
    if(start==NULL)
    cout<<"\n List not created....";
   else
   {
         temp=start;
          do
          {
             cout<<temp->data<<"   ";
             temp=temp->add;
          }while(temp!=start);
   } 
}

int main()
{
    system("cls");
    Node obj;
    int choice;
   do
  {  
    cout<<"\n---------------------Circular Linked List-----------------";
    cout<<"\n 1. Create";
    cout<<"\n 2. Insert First";
    cout<<"\n 3. Insert Middle";
    cout<<"\n 4. Insert Last";
    cout<<"\n 5. Delete First";
    cout<<"\n 6. Delete Middle";
    cout<<"\n 7. Delete Last";
    cout<<"\n 8. Display";
    cout<<"\n 9. Exit";
    cout<<"\n---------------------------------------------------------";
    cout<<"\n Enter your choice: ";
    cin>>choice;
               
    switch(choice)
    {
         case 1:obj.create();break;
         case 2:obj.insertFirst();break;
         case 3:obj.insertMiddle();break;
         case 4:obj.insertLast();break;
         case 5: obj.deleteFirst();break;
         case 6: obj.deleteMiddle();break;
         case 7: obj.deleteLast();break;
         case 8:obj.display();break;
         case 9:break;
         default:cout<<"\n Invalid choice";
    }
}while(choice!=9); 
    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 *