Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday 15 January 2018

DEQUEUE implementation with array

#include<stdio.h>
#include<stdlib.h>
#define MAX 10
typedef struct DQueue
{
int A[MAX];
int front;
int rear;
}qp;
qp *ps;
int isEmpty()
{
if(ps->rear==-1)
return 1;
return 0;
}
int isFull()
{
if(ps->front==(ps->rear+1)%MAX)
return 1;
return 0;
}
void pushF()
{
if(isFull())
printf("Dqueue is Full.......\n\n");
else
{
int ele;
printf("Enter the Element......\n");
scanf("%d",&ele);
if(ps->front==-1)
{
ps->rear=0;
ps->front=0;
ps->A[ps->front]=ele;
}
else{
ps->front=(ps->front-1+MAX)%MAX;
ps->A[ps->front]=ele;
}
}
}
void pushL()
{
if(isFull())
printf("Dqueue is Full.......\n\n");
else
{
int ele;
printf("ENTER THE ELEMENT......\n");
scanf("%d",&ele);
if(ps->front==-1)
{
ps->rear=0;
ps->front=0;
ps->A[ps->front]=ele;
return;
}
ps->rear=(ps->rear+1+MAX)%MAX;
ps->A[ps->rear]=ele;
}
}
void popF()
{
if(isEmpty())
printf("DEQUEUE IS OVERFLOW.....\n");
else if(ps->front==ps->rear)
{
printf("DELETED ELEMENT IS -> %d\n",ps->A[ps->front]);
ps->front=ps->rear=-1;
}
else
{
printf("DELETED ELEMENT IS -> %d\n",ps->A[ps->front]);
ps->front=(ps->front+1+MAX)%MAX;
}
}
void popL()
{
if(isEmpty())
printf("DEQUEUE IS UNDERFLOW.....\n");
else if(ps->front==ps->rear)
{
printf("DELETED ELEMENT IS -> %d\n",ps->A[ps->front]);
ps->front=ps->rear=-1;
}
else
{
printf("DELETED ELEMENT IS -> %d\n",ps->A[ps->rear]);
ps->rear=(ps->rear-1+MAX)%MAX;
}
}
void Display()
{
if(isEmpty())
printf("DEQUEUE IS EMPTY..\n");
else if(ps->front>ps->rear)
{
int i;
for(i=ps->front;i<MAX;i++)
printf("%d-> ",ps->A[i]);
for(i=0;i<=ps->rear;i++)
printf("%d-> ",ps->A[i]);
}
else
{
int i;
for(i=ps->front;i<=ps->rear;i++)
printf("%d-> ",ps->A[i]);
}
}
int main()
{
ps=(qp *)malloc(sizeof(qp));
ps->front=-1,ps->rear=-1;
char ch='Y';
int n;
while(ch=='Y' || ch=='y')
{
printf("MENU IS.....\n\n1. PushF\n2. PushL\n3. PopF\n4. PopL\n5. Display\n\n");
printf("Enter your choice....\n");
scanf("%d",&n);
switch(n)
{
case 1: pushF();break;
case 2: pushL();break;
case 3: popF();break;
case 4: popL();break;
case 5: Display();
printf("\n");
break;
default: printf("Wrong Choice...........\n\n");
}
printf("Press Y to continue............\n");
scanf("%c",&ch);
scanf("%c",&ch);
}
}

No comments:

Post a Comment