Sie sind auf Seite 1von 6

#include<iostream>

using namespace std;


struct node
{
int roll_no;
char name[20];
struct node *next;
struct node *prev;
};
struct node *head=NULL;

void read(struct node* temp)


{
cin>>temp->roll_no;
gets(temp->name);

}
void print_node(struct node* temp)
{
cout<<temp->roll_no;
puts(temp->name);

}
struct node *search_last()
{
struct node *temp,*prevtemp,*first;
first = head;
temp=head;
do
{
prevtemp=temp;
temp=temp->next;
}while(temp->next!=first);
return temp;
}
struct node *search_sl()
{
struct node *temp,*prevtemp,*first;
first = head;
temp=head;
do
{
prevtemp=temp;
temp=temp->next;
}while(temp->next!=first);
return prevtemp;
}

struct node *search_by_roll(int tar)


{
struct node *temp,*first;
temp=head;
first=head;
do
{
if(temp->roll_no==tar)
{
return temp;
}

temp=temp->next;
}while(temp!=first);
return NULL;
}

void insert_at_head(struct node *cur)


{
if(head==NULL)
{
head=cur;
cur->next=cur;
cur->prev=cur;
}
else
{
struct node *first,*last;
first=head;
last=search_last();
head=cur;
cur->next=first;
first->prev=cur;
cur->prev=last;
last->next=cur;
}

}
void insert_at_end(struct node *cur)
{
if(head==NULL)
{
head=cur;
cur->next=cur;
cur->prev=cur;
}
else
{
struct node *last,*first;
first=head;
last=search_last();
last->next=cur;
cur->prev=last;
cur->next=first;
first->prev=cur;
}
}
void insert_after_roll_number(struct node *cur,int target)
{
if(head==NULL)
{
head=cur;
cur->next=cur;
cur->prev=cur;

}
else
{
struct node *s,*sn;
s=search_by_roll(target);
sn=s->next;
s->next=cur;
cur->next=sn;
cur->prev=s;
sn->prev=cur;

}
}
struct node* delete_at_head()
{
struct node *first;
first=head;
if(head==NULL)
{
return NULL;
}
else if(first->next==first)
{
head=NULL;
return first;
}
else
{
struct node *sec,*last;
last=search_last();
sec=first->next;
head=sec;
sec->prev=last;
last->next=sec;
return first;

}
struct node* delete_at_end()
{
struct node *first;
first=head;
if(head==NULL)
{
return NULL;
}
else if(first->next==first)
{
head=NULL;
return first;
}
else
{
struct node *sl,*l,*first;
first=head;
sl=search_sl();
l=sl->next;
sl->next=first;
first->prev=sl;
return l;
}

}
struct node* delete_by_roll_number(int target)
{
struct node *first;
first=head;
if(head==NULL)
{
return NULL;
}
else if(first->next==first)
{
head=NULL;
return first;
}
else
{
struct node *sp,*s,*sn;
s=search_by_roll(target);
sp=s->prev;
sn=s->next;

sp->next=sn;
sn->prev=sp;
return s;
}

}
void display_all( )
{
struct node * temp;
temp = head;
do
{
if(head == NULL)
{
cout<<"the list is empty"<<endl<<endl;
}
else
{
print_node(temp);
temp = temp->next;
}

}while(temp!=head);
}
void main()
{
struct node *cur;
int target;
int choice;
while(1)
{
cout<<"1. insert at Head"<<endl;
cout<<"2. insert at end"<<endl;
cout<<"3. insert after Roll Number"<<endl;
cout<<"4.Delete at Head"<<endl;
cout<<"5.Delete at end"<<endl;
cout<<"6.Delete by Roll Number"<<endl;
cout<<"9.Displayall"<<endl;
cout<<"10.exit"<<endl;

cout<<" \t Enter Your Choice : "<<endl;


cin>>choice;
if(choice==1)
{
cout<<"Enter Data :"<<endl;
cur = new node();
read(cur);
insert_at_head(cur);
}
else if(choice==2)
{
cout<<"Enter Data :"<<endl;
cur = new node();
read(cur);
insert_at_end(cur);

}
else if(choice==3)
{
cur = new node();
cout<<"Enter New Roll No : "<<endl;
read(cur);
cout<<"Enter Roll number After which you want to Add The Roll No. ";
cin>>target;
insert_after_roll_number(cur,target);

}
else if(choice==4)
{
struct node *temp;
temp=delete_at_head();
print_node(temp);
}
else if(choice==5)
{
struct node *temp;
temp=delete_at_end();
print_node(temp);

}
else if(choice==6)
{
cin>>target;

struct node *temp;


temp=delete_by_roll_number(target);
print_node(temp);

}
else if(choice==9)
{
display_all();
}
else if(choice==10)
{
break;
}

}
}

Das könnte Ihnen auch gefallen