a double-ended queue is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).
#include<stdio.h>
#include<stdlib.h>
typedef struct nodetype{
int info;
struct nodetype *next;
}node;
node *front=NULL,*rear=NULL;
void display(){
if(front==NULL){
printf("NO element is present");
return;
}
node *p=front;
while(p!=NULL){
printf("-->%d\n",p->info);
p=p->next;
}
return;
}
void insert(ele){
node *temp=(node*)malloc(sizeof(node));
if(temp==NULL){
printf("OVERFLOW");
return;
}
temp->info=ele;
temp->next=NULL;
if(front==NULL)
front=rear=temp;
else{
rear->next=temp;
rear=temp;
}
display();
}
void insert_front(ele){
node *temp=(node*)malloc(sizeof(node));
if(temp==NULL){
printf("OVERFLOW");
return;
}
temp->info=ele;
if(front==NULL){
front=rear=temp;
temp->next=NULL;
}
else{
temp->next=front;
front=temp;
}
display();
}
delete(){
if(front==NULL){
printf("UNDERFLOW");
return;
}
int p=front->info;
if(front->next==NULL)
front=rear=NULL;
else
front=front->next;
printf("The element deleted is :%d\n",p);
display();
return;
}
delete_rear(){
if(front==NULL){
printf("UNDERFLOW");
return;
}
int pq=rear->info;
node *p=front;
if(front==rear){
front=rear=NULL;
return;
}
node *q;
while(p->next!=NULL){
q=p;
p=p->next;
}
q->next=NULL;
rear=q;
printf("The element deleted is :%d\n",pq);
display();
return;
}
main(){
char ch='y';
int c,ele,k;
do{
printf("1.Insert\n2.Display\n3.Delete\n4.Insert at Front\n5.Delete at
rear\nChoice:");
scanf("%d",&c);
switch(c){
case 1:printf("Enter the element :");
scanf("%d",&ele);
insert(ele);
break;
case 2:display();
break;
case 3:delete();
break;
case 4:printf("Enter the element :");
scanf("%d",&ele);
insert_front(ele);
break;
case 5:delete_rear();
break;
}
printf("Press Y to continue :");
ch=getchar();
ch=getchar();
}
while(ch=='y'||ch=='Y');
}
No comments:
Post a Comment