#include<iostream.h>
#include<conio.h>
class list
{
struct node
{
int info;
node *next;
node *pre;
}*head;
public:
list()
{
head=NULL;
}
void append(int data)
{
node *temp,*temp1;
if(head==NULL)
{
temp=new node;
temp->info=data;
temp->next=NULL;
temp->pre=NULL;
head=temp;
}
else
{
temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp1=new node;
temp1->info=data;
temp1->next=NULL;
temp1->pre=temp;
temp->next=temp1;
}
}
void beg_insert(int data)
{
node *temp;
temp=new node;
temp->info=data;
temp->next=head;
temp->pre=NULL;
temp->next->pre=temp;
head=temp;
}
void in_insert(int pos,int data)
{
node *temp,*t;
int a;
a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && temp==NULL)
append(data);
else if(temp==NULL)
cout<<"\n\tWrong position u given";
else
{
t=new node;
t->info=data;
t->next=temp->next;
t->pre=temp;
temp->next->pre=t;
temp->next=t;
}
}
void del( )
{
node *temp,*t;
temp=head;
if(temp==NULL)
cout<<"\n \tList not Exists ";
else if(temp->next==NULL)
{
delete temp;
head=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
t=temp;
temp->pre->next=NULL;
delete t;
}
}
void beg_del()
{
node *temp;
temp=head;
if(head==NULL)
cout<<"\n\t Link list has np nodes";
else
{
head=temp->next;
temp->next->pre=NULL;
delete temp;
}
}
void mid_del(int pos)
{
node *temp,*t;
int a=pos;
temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
if(a==1 && head->next==NULL)
del();
else if(a==1 && head->pre==NULL)
beg_del();
else if(temp==NULL)
cout<<"\n\t List not Exist ..";
else
{
t=temp;
temp->pre->next=t->next;
temp->next->pre=t->pre;
delete t;
}
}
void show()
{
node *temp;
temp=head;
cout<<"\n\t";
if(temp==NULL)
cout<<"\n \tList not exist\n";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->next;
}
temp=head;
while(temp->next!=NULL)
temp=temp->next;
cout<<"\nRevesrse ";
while(temp!=NULL)
{
cout<<" - "<<temp->info;
temp=temp->pre;
}
}
};
void main()
{
list l;
clrscr();
int ch,pos;
while(1)
{
cout<<"\n\t\tENter your Choice : ";
cout<<"\n 1: To append\n 2: To insert at Begining\n 3: To insert at any position";
cout<<"\n 4: To delete\n 5: To del at begining\n 6: To Delete at any position ";
cout<<"\n 7: To show\n 8: To exit ";
cin>>ch;
if(ch==1)
{
cout<<"\n\t Enter data to append : ";
cin>>ch;
l.append(ch);
}
else if(ch==2)
{
cout<<"\n\t Enter element to insert at begining : " ;
cin>>ch;
l.beg_insert(ch);
}
else if(ch==3)
{
cout<<"\nEnter element to insert at specific position : ";
cin>>ch;
cout<<"\n Enter position : ";
cin>>pos;
l.in_insert(pos,ch);
}
else if(ch==4)
l.del();
else if (ch==5)
l.beg_del();
else if(ch==6)
{
cout<<"\n Enter position where u have to delete ";
cin>>pos;
l.mid_del(pos);
}
else if(ch==7)
l.show();
else if(ch==8)
{
cout<<"\n Press Enter to Exit";
break;
}
else
cout<<"\n\t Wrong Choice ";
}
getch();
}
No comments:
Post a Comment