#include<iostream>
using namespace std;
class structures
{
struct std
{
int rno;
char name[20];
struct std*link;
}*head;
int size;
public:
void getdata();
void display();
void insertbegin();
void insertmiddle();
void insertend();
void delbegin();
void dellast();
void delmiddle();
void search();
structures()
{
head=NULL;
}
};
void structures::getdata()
{
struct std*t;
cout<<"enter size \n";
cin>>size;
for(int i=0;i<size;i++)
{
t=new std;
cout<<"\nenter roll no ";
cin>>t->rno;
cout<<"\nName is ";
cin>>t->name;
if(head==NULL)
{
t->link=NULL;
head=t;
}
else
{
t->link=head;
head=t;
}
}
}
void structures::insertbegin()
{
struct std*t;
t=new std;
cout<<"\n Enter rollno ";
cin>>t->rno;
cout<<"Name is ";
cin>>t->name;
if(head==NULL)
{
head=t;
t->link=NULL;
}
else
{
t->link=head;
head=t;
}
}
void structures::insertmiddle()
{
int no;
struct std*t;
t=new std;
cout<<"\n enter roll no";
cin>>t->rno;
cout<<"\nName ";
cin>>t->name;
cout<<"enter roll no after which the node to be inserted\n";
cin>>no;
std*p;
p=new std;
p=head;
while(p->rno!=no&p!=NULL)
p=p->link;
if(p!=NULL)
{
t->link=p->link;
p->link=t;
}
}
void structures::insertend()
{
struct std*t;
t=new std;
cout<<"\n Enter Roll no ";
cin>>t->rno;
cout<<"\nName is ";
cin>>t->name;
if(head==NULL)
{
head=t;
t->link=NULL;
}
else
{
std*p;
p=new std;
p=head;
while(p->link!=NULL)
p=p->link;
p->link=t;
t->link=NULL;
}
}
void structures::delbegin()
{
struct std*t;
t=new std;
t=head;
head=head->link;
delete(t);
}
void structures::dellast()
{
struct std*p,*t;
p=new std;
t=new std;
p=head;
while(p->link->link!=NULL)
p=p->link;
t=p->link;
p->link=NULL;
delete(t);
}
void structures::delmiddle()
{
int no;
cout<<"enter rno of node to be deleted";
cin>>no;
std*p,*t;
p=head;
while(p->link->link!=NULL&p->link->rno!=no)
p=p->link;
t=p->link;
p->link=t->link;
delete(t);
}
void structures::search()
{
int r;
std*p;
p=new std;
p=head;
cout<<"enter rno of node to search ";
cin>>r;
while(p->rno!=r&p->link!=NULL)
p=p->link;
if(p==NULL)
{
cout<<"\nElement not found \n";
}
else
{
cout<<"node is "<<p->name<<"\t"<<p->rno;
}
}
void structures::display()
{
std*p;
p=new std;
p=head;
cout<<"\nStudent details are\n";
while(p!=NULL)
{
cout<<"\nRoll No is "<<p->rno;
cout<<"\n Name is "<<p->name;
p=p->link;
}
}
int main()
{
structures ob;
ob.getdata();
int option,n=1;
while(n==1)
{
cout<<"\n Enter option";
cout<<"\n1.insert begin\n2.insert middle\n3.insert end\n4.delete begin\n5.delete last\n6.delete middle\n7.search\n8.display\n0.exit program\n";
cin>>option;
switch(option)
{
case 1:
ob.insertbegin();
ob.display();
break;
case 2:
ob.insertmiddle();
ob.display();
break;
case 3:
ob.insertend();
ob.display();
case 4:
ob.delbegin();
ob.display();
case 5:
ob.dellast();
ob.display();
case 6:
ob.delmiddle();
ob.display();
case 7:
ob.search();
case 8:
ob.display();
case 0:
n=0;
break;
}
}
}