C++ program to implement circular linked list
C++ program to implement circular linked list
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
typedef struct links
{
int data;
struct links *next;
}links;
class linkedlist
{
links *start;
public:
linkedlist()
{
start=NULL;
}
void create();
void insert();
void display();
void delet();
void search();
void count();
void erase(links *);
links *getstart()
{
return start;
}
void setstart()
{
start=NULL;
}
};
void linkedlist::create()
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
start=new links;
start->data=take;
start->next=start;
temp=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::insert()
{
int a,da;
links *ne,*temp=start;
cout<<"Enter the element to be inserted ";
cin>>da;
ne=new links;
ne->data=da;
if(start!=NULL)
{
cout<<"Enter your choice\n 1.Insert as a first node\n 2.Insert as a last node\n 3.Insert between node ";
cin>>a;
switch(a)
{
case 1:
ne->next=start;
while(temp->next!=start)
temp=temp->next;
temp->next=ne;
start=ne;
break;
case 2:
ne->next=start;
while(temp->next!=start)
temp=temp->next;
temp->next=ne;
break;
case 3:
cout<<"Enter the element after which element to be inserted ";
cin>>da;
while(temp->data!=da)
temp=temp->next;
ne->next=temp->next;
temp->next=ne;
break;
default:
cout<<"Sorry wrong choice\n";
break;
}
}
else
{
start=ne;
start->next=start;
}
}
void linkedlist::delet()
{
int a;
links *de,*pre,*temp=start;
display();
cout<<"Enter the element to be deleted ";
cin>>a;
while(temp->data!=a)
{
if(temp->next==start)
{
cout<<"Entered element is not present";
return ;
}
pre=temp;
temp=temp->next;
}
if(temp==start)
{
if(start->next==start)
{
delete temp;
setstart();
}
else
{
while(temp->next!=start)
temp=temp->next;
temp->next=start->next;
temp=start;
start=start->next;
delete temp;
}
}
else if(temp->next==start)
{
pre->next=start;
delete temp;
}
else
{
pre->next=temp->next;
delete temp;
}
display();
}
void linkedlist::display()
{
links *temp=start;
if(start==NULL)
{
cout<<"Linked list is empty "<<endl;
return;
}
while(temp->next!=start)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void linkedlist::search()
{
int a;
links *temp=start;
cout<<"Enter the element to be searched ";
cin>>a;
while(temp->data!=a)
{
if(temp->next==start)
{
cout<<"Element is not present "<<endl;
return;
}
temp=temp->next;
}
cout<<"Element is present"<<endl;
}
void linkedlist::count()
{
int c=0;
links *temp=start;
if(temp==NULL)
cout<<"Linked List is Empty "<<endl;
else
{
while(temp->next!=start)
{
temp=temp->next;
c++;
}
cout<<"Total no. of nodes in linked list "<<c+1<<endl;
}
}
void linkedlist::erase(links *temp)
{
if(temp->next=start)
{
delete temp;
return ;
}
else
{
temp=temp->next;
erase(temp);
delete temp;
}
}
int main()
{
int a,flag=1;
linkedlist linklist;
while(flag)
{
cout<<"Main Menu\n\t1.) Creation\n\t2.) Display\n\t3.) Insert\n\t4.) Delete\n\t5.) Search\n\t6.) Count Nodes\n\t7.) Erase Linked List\n\t8.) Exit";
cout<<"\nEnter your choice ";
cin>>a;
switch(a)
{
case 1:
linklist.create();
break;
case 2:
linklist.display();
break;
case 3:
linklist.insert();
break;
case 4:
linklist.delet();
break;
case 5:
linklist.search();
break;
case 6:
linklist.count();
break;
case 7:
linklist.erase(linklist.getstart());
linklist.setstart();
break;
case 8:
flag=0;
default:
cout<<"Sorry Wrong choice";
}
}
}
#include<conio.h>
#include<iostream>
using namespace std;
typedef struct links
{
int data;
struct links *next;
}links;
class linkedlist
{
links *start;
public:
linkedlist()
{
start=NULL;
}
void create();
void insert();
void display();
void delet();
void search();
void count();
void erase(links *);
links *getstart()
{
return start;
}
void setstart()
{
start=NULL;
}
};
void linkedlist::create()
{
int take;
links *temp;
char ch;
cout<<"Enter the element ";
cin>>take;
start=new links;
start->data=take;
start->next=start;
temp=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
while(ch!='n'&&ch!='N')
{
cout<<"Enter the element ";
cin>>take;
temp->next=new links;
temp=temp->next;
temp->data=take;
temp->next=start;
cout<<"Want to enter more elements (Y/N)\n";
ch=getch();
}
}
void linkedlist::insert()
{
int a,da;
links *ne,*temp=start;
cout<<"Enter the element to be inserted ";
cin>>da;
ne=new links;
ne->data=da;
if(start!=NULL)
{
cout<<"Enter your choice\n 1.Insert as a first node\n 2.Insert as a last node\n 3.Insert between node ";
cin>>a;
switch(a)
{
case 1:
ne->next=start;
while(temp->next!=start)
temp=temp->next;
temp->next=ne;
start=ne;
break;
case 2:
ne->next=start;
while(temp->next!=start)
temp=temp->next;
temp->next=ne;
break;
case 3:
cout<<"Enter the element after which element to be inserted ";
cin>>da;
while(temp->data!=da)
temp=temp->next;
ne->next=temp->next;
temp->next=ne;
break;
default:
cout<<"Sorry wrong choice\n";
break;
}
}
else
{
start=ne;
start->next=start;
}
}
void linkedlist::delet()
{
int a;
links *de,*pre,*temp=start;
display();
cout<<"Enter the element to be deleted ";
cin>>a;
while(temp->data!=a)
{
if(temp->next==start)
{
cout<<"Entered element is not present";
return ;
}
pre=temp;
temp=temp->next;
}
if(temp==start)
{
if(start->next==start)
{
delete temp;
setstart();
}
else
{
while(temp->next!=start)
temp=temp->next;
temp->next=start->next;
temp=start;
start=start->next;
delete temp;
}
}
else if(temp->next==start)
{
pre->next=start;
delete temp;
}
else
{
pre->next=temp->next;
delete temp;
}
display();
}
void linkedlist::display()
{
links *temp=start;
if(start==NULL)
{
cout<<"Linked list is empty "<<endl;
return;
}
while(temp->next!=start)
{
cout<<temp->data<<"-> ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
void linkedlist::search()
{
int a;
links *temp=start;
cout<<"Enter the element to be searched ";
cin>>a;
while(temp->data!=a)
{
if(temp->next==start)
{
cout<<"Element is not present "<<endl;
return;
}
temp=temp->next;
}
cout<<"Element is present"<<endl;
}
void linkedlist::count()
{
int c=0;
links *temp=start;
if(temp==NULL)
cout<<"Linked List is Empty "<<endl;
else
{
while(temp->next!=start)
{
temp=temp->next;
c++;
}
cout<<"Total no. of nodes in linked list "<<c+1<<endl;
}
}
void linkedlist::erase(links *temp)
{
if(temp->next=start)
{
delete temp;
return ;
}
else
{
temp=temp->next;
erase(temp);
delete temp;
}
}
int main()
{
int a,flag=1;
linkedlist linklist;
while(flag)
{
cout<<"Main Menu\n\t1.) Creation\n\t2.) Display\n\t3.) Insert\n\t4.) Delete\n\t5.) Search\n\t6.) Count Nodes\n\t7.) Erase Linked List\n\t8.) Exit";
cout<<"\nEnter your choice ";
cin>>a;
switch(a)
{
case 1:
linklist.create();
break;
case 2:
linklist.display();
break;
case 3:
linklist.insert();
break;
case 4:
linklist.delet();
break;
case 5:
linklist.search();
break;
case 6:
linklist.count();
break;
case 7:
linklist.erase(linklist.getstart());
linklist.setstart();
break;
case 8:
flag=0;
default:
cout<<"Sorry Wrong choice";
}
}
}
No comments:
Post a Comment