Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
1K views

Circular Link List Program in C++

This C++ program implements functions for a linked list including insertion, deletion, and traversal at the beginning, end, and a specific position. The main function displays an menu and calls the message function which gets user input and calls the appropriate linked list function. Functions are defined to insert, delete, and traverse nodes in the linked list by changing the next pointers between nodes.

Uploaded by

pratikjain998
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Circular Link List Program in C++

This C++ program implements functions for a linked list including insertion, deletion, and traversal at the beginning, end, and a specific position. The main function displays an menu and calls the message function which gets user input and calls the appropriate linked list function. Functions are defined to insert, delete, and traverse nodes in the linked list by changing the next pointers between nodes.

Uploaded by

pratikjain998
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<conio.

h>
#include<iostream.h>
#include<stdlib.h>
void message();
void insertion_beg();
void insertion_end();
void insertion_pos();
void deletion_beg();
void deletion_end();
void deletion_pos();
void traverse();
struct ll{
struct ll *next;
int data;
};
struct ll *start=NULL;
void main(){
clrscr();
cout<<"Please enter 1 for insertion at beginning\n";
cout<<"Please enter 2 for insertion at ending\n";
cout<<"Please enter 3 for insertion at specific position\n";
cout<<"Please enter 4 for Deletion at beginning\n";
cout<<"Please enter 5 for Deletion at ending\n";
cout<<"Please enter 6 for Deletion at specific position\n";
cout<<"please enter 7 for traversal\n";
cout<<"please enter 8 for exit\n";
message();
getch();
}
void message(){
int choice;
cout<<"please enter your choice\n";
cin>>choice;
switch(choice){
case 1:
insertion_beg();
break;
case 2:
insertion_end();
break;
case 3:
insertion_pos();
break;
case 4:
deletion_beg();
break;
case 5:
deletion_end();
break;
case 6:
deletion_pos();
break;
case 7:
traverse();
break;
case 8:
exit(1);
break;
default:
cout<<"please enter valid choice";
message();
break;
}
}
void insertion_end(){
struct ll *node=new ll;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=start;
}
else{
struct ll *p=start;//creating a temp pointer
while(p->next!=start){
p=p->next;
}
p->next=node;
node->next=start;
cout<<"please enter data\n";
cin>>node->data;
}
message();
}
void insertion_beg(){
struct ll *node=new ll;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=NULL;
start->next=start;
}
else{
struct ll *p=start;
while(p->next!=start){
p=p->next;
}
node->next=start;
cout<<"enter data\n";
cin>>node->data;
start=node;
p->next=start;
}
message();
}
void insertion_pos(){
struct ll * node= new ll;
int pos,i;
cout<<"please enter position\n";
cin>>pos;
if(start==NULL){
start=node;
cout<<"please enter data\n";
cin>>start->data;
start->next=NULL;
start->next=start;
}
else{
struct ll *p=start;
struct ll *q;
i=1;
while(p->next!=NULL && pos!=i){
q=p;
p=p->next;
i=i+1;
}
if(pos==1){
insertion_beg();
}
else if(pos>i){
p->next=node;
node->next=start;
cout<<"please enter data\n";
cin>>node->data;
}
else if(pos<1){
cout<<"please enter valid position\n";
}
else{
cout<<"please enter data\n";
cin>>node->data;
node->next=p;
q->next=node;
}
}
message();
}
void deletion_end(){
//in this we are deleting last node
struct ll *p=start;//creating two temp pointer p and q
struct ll *q=NULL;
if(start==NULL){
cout<<"link list is empty\n";
}
else{
while(p->next!=start){
q=p;//q hold 1 less position than p
p=p->next;
}
if(q==NULL)
start=NULL;
else
q->next=start;
delete p;
}
message();
}
void deletion_beg(){
struct ll *p=start;
struct ll *r=start;
if(start->next==start){
delete p;
start=NULL;
}
else if(start==NULL){
cout<<"Link List is empty\n";
}
else{
while(r->next!=start){
r=r->next;
}
start=p->next;
delete p;
r->next=start;
}
message();
}
void deletion_pos(){
if(start==NULL){
cout<<"Link List is empty\n";
}
else{
struct ll *p=start;
struct ll *q;
int i=1,pos;
cout<<"please enter specific position\n";
cin>>pos;
while(p->next!=NULL && pos!=i){
q=p;
p=p->next;
i=i+1;
}
if(pos==1){
deletion_beg();
}
else if(pos>i){
cout<<"\nlist contain only"<<i<<"element\n";
}
else if(pos==i && p->next==NULL){
deletion_end();
}
else{
q->next=p->next;
delete p;
}
}
message();
}
void traverse(){
if(start==NULL){
cout<<"Link List Empty\n";
message();
}
else{
struct ll *p=start;
while(p->next!=start){
cout<<p->data<<endl;
p=p->next;
}
cout<<p->data<<endl;
message();
}
}

You might also like