using namespace std;
struct node
{
int data;
struct node *prev,*next;
};
class dlist
{
struct node *start;
public:
dlist()
{
start=NULL;
}
void insert_beg(int);
void insert_end(int);
void insert_after(int,int);
void insert_before(int,int);
void delete_beg();
void delete_end();
void delete_pos(int,int);
void display_forward();
void display_reverse();
};
void dlist::insert_beg(int element)
{
struct node *p,*temp;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
p->next=start;
p->prev=NULL;
start=p;
if(start!=NULL)
start->prev=p;
}
void dlist::insert_end(int element)
{
struct node *p,*temp;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
if(start==NULL)
{
p->next=start;
p->prev=start;
start=p;
}
else
{ temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
p->prev=temp;
temp->next=p;
p->next=NULL;
temp=p;
}
}
void dlist::insert_after(int after,int element)
{
struct node *temp;
if(start!=NULL)
{
temp=start;
while(temp->data!=after&&temp->next!=NULL)
{
temp=temp->next;
}
if(temp->next==NULL&&temp->data==after)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
p->next=temp->next;
p->prev=temp;
temp->next=p;
}
else if(temp->data==after&&temp->next!=NULL)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
p->next=temp->next;
p->prev=temp;
temp->next->prev=p;
temp->next=p;
}
else
{
cout<<"item not found\n";
}
}
else
{
cout<<"list is empty\n";
}
}
void dlist::insert_before(int before,int element)
{
struct node *temp;
if(start!=NULL)
{
temp=start;
while(temp->data!=before&&temp->next!=NULL)
{
temp=temp->next;
}
if(temp->prev==NULL&&temp->data==before)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
p->next=temp;
p->prev=temp->prev;
start=p;
temp->prev=p;
}
else if(temp->data==before)
{
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
p->data=element;
p->next=temp;
p->prev=temp->prev;
temp->prev->next=p;
temp->prev=p;
}
else
{
cout<<"item not found\n";
}
}
else
{
cout<<"list is empty\n";
}
}
void dlist::delete_beg()
{
struct node *temp;
temp=start;
if(start!=NULL)
{
if(temp->next==NULL)
start=temp->next;
else
temp->next->prev=start;
start=temp->next;
free(temp);
}
else
{
cout<<"list is empty\n";
}
}
void dlist::delete_end()
{
struct node *temp;
temp=start;
if(start!=NULL)
{
while(temp->next!=NULL)
{
temp=temp->next;
}
if(start->prev!=NULL)
temp->prev->next=NULL;
else
start=temp->next;
free(temp);
}
else
{
cout<<"list is empty\n";
}
}
void dlist::delete_pos(int,int)
{
}
void dlist::display_forward()
{
struct node *temp;
temp=start;
while(temp!=NULL)
{
cout<<temp->data<<"<--->";
temp=temp->next;
}
}
void dlist::display_reverse()
{
}
int main()
{
dlist obj;
int item,pos,choice;
while(choice!=10)
{
cout<<"Enter your choice\nEnter 1 for display the list forward\nEnter 2 for inserting an item begining\nEnter 3 for insert at end\nEnter 4 for insert after\nEnter 5 for insert before\nEnter 6 for delete frm beg\nEnter 7 for delete at end\nEnter 8 for delete at postiton\nEnter 9 for traverse reverse\nEnter 10 for exit\n";
cin>>choice;
switch(choice)
{
case 1:
obj.display_forward();
break;
case 2:
cout<<"enter the item\n";
cin>>item;
obj.insert_beg(item);
break;
case 3:
cout<<"Enter the item\n";
cin>>item;
obj.insert_end(item);
break;
case 4:
cout<<"Enter the position\n" ;
cin>>pos;
cout<<"Enter the item\n";
cin>>item;
obj.insert_after(pos,item);
break;
case 5:
cout<<"Enter the position\n";
cin>>pos;
cout<<"Enter the item\n";
cin>>item;
obj.insert_before(pos,item);
break;
case 6:
obj.delete_beg();
break;
case 7:
obj.delete_end();
break;
case 8:
cout<<"Enter the position\n";
cin>>pos;
cout<<"Enter the item\n";
cin>>item;
obj.delete_pos(pos,item);
break;
case 9:
obj.display_reverse();
break;
}
}
return 0;
}
Comments