# Write an algorithm for the implementation of a Dequeue – IGNOU MCA Assignment 2015 – 16

By | September 23, 2015

MASTER OF COMPUTER APPLICATIONS

Course Code : MCS-021
Course Title : Data and File Structures
Assignment Number : MCA(II)/021/Assignment/15-16
Maximum Marks : 100
Weightage : 25%
Write an algorithm for the implementation of a Dequeue .

#include <stdio.h>
#define max 10

int top1, top2, stk_arr[max];

void push();
void pop();
void display();

void main()
{
int ch;
top1=-1,top2=max;
do
{
printf(“\n 1:push\n 2:pop\n 3:display\n 4:exit\n choice:”);
scanf(“%d”, &ch);
switch (ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4:printf(“exiting from program\n”);
break;
default:printf(“wrong choice\n”);
break;
}
}while(ch!=4);
}
void push()
{
int x,ch;
if(top1==top2-1)
{
printf(“stack overflow \n”);
return;
}
printf(“enter a no \n”);
scanf(“%d”,&x);
printf(“\n press 1 to push in stack1 or press 2 for stack2:”);
scanf(“%d”,&ch);
if(ch==1)
stk_arr[++top1]=x;
else
stk_arr[--top2]=x;
printf(“\n %d element is successfully pushed \n”,x);
return;
}

void pop()
{
int y,ch;
printf(“\n press 1 to pop from stack1 or press 2 for stack2″);
scanf(“%d”,&ch);
if(ch==1)
{
if(top1==-1)
{
printf(“stack underflow\n”);
return;
}
y=stk_arr[top1];
stk_arr[top1--]=0;
}
else
{
if(top2==max)
{
printf(“stack underflow\n”);
return;
}
y=stk_arr[top2];
stk_arr[top2++]=0;
}
printf(“\n%d element is successfully poped from stack \n”, y);
return;
}

void display()
{
int i;
if (top1 == -1)
{
printf(“stack 1 is empty \n”);
}
else
{
printf(“elements of Stack 1 are : \n”);
for (i = 0; i <= top1; i++)
{
printf(“%d\n”,stk_arr[i]);
}
}
if (top2 == max)
{
printf(“stack 2 is empty \n”);
}
else
{
printf(“elements of Stack 2 are : \n”);
for (i = max; i >= top2; i–)
{
printf(“%d\n”,stk_arr[i]);
}
}
return ;
}

Program Code:

 Source code
```#include <stdio.h>
#define MAX 10
void insert_front(int *,int *,int *,int);
void insert_back(int *,int *,int *,int);
void delete_front(int *,int *,int *,int *);
void delete_back(int *,int *,int *,int *);
void display(int *,int *,int *);
int c=0;
void main()
{
int que[MAX];
int front,rear;
int value,choice,i;
front = rear = -1;
for(i=0;i<MAX;i++)
que[i]=-1;
START:
do
{
printf("\n\n\tDEQUEUE:\n");
display(que,&front,&rear);
printf("\n______________________________________");
printf("\n 1. insert_front\n");
printf("\n 2. insert_back\n");
printf("\n 3. delete_front\n");
printf("\n 4. delete_back\n");
printf("\n 5. Print elements in DeQue\n");
printf("\n 6. Quit\n");
printf("\n______________________________________");
printf("\n\tENTER CHOICE HERE :");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("Enter the element to be inserted in front\n");
scanf("%d",&value);
insert_front(que,&front,&rear,value);
break;
case 2 : printf("Enter the element to be inserted in end\n");
scanf("%d",&value);
insert_back(que,&front,&rear,value);
break;
case 3 : delete_front(que,&front,&rear,&value);
if(value == -1)
printf("\nDeque is empty\n");
else
printf("The value deleted from front is %d\n",value);
break;
case 4 : delete_back(que,&front,&rear,&value);
if(value == -1)
printf("\nDeque is empty\n");
else
printf("The value deleted from front is %d\n",value);
break;
case 5 : display(que,&front,&rear);
break;
case 6 : printf("\npress any key to QUIT !!!\n");
goto EXIT;
default: goto START;
}
}while(1);
EXIT:
getch();
}
void insert_front(int que[],int *front,int *rear,int value)
{
int i,k;
if(*front == 0 && *rear == MAX-1)
{
printf("\nDequeue is full.\n");
return;
}
if(*front == -1)
{
*rear=*front=0;
que[*front]=value;
c++;
return;
}
if(*rear != MAX-1)
{
printf("shifting");
k=*rear+1;
for(i=1;i<=c;i++)
{
que[k]=que[k-1];
k--;
}
que[k]=value;
*front=k;
(*rear)++;
c++;
}
else
{
(*front)--;
que[*front]=value;
c++;
}
}
void insert_back(int que[],int *front,int *rear,int value)
{
int i,k;
if(*front == 0 && *rear == MAX-1)
{
printf("\nDequeue is full.\n");
return;
}
if(*rear == -1)
{
*rear=*front=0;
que[*rear]=value;
c++;
return;
}
if(*rear == MAX-1)
{
printf("shifting");
k=*front-1;
for(i=1;i<=c;i++)
{
que[k]=que[k+1];
k++;
}
que[k]=value;
*rear=k;
(*front)++;
c++;
}
else
{
(*rear)++;
que[*rear]=value;
c++;
}
}
void delete_front(int que[],int *front,int *rear,int *value)
{
if(*front == -1)
{
printf("\nDeque is empty\n");
*value=-1;
return;
}
*value = que[*front];
que[*front]=-1;
if(*front == *rear)
{
*front=-1;
*rear=-1;
*value=-1;
c--;
}
else
{
(*front)++;
c--;
}
}
void delete_back(int que[],int *front,int *rear,int *value)
{
if(*rear == -1)
{
printf("\nDeque is empty\n");
*value=-1;
return;
}
*value = que[*rear];
que[*rear]=-1;
if(*rear == *front)
{
*front=-1;
*rear=-1;
*value=-1;
c--;
}
else
{
(*rear)--;
c--;
}
}
void display(int *que,int *front,int*rear)
{
int i;
if(*front == -1 || *rear == -1)
printf("\nDequeue is empty\n");
else
{ printf("\nfront->");
for(i=*front;i<=*rear;i++)
printf(" %d",que[i]);
printf(" <-rear");
}
}```

Screen Shots: