implimentation of circular linked list using c++

#include
#include
#include
# define NULL 0
struct linked_list
{
int number;
linked_list*next;
};
typedef linked_list node;
node*head;
node*tail;
node*p;
int s=0;
class linkedlist
{
public:
void creat();
void print();
void insert();
void del();
};
void main()
{
clrscr();
int ch;
linkedlist llist;
char choice='y';
while(choice=='y')
{
cout<<" \t\t\t Menu"< cout<<" \t\t\t ~~~~"< cout<<"1.Creat"< cout<<"2.Insert"< cout<<"3.Delete"< cout<<"4.Print"< cout<<"5.Exit"< cout<<"Enter your choice"< cin>>ch;
switch(ch)
{
case 1:
llist.creat();
break;
case 2:
llist.insert();
break;
case 3:
llist.del();
break;
case 4:
llist.print();
break;
case 5:
exit(0);
}
cout<<"\nDo you want to continue(y/n)"< cin>>choice;
}
getch();
}
void linkedlist::creat()
{
int f1=0,f2=0;
node*list;
list=new node;
do
{
cout <<"Input a number enter 0 after last node\n";
cin>>list->number;
if(f1==0)
{
head=list;
f1=1;
}
if(list->number==0)
{
list->next=head;
tail=list;
f2=1;
}
else
{
list->next=new node;
list=list->next;
}
}
while(f2==0);
return;
}
void linkedlist::print()
{
node*list;
list=head;
if(s==0)
p=list;
s=1;
cout<<"Address"<<"\t"<<"\t"<<"data"<<"\t"<<"next\n";
cout<<"----------------------------------------------\n";
while(list->next->next!=head)
{
cout< cout<number<<"\t";
list=list->next;
cout< }
cout< cout<number<<"\t";
cout< return;
}
void linkedlist::insert()
{
node*n1;
node*n2;
int key;
int x;
node*find(int a);
cout<<"Enter the value of new iteam"< cin>>x;
cout<<"Enter the key item 0 to insert at last"< cin>>key;
if(head->number==key)
{
n2=new node;
n2->number=x;
n2->next=head;
}
else
if(key==0)
{
n2=new node;
tail->number=x;
tail->next=n2;
n2->number=NULL;
n2->next=head;
tail=n2;
}
else
{
n1=find(key);
if(n1==NULL)
cout<<"Key not found"< else
{
n2=new node;
n2->number=x;
n2->next=n1->next;
n1->next=n2;
}
}
return;
}
node*find(int a)
{
node*list;
list=new node;
list=head;
do
{
if(list->next->number==a)
return(list);
else if (list->next==head)
return(NULL);
else
list=list->next;
}
while(1);
}
void linkedlist::del()
{
node*n1;
int key;
node*p;
node*find(int a);
cout<<"Enter the key item"< cin>>key;
if(key==0)
{
cout<<"Invalied key"< return;
}
if(head->number==key)
{
p=head->next;
delete(head);
head=p;
tail->next=head;
}
else
{
n1=find(key);
if(n1==NULL)
cout<<"Key not found"< else
{
p=n1->next->next;
delete(n1->next);
n1->next=p;
}
}
return;
}


Comments