Menu Driven Program For Circular Linked List: creation, traversing, insertion and deletion(Circular Linked List)

 Menu Driven Program For Circular Linked : creation, traversing, insertion and deletion(Circular Linked List)

Circular Linked List is a very useful data structure and it also has various daily life applications . The below C++ program shows various operations for Circular Linked List like creation of circular linked list, traversing or displaying it, insertion and deletion operations in circular linked list at various locations that is at the end , in the beginning and at any given location. 

 Menu driven program for Circular linked list operations

a) Creation of List

b) Traversing of List

c) Insertion operation

d) Deletion operation


#include <bits/stdc++.h>

using namespace std;

struct Node

{

    int data;

    struct Node *next;

}*Head=NULL;

 

void create(int a[],int n)

{

    struct Node *t,*last;

    Head=new Node;

    Head->data=a[0];

    Head->next=Head;

    last=Head;

  

        for(int i=1;i<n;i++)

        {

           t=new Node;

    t->data=a[i];

    t->next=last->next;

    last->next=t;

    last=t;

        }

}

 

int count(struct Node *p)

{

    int c=0;

    do

    {

        c++;

     p=p->next;

    }while(p!=Head);

    return c;

}

 

void insert(int pos, int x)

{

    if(pos>count(Head) || pos<0)

    return;

    struct Node *t,*p=Head;

    t=new Node;

    t->data=x;

    if(pos==0)

    {

        if(Head==NULL)

        {

            Head=t;

            Head->next=t;

        }

        while(p->next!=Head)

        p=p->next;

        t->next=Head;

        p->next=t;

        Head=t;

    }

    else{

        for(int i=0;i<pos-1;i++)

            p=p->next;

            t->next=p->next;

            p->next=t;

    }

}

 

int Delete(struct Node *p,int index)

{

    struct Node *q;

    int x=-1;

    if(index<1 || index>count(Head))

    return x;

     if(index==1)

    {

        x=p->data;

         while(p->next!=Head)

        p=p->next;

        if(Head==p)

        {

            delete Head;

            Head=NULL;

        }

        else

        {

        p->next=Head->next;

        delete Head;

        Head=p->next;

        }

        return x;

    }

    else

    {

    for(int i=0;i<index-2;i++)

    {

        p=p->next;

    }

    q=p->next;

    p->next=q->next;

    x=q->data;

    delete q;

    return x;

    }

}

void display(struct Node *p)

{

    do

    {

        cout<<p->data<<" ";

        p=p->next;

    }while(p!=Head);

}

 

int main()

{

    int a[500];

    int option,n,pos,x,index,t;

    do

    {

        cout<<"1. Create Circular Linked list"<<endl<<"2. Insert in Circular Linked list"<<endl<<"3. Delete "<<endl<<"4. Display"<<endl<<"5. Exit"<<endl;;

        cout<<"Enter an option :"<<endl;

        cin>>option;

        switch(option)

        {

        case 1 :

        {

            cout<<"Enter no of integers : "<<endl;

            cin>>n;

            cout<<"Enter the numbers"<<endl;

            for(int i=0;i<n;i++)

            cin>>a[i];

            create(a,n);

            cout<<endl;

            break;

        }

        case 2:

        {

            cout<<"Enter position to insert an element : "<<endl;

            cin>>pos;

            cout<<"Enter element : "<<endl;

            cin>>x;

            insert(pos,x);

            cout<<endl;

            break;

        }

        case 3:

        {

            cout<<"Enter position to delete element : "<<endl;

            cin>>index;

            cout<<"Deleted element is: "<<Delete(Head,index);

            cout<<endl;

            break;

        }

        case 4:

        {

            cout<<"Displaying elements :";

            display(Head);

            cout<<endl;

            break;

        }

        default:

        cout<<"Exiting program......"<<endl;

        }

    }while(option<=4);

    return 0;

}


Creation and Displaying of Circular Linked List



Insertion in Circular Linked List


Deletion in Circular Linked List

Recommended Posts

Comments

Post a Comment

Feel free to ask if you have any queries and if want any information . I feel happy to interact with you all and help you .

Popular posts from this blog

Notice Writing Format with Examples ( Notice writing )

CSAB (Fee Refund)

All Derivations of Ray Optics Class 12 ( Ray Optics )

Solid state previous year questions CBSE with answers( One mark)

JEE Mains -Mole Concept and Stoichiometry Previous Year Questions (with Answers)

CSAB 2020 ( CSAB Fee Refund )

Message Writing,Format for message writing

Electrostatics Important Five mark questions with answers for CBSE Class 12 (previous year)

Solid state previous year questions CBSE with answers( Two and Three Mark )