Google

Program to implement a Queue using Array by incrementing front as well as rear

Written on:October 2, 2012
Comments are closed

Queues

Program to implement a Queue using Array by incrementing front as well as rear

/*Implementation of Linear queues using arrays*/
/*Queue implementation by shifting front as well as rear*/

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

#define MAX 4

typedef struct queue
{int front, rear;
int arr[MAX];
}queue;

/*Enqueue will increment rear by one and add new element
at this subscript*/

void enqueue(queue *q, int x)
{if(q->rear==MAX-1)
{printf(“nQueue Overflow!”);
exit(1);
}
q->arr[++q->rear]=x;
}
/*Dequeue operation increments front by 1.
Linear queues are not considered efficient since
such an approach may result in an overflow, even if
empty blocks are available at beginning. To remove
this deficiency circular queues may be considered*/

int dequeue(queue *q)
{
int i,no;
if((q->rear==-1)&&(q->front>q->rear))
{printf(“nQueue Underflow!”);
exit(1);
}
return (q->arr[q->front++]);
}

void display(queue *q)
{int i;
for(i=q->front;i<=q->rear;i++)
printf(“%dt”,q->arr[i]);
}

void main()
{
int ch,num;
queue q;
q.front=0;
q.rear=-1;
while(1)
{
clrscr();
printf(“nMenu”);
printf(“nn1. Enqueue”);
printf(“n2. Dequeue”);
printf(“n3. Display”);
printf(“n4. Exit”);
printf(“nnEnter your choice?”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“nEnter number?”);
scanf(“%d”,&num);
enqueue(&q,num);
break;
case 2:
num=dequeue(&q);
printf(“nElement removed from the queue is %d”,num);
break;
case 3:
display(&q);
break;
case 4:
exit(0);
default:
printf(“nInvalid choice!”);
}
getch();
}
}

 

Sorry, the comment form is closed at this time.