/* circuler link list */
//Single Circular Linked List
#include<iostream>
using namespace std;
class cnode
{
private:
int data;
cnode *next;
public:
void insert();
void add();
void search();
void update();
void delet();
void show();
};
cnode *head=NULL;
cnode *tail=NULL;
cnode *current=NULL;
cnode *temp=NULL;
int p,num;
int size=0;
int main()
{
cnode c1;
string x;
while(1)
{
cout<<"\n\n1 to insert node"<<endl;
cout<<"\n2 to Add node"<<endl;
cout<<"\n3 to Search in Nodes"<<endl;
cout<<"\n4 to update a value"<<endl;
cout<<"\n5 to delete a node"<<endl;
cout<<"\n6 to display"<<endl;
cout<<"\n7 to exit: ";
cin>>x;
if(x=="1")
{
c1.insert();
}
else if(x=="2")
{
c1.add();
}
else if(x=="3")
{
c1.search();
}
else if(x=="4")
{
c1.update();
}
else if(x=="5")
{
c1.delet();
}
else if(x=="6")
{
c1.show();
}
else if(x=="7")
{
return 0;
}
else
{
cout<<"\nInvalid command\n"<<endl;
}
}
}
//insertion function
void cnode::insert()
{
cout<<"\nEnter value: ";
cin>>num;
temp=new cnode;
temp->data=num;
temp->next=head;
if(head==NULL)
{
head=temp;
tail=temp;
current=temp;
}
else
{
tail->next=temp;
tail=temp;
current=temp;
}
size++;
cout<<"\nNode Created successfully\n"<<endl;
}
//insertion function
//Addition Function, to insert in mid or start or last
void cnode::add()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
cout<<"\n0 to add node in the start"<<endl;
cout<<"OR any other position after which you want to Add the node: ";
cin>>p;
if(p<0 ||p>size)
{
cout<<"\nInvalid Position Entered\n"<<endl;
}
else
{
cout<<"\nEnter value: ";
cin>>num;
temp=new cnode;
temp->data=num;
temp->next=NULL;
if(p==0)
{
temp->next=head;
head=temp;
}
if(p>0 && p<size)
{
current=head;
for(int i=1;i<p;i++)
{
current=current->next;
}
temp->next=current->next;
current->next=temp;
}
if(p==size)
{
current=tail;
current->next=temp;
temp->next=head;
tail=temp;
current=temp;
}
size++;
cout<<"\nNode Added Successfully\n"<<endl;
}
}
}
//Addition Function, to insert in mid or start or last
//searching function
void cnode::search()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to search: ";
cin>>num;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//searching function
//upadation Function
void cnode::update()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to update in the list: ";
cin>>num;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
cout<<"Enter new value to update: ";
cin>>num;
current->data=num;
cout<<"\nValue updated Successfully\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//upadation Function
//Deletion Function
void cnode::delet()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to delete from the list: ";
cin>>num;
p=1;
current=head;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
p++;
temp=current;
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
if(p==1)
{
head=current->next;
delete current;
current=head;
}
if(p>1 && p<size)
{
temp->next=current->next;
delete current;
current=temp;
}
if(p==size)
{
tail=temp;
tail->next=head;
delete current;
current=tail;
}
size--;
cout<<"\nNode deleted Successfully\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//Deletion Function
//Display function
void cnode::show()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
current=head;
cout<<"\n**********************\n"<<endl;
cout<<"Total Nodes: "<<size<<"\n"<<endl;
for(int i=1;i<=size;i++)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<"\n\n**********************\n"<<endl;
}
}
//Display function
/*
*/
//Single Circular Linked List
#include<iostream>
using namespace std;
class cnode
{
private:
int data;
cnode *next;
public:
void insert();
void add();
void search();
void update();
void delet();
void show();
};
cnode *head=NULL;
cnode *tail=NULL;
cnode *current=NULL;
cnode *temp=NULL;
int p,num;
int size=0;
int main()
{
cnode c1;
string x;
while(1)
{
cout<<"\n\n1 to insert node"<<endl;
cout<<"\n2 to Add node"<<endl;
cout<<"\n3 to Search in Nodes"<<endl;
cout<<"\n4 to update a value"<<endl;
cout<<"\n5 to delete a node"<<endl;
cout<<"\n6 to display"<<endl;
cout<<"\n7 to exit: ";
cin>>x;
if(x=="1")
{
c1.insert();
}
else if(x=="2")
{
c1.add();
}
else if(x=="3")
{
c1.search();
}
else if(x=="4")
{
c1.update();
}
else if(x=="5")
{
c1.delet();
}
else if(x=="6")
{
c1.show();
}
else if(x=="7")
{
return 0;
}
else
{
cout<<"\nInvalid command\n"<<endl;
}
}
}
//insertion function
void cnode::insert()
{
cout<<"\nEnter value: ";
cin>>num;
temp=new cnode;
temp->data=num;
temp->next=head;
if(head==NULL)
{
head=temp;
tail=temp;
current=temp;
}
else
{
tail->next=temp;
tail=temp;
current=temp;
}
size++;
cout<<"\nNode Created successfully\n"<<endl;
}
//insertion function
//Addition Function, to insert in mid or start or last
void cnode::add()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
cout<<"\n0 to add node in the start"<<endl;
cout<<"OR any other position after which you want to Add the node: ";
cin>>p;
if(p<0 ||p>size)
{
cout<<"\nInvalid Position Entered\n"<<endl;
}
else
{
cout<<"\nEnter value: ";
cin>>num;
temp=new cnode;
temp->data=num;
temp->next=NULL;
if(p==0)
{
temp->next=head;
head=temp;
}
if(p>0 && p<size)
{
current=head;
for(int i=1;i<p;i++)
{
current=current->next;
}
temp->next=current->next;
current->next=temp;
}
if(p==size)
{
current=tail;
current->next=temp;
temp->next=head;
tail=temp;
current=temp;
}
size++;
cout<<"\nNode Added Successfully\n"<<endl;
}
}
}
//Addition Function, to insert in mid or start or last
//searching function
void cnode::search()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to search: ";
cin>>num;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//searching function
//upadation Function
void cnode::update()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to update in the list: ";
cin>>num;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
cout<<"Enter new value to update: ";
cin>>num;
current->data=num;
cout<<"\nValue updated Successfully\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//upadation Function
//Deletion Function
void cnode::delet()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
bool flag=false;
cout<<"\nEnter value to delete from the list: ";
cin>>num;
p=1;
current=head;
for(int i=1;i<=size;i++)
{
if(current->data==num)
{
flag=true;
i=size;
}
else
{
p++;
temp=current;
current=current->next;
}
}
if(flag==true)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue founded: "<<current->data<<"\n"<<endl;
if(p==1)
{
head=current->next;
delete current;
current=head;
}
if(p>1 && p<size)
{
temp->next=current->next;
delete current;
current=temp;
}
if(p==size)
{
tail=temp;
tail->next=head;
delete current;
current=tail;
}
size--;
cout<<"\nNode deleted Successfully\n"<<endl;
}
if(flag==false)
{
cout<<"\n**********************\n"<<endl;
cout<<"\nValue Not Founded\n"<<endl;
cout<<"\n**********************\n"<<endl;
}
}
}
//Deletion Function
//Display function
void cnode::show()
{
if(size==0)
{
cout<<"\nList is Empty\n"<<endl;
}
else
{
current=head;
cout<<"\n**********************\n"<<endl;
cout<<"Total Nodes: "<<size<<"\n"<<endl;
for(int i=1;i<=size;i++)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<"\n\n**********************\n"<<endl;
}
}
//Display function
/*
*/
Comments
Post a Comment