Complete Circular Singly Linked List C++
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node * next;
};
struct node *head, *newnode, *tail, *temp, *stemp;
int count=0,f=1; // f==1 means that list is not created or became empty
void create()
{
head =0;
tail=0;
int choice=1;
while(choice==1)
{
::count++;
newnode=(struct node*)malloc(sizeof(struct node));
cout<<"enter data";
cin>>newnode->data;
if(head==0)
{
head=newnode;
tail=newnode;
newnode->next=newnode;
}
else
{
tail->next=newnode;
tail= newnode;
tail->next=head;
}
cout<<"want another node (0,1): ";
cin>>choice;
}
}
void insrt()
{
int pos,i;
::count++;
cout<<"\nenter the position of insertion";
cin>>pos;
newnode=(struct node*)malloc(sizeof(struct node));
cout<<"enter data";
cin>>newnode->data;
//forming link
if(pos==1)//at beginning
{
newnode->next=head;
tail->next=newnode;
head=newnode;
}
else if(pos==::count)//at end
{
newnode->next=tail->next;
tail->next=newnode;
tail=newnode;
}
else //at some position in middle
{
i=1;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
void delpos(int pos)
{
int i;
temp=head;
if(temp->next==tail->next) // if only one node is present in list.
{
cout<<"this was the last node of the list";
tail=0;
head=0;
free(temp);
}
else
{
if(pos==1)
{
temp=head;
head=temp->next;
free(temp);
}
else if(pos==::count)
{
temp=head;
while(temp!=tail)
{
stemp=temp;
temp=temp->next;
}
stemp->next=tail->next;
tail=stemp;
free(temp);
}
else
{
i=0;
temp=head;
while(i<pos-1)
{
stemp=temp;
temp=temp->next;
i++;
}
stemp->next=temp->next;
free(temp);
}
}
}
void print()
{
temp=head;
cout<<"printing the list of size : "<<::count<<endl;
while(temp!=tail)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data; // this is to print the last node;
}
int main()
{
int choice,conti=1,pos;
::f=1;
while(conti==1)
{
cout<<"Press 1. to create list";
cout<<"\nPress 2. to insert in list";
cout<<"\nPress 3. to delete in list";
cout<<"\nPress 4. to print list";
cout<<"\nEnter your choice: ";
;cin>>choice;
if(choice==1)
{
if(::f==1)
{create();::f=0;}
else
cout<<"list already created";
}
else if(::f==1)
cout<<"list is empty, create list first";
else
{
if(choice==2)
insrt();
else if(choice==3)
{
cout<<"enter the position of deletion: ";
cin>>pos;
if(pos>::count)
cout<<"invalid position";
else{
delpos(pos);
::count-=1;
if(::count==0)
::f=1;
}
}
else if(choice==4)
print();
else
cout<<"invalid choice";
}
cout<<"\nDo you want to continue(0,1): ";
cin>>conti;
}
}
using namespace std;
struct node
{
int data;
struct node * next;
};
struct node *head, *newnode, *tail, *temp, *stemp;
int count=0,f=1; // f==1 means that list is not created or became empty
void create()
{
head =0;
tail=0;
int choice=1;
while(choice==1)
{
::count++;
newnode=(struct node*)malloc(sizeof(struct node));
cout<<"enter data";
cin>>newnode->data;
if(head==0)
{
head=newnode;
tail=newnode;
newnode->next=newnode;
}
else
{
tail->next=newnode;
tail= newnode;
tail->next=head;
}
cout<<"want another node (0,1): ";
cin>>choice;
}
}
void insrt()
{
int pos,i;
::count++;
cout<<"\nenter the position of insertion";
cin>>pos;
newnode=(struct node*)malloc(sizeof(struct node));
cout<<"enter data";
cin>>newnode->data;
//forming link
if(pos==1)//at beginning
{
newnode->next=head;
tail->next=newnode;
head=newnode;
}
else if(pos==::count)//at end
{
newnode->next=tail->next;
tail->next=newnode;
tail=newnode;
}
else //at some position in middle
{
i=1;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->next=temp->next;
temp->next=newnode;
}
}
void delpos(int pos)
{
int i;
temp=head;
if(temp->next==tail->next) // if only one node is present in list.
{
cout<<"this was the last node of the list";
tail=0;
head=0;
free(temp);
}
else
{
if(pos==1)
{
temp=head;
head=temp->next;
free(temp);
}
else if(pos==::count)
{
temp=head;
while(temp!=tail)
{
stemp=temp;
temp=temp->next;
}
stemp->next=tail->next;
tail=stemp;
free(temp);
}
else
{
i=0;
temp=head;
while(i<pos-1)
{
stemp=temp;
temp=temp->next;
i++;
}
stemp->next=temp->next;
free(temp);
}
}
}
void print()
{
temp=head;
cout<<"printing the list of size : "<<::count<<endl;
while(temp!=tail)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data; // this is to print the last node;
}
int main()
{
int choice,conti=1,pos;
::f=1;
while(conti==1)
{
cout<<"Press 1. to create list";
cout<<"\nPress 2. to insert in list";
cout<<"\nPress 3. to delete in list";
cout<<"\nPress 4. to print list";
cout<<"\nEnter your choice: ";
;cin>>choice;
if(choice==1)
{
if(::f==1)
{create();::f=0;}
else
cout<<"list already created";
}
else if(::f==1)
cout<<"list is empty, create list first";
else
{
if(choice==2)
insrt();
else if(choice==3)
{
cout<<"enter the position of deletion: ";
cin>>pos;
if(pos>::count)
cout<<"invalid position";
else{
delpos(pos);
::count-=1;
if(::count==0)
::f=1;
}
}
else if(choice==4)
print();
else
cout<<"invalid choice";
}
cout<<"\nDo you want to continue(0,1): ";
cin>>conti;
}
}
Comments
Post a Comment