C++ Menu Driven Program for Circular Singly Linked List

By | 09.01.2017

Circular Singly Linked List


Write a C++ Menu Driven Program for Circular Singly Linked List. Here’s simple C++ Menu Driven Program for Circular Singly Linked List in C++ Programming Language.


What is Linked List ?


Linked list is a linear data structure that contains sequence of elements such that each element links to its next element in the sequence. Each link contains a connection to another link.

Following are the important terms to understand the concept of Linked List.

  • Link − Each link of a linked list can store a data called an element.
  • Next − Each link of a linked list contains a link to the next link called Next.

Each element in a linked list is called as “Node”. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs.


Below is the source code for C++ Menu Driven Program for Circular Singly Linked List which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Menu Driven Program for Circular Singly Linked List  */

#include<iostream>
#include<stdio.h>

using namespace std;

typedef struct nodee
    {
    int data;
    nodee *next;
    nodee *prev;
    }node;
node * addnode(node*);
void shownodes(node*);
node *delete_node(node*);
node *head;
int main()
{
    int ch;
    ch=1;
    head=NULL;
    while( ch != 0)
    {
    cout<<"\nMENU\n";
    cout<<"\t1.ADD A NODE\n";
    cout<<"\t2.DISPLAY THE NODES\n";
    cout<<"\t3.DELETION\n";
    cout<<"\t0.QUIT\n";
    cin>>ch;switch(ch)
        {
        case 1:
            head=addnode(head);break;
        case 2:
            shownodes(head);break;
        case 3:
            head=delete_node(head);break;
        case 0:
            ch=0;break;
        default:
            cout<<"INVALID CHOICE  ?? \n";break;
        }
    }
    return 0;
}
node * addnode(node *f)
{
    node *n;
    int ch;
    n=new node;
    cout<<"ENTER A ELEMENT TO BE INSERTED :";
    cin>>n->data;
    n->next=n->prev=NULL;
    if(f==NULL)
    {f=n;cout<<"\nLIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..\n";}
    else{
    cout<<"WHERE YOU WANT TO INSERT :\n";
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch)
    {
    case 1:
    n->next=f;
    f->prev=n;
    f=n;
    cout<<"SUCCESS\n";
    break;
    case 2:    
        node *temp;
        
        temp=f;
        while(temp->next != NULL)
        {
            temp=temp->next;
        }
        temp->next=n;
        n->prev=temp;
        cout<<"SUCCESS \n";
        break;
    default:
    cout<<"INVALID ??\n";break;
    }
    }
    return f;
}
void shownodes(node *f)
{
    node *temp=f;
    if ( temp == NULL ){
    cout<<endl<<"EMPTY LIST ..\n";
    return;}
    else{
    cout<<"HEAD";
    while(temp!=NULL)
    {cout<<" <=> ";
    cout<<temp->data;temp=temp->next;}
    cout<<" -> NULL "<<endl;}
}
node * delete_node(node *f)
{
    node *temp1,*temp2;int ch;
    temp1=temp2=f;
    if( temp1 == NULL ){
    cout<<"\nEMPTY LIST ... CANNOT BE DELETED \n";}
    else{
    cout<<"From\n\t1.BEG\n\t2.END\n";
    cin>>ch;
    switch(ch){
    case 1:f=f->next;
cout<<"SUCCESS\n";break;
    case 2: while(temp1->next!=NULL){
        temp2=temp1;temp1=temp1->next;}
        temp2->next=NULL;
        cout<<"SUCCESS \n";break;
    default:cout<<"INVALID ??\n";break;}}
    return f;
}

OUTPUT : :


/*  C++ Menu Driven Program for Circular Singly Linked List  */

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
1
ENTER A ELEMENT TO BE INSERTED :1

LIST IS EMPTY.. NODE IS INSERTED AT THE BEGINING..

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
1
ENTER A ELEMENT TO BE INSERTED :2
WHERE YOU WANT TO INSERT :
From
        1.BEG
        2.END
1
SUCCESS

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
1
ENTER A ELEMENT TO BE INSERTED :3
WHERE YOU WANT TO INSERT :
From
        1.BEG
        2.END
2
SUCCESS 

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
2
HEAD <=> 2 <=> 1 <=> 3 -> NULL 

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
3
From
        1.BEG
        2.END
1
SUCCESS

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
2
HEAD <=> 1 <=> 3 -> NULL 

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
3
From
        1.BEG
        2.END
2
SUCCESS 

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
3
From
        1.BEG
        2.END
3
INVALID ??

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
2
HEAD <=> 1 -> NULL 

MENU
        1.ADD A NODE
        2.DISPLAY THE NODES
        3.DELETION
        0.QUIT
0
 
Exit code: 0

Above is the source code and output for C++ Menu Driven Program for Circular Singly Linked List which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

5 1 vote
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments