IMPLEMENTATION OF SINGLY LINKED LIST using c++ program

#include
#include
#include
#define NULL 0
struct linked_list
{
int number;
linked_list *next;
};
typedef linked_list node;
class linkedlist
{
public:
void create(node *head);
void print(node *head);
node *insert(node *head);
node *del(node *head);
};
void main()
{
clrscr();
int ch;
linkedlist llist;
node *head;
head=new node;
char choice='y';
while(choice=='y')
{
cout<<"\n\nMENU"< cout<<"~~~~";
cout<<"\n1.Create \n2.Insert \n3.Delete \n4.Print \n5.Exit";
cout<<"\n Enter your choice"< cin>>ch;
switch(ch)
{
case 1:llist.create(head);
break;
case 2:head=llist.insert(head);
break;
case 3:head=llist.del(head);
break;
case 4:llist.print(head);
break;
case 5:exit(0);
}
cout<<"Do you want to continue(Y/N)"< cin>>choice;
}
getch();
}
void linkedlist::create(node *list)
{
cout<<"Input a number,enter 0 after last node\n"< cin>>list->number;
if(list->number==0)
{
list->next=NULL;
}
else
{
list->next=new node;
create(list->next);
}
return;
}
void linkedlist :: print(node *list)
{

if(list->next!=NULL)
{
cout<number<<"-->";
if(list->next->next==NULL)
cout<next->number;
print(list->next);
}
return;
}
node *linkedlist::insert(node *head)
{
node *n2;
node *n1;
int key;
int x;
node *find(node *p,int a);
cout<<"Enter value to be inserted"<cin>>x;
cout<<"Enter the key item"<cin>>key;
if(head->number==key)
{
n2=new node;
n2->number=x;
n2->next=head;
head=n2;
}
else
{
n1=find(head,key);
if(n1==NULL)
cout<<"key not found"< else
{
n2=new node;
n2->number=x;
n2->next=n1->next;
n1->next=n2;
}
}
return(head);
}
node *find(node *list,int key)
{
if(list->next->number==key)
return(list);
else
if(list->next->next!=NULL)
find(list->next,key);
else
return(NULL);
}
node *linkedlist::del(node *head)
{
node *n1;
int key;
node *p;
node *find(node *p,int a);
cout<<"Enter the key item"< cin>>key;
if(key==0)
{
cout<<"Invalid key"< return(head);
}
if(head->number==key)
{
p=head->next;
delete(head);
head=p;
}
else
{
n1=find(head,key);
if(n1==NULL)
cout<<"key not found"< else
{
p=n1->next->next;
delete(n1->next);
n1->next=p;
}
}
return(head);
}

Comments