Program of Circular linked list using C++

// Circular linked list //

#include"iostream.h"
#include"conio.h"
#define NULL 0
struct list
{
int data;
struct list *next;
};
typedef struct list node;
node *find(node *,int);
node *start;
int main()
{
int menu(void);
void create(node *);
void display(node *);
void insert(node *,int,int);
void del(node *,int);
int choice,ch;
int data,tar;
node *newrec;
node *t;
start=NULL;
do{
clrscr();
choice=menu();
switch(choice)
{
case'1':
cout<<"\nCreating the list";
cout<<"\nEnter the terms(type 0 to end)\n";
start=new node;
start->data=9999;
start->next=new node;
create(start->next);
break;
case'2':
if(start==NULL)
cout<<"\nList does not exist";
else
{
cout<<"\nDisplay the list\n";
display(start->next);
}
getch();
break;
case'3':
if(start==NULL)
{
cout<<"\nList does not exist";
getch();
}
else
{
cout<<"\nEnter the term to insert:";
cin>>data;
cout<<"\nEnter the target term:";
cin>>tar;
insert(start,data,tar);}
break;
case'4':
if(start==NULL)
{
cout<<"\nList does not exist:";
getch();
}
else
{
cout<<"\nEnter the term to delete:";
cin>>data;
del(start,data);
}
break;
case'5':
cout<<"\nExiting";
break;
default:
cout<<"\nNot valid choice";
getch();
break;
}
}while(choice!=5);
getch();
return(0);
}
int menu(void)
{
int ch;
cout<<"\n 1->Creation of the list";
cout<<"\n 2->Displaying the list";
cout<<"\n 3->Creation in the list";
cout<<"\n 4->Deletion in the list";
cout<<"\n 5->Exit";
cout<<"\nEnter your choice:";
cin>>ch;
return(ch);
}
void create(node *record)
{
cin>>record->data;
if(record->data==0)
record->next=start;
else
{
record->next=new node;
create(record->next);
}
return;
}
void display(node *record)
{
if(record->next!=start)
{
cout<data<display(record->next);
}
return;
}
void insert(node *record,int data,int target)
{
node *tag,*newrec;
newrec=new node;
tag=new node;
if(record->data==target)
{
newrec->next=start->next;
start->next=newrec;
}
else
{
tag=find(record,target);
newrec->next=tag->next;
tag->next=newrec;
}
if(tag==NULL)
{
cout<<"Target item not present in the list\n";
getch();
return;
}
newrec->data=data;
return;
}
void del(node *record,int target)
{
node *tag,*temp;
temp=new node;
tag=new node;
record=record->next;
if(record->data==target)
{
temp=record;
start->next=temp->next;
}
else
{
tag=find(record,target);
temp=tag->next;
tag->next=temp->next;
}
if(tag==NULL)
{
cout<<"Target item not present in the list\n";
getch();
return;
}
return;
}
node *find(node *record,int target)
{
if(record->next->data==target)
{
return(record);
}
else if(record->next==start)
return(NULL);
else
find(record->next,target);
}

Post a Comment

Previous Post Next Post