Solution using C Language:
#include<stdio.h>
#include<stdlib.h>
struct dnode
{
int info;
struct dnode*prev;
struct dnode*next;
};
void insert_front(struct dnode **p,int item)
{
struct dnode *temp;
temp=(struct dnode*)malloc(sizeof(struct dnode));
temp->prev=NULL;
temp->info=item;
temp->next=NULL;
struct dnode *q;
q=*p;
if(q==NULL)
{
*p=temp;
}
else
{
temp->next=q;
q->prev=temp;
*p=temp;
}
}
void delete_front(struct dnode **p)
{
struct dnode *q;
q=*p;
if(q==NULL)
{
printf("No nodes to delete\n");
exit(0);
}
else if(q->next==NULL)
{
free(q);
*p=NULL;
}
else
{
struct dnode *r;
r=q;
r=r->next;
r->prev=NULL;
q->next=NULL;
free(q);
*p=r;
}
}
void display(struct dnode *p)
{
while(p!=NULL)
{
printf("%d ",p->info);
p=p->next;
}
}
int main()
{
int ch;
struct dnode *first=NULL;
int item;
do
{
printf("1.Insert_front 2.Delete _front 3.Display 4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:{
printf("Enter the item to be inserted\n");
scanf("%d",&item);
insert_front(&first,item);
}
break;
case 2:{
delete_front(&first);
}
break;
case 3:{
display(first);
}
break;
case 4:exit(0);
}
}
while(ch);
return 0;
}
Output:
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
1
Enter the item to be inserted
5
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
3
5 1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
2
1.Insert_front 2.Delete _front 3.Display 4.Exit
Enter your choice
4
I was very pleased to find this web-site.I wanted to thank you for your time for this wonderful read!!! I definitely enjoy every little bit of it and I have you bookmarked to check out new stuff you blog post.
ReplyDeleteRelax guys, I recommend you 3 addictive games: Dino Hunter: Killing Strand action game or Turbo Drift racing game and Idle Craft 3D funny game
Visit my blog spot: Zoxy game blog | Y8 blog game for free | Friv blog hot game