#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int dat;
struct node *p;
};
struct node *first;
void main()
{
int a;
clrscr();
struct node * insf(struct node *);
struct node * insend(struct node *);
struct node * del(struct node*);
struct node * reverse(struct node*);
void trv(struct node *);
again:
cout<<" \n 1. insert a node at first ";
cout<<" \n 2. insert a node at end ";
cout<<" \n 3. traverse ";
cout<<" \n 4. deletion ";
cout<<" \n 5. reverse ";
cout<<" Enter Your Choice ";
cin>>a;
switch(a)
{
case 1: first=insf(first);
break;
case 2: first=insend(first);
break;
case 3: trv(first);
break;
case 4: first=del(first);
break;
case 5: first=reverse(first);
break;
default: exit(0);
}
int n;
cout<<"\n do you want to continue ? y=1 & n=0 ";
cin>>n;
if(n==1)
{
goto again;
}
getch();
}
struct node * insf(struct node *f)
{
struct node * nw;
nw=(struct node *)malloc(sizeof(struct node));
cout<<"\n\n enter the value of new node ";
cin>>nw->dat;
nw->p=f;
return(nw);
}
struct node * insend(struct node *f)
{
struct node *nw,*s;
nw=(struct node *)malloc(sizeof(struct node));
cout<<"\n\n enter the value of new node ";
cin>>nw->dat;
if(f==0)
{
nw->p=0;
return(nw);
}
s=f;
while(s->p!=0)
{
s=s->p;
}
nw->p=0;
s->p=nw;
return(f);
}
void trv(struct node *f)
{
if(f==0)
{
cout<<"list is empty ";
}
else
{
struct node *s;
s=f;
cout<<"\n\n list is :";
cout<<s->dat<<" ";
while(s->p!=0)
{
s=s->p;
cout<<s->dat<<" ";
}
}
}
struct node* del(struct node *f)
{
struct node *temp=f;
if(f==NULL)
{
cout<<"\nlist is empty ";
return(f);
}
else
{
int val;
cout<<" enter the value of node : ";
cin>>val;
if(temp->dat==val)
{
cout<<"\n node is deleted ";
f=f->p;
return(f);
}
if(temp->p==0)
{
cout<<"\n node not found ";
return(f);
}
while((((temp->p)->dat)!=val)&&((temp->p)!=0))
temp=temp->p;
if((temp->p)==0)
{
cout<<" \n node not found";
return(f);
}
else
{
temp->p=(temp->p)->p;
cout<<"\n Node is deleted ";
return(f);
}
}
}
struct node* reverse(struct node *f)
{
struct node *t1,*t2,*t3;
if(f==0)
{
cout<<"link list is empty ";
return(f);
}
else
{
if(f->p==0)
{
return(f);
}
t1=f;
t2=f->p;
t3=t2->p;
t1->p=0;
t2->p=t1;
while(t3!=0)
{
t1=t2;
t2=t3;
t3=t3->p;
t2->p=t1;
}
cout<<"\n now link list is reversed ";
return(t2);
}
}