Since the stack is list of elements, the queue is also a list of elements. The stack & the queue differ just in the position where the elements may be added or deleted. Similar to other liner data structures, queues can also be implemented by using arrays. Program 1 lists the implementation of a queue by using arrays.
Program: Array implementation of any Queue
include "stdio.h"
define QUEUE_LENGTH 50
struct queue
{ int element[QUEUE_LENGTH];
int front, rear, choice,x,y;
}
struct queue q;
main()
{
int choice,x;
printf ("enter 1 for add and 2 to eliminate element front the queue")
printf("Enter your alternative")
scanf("%d",&choice);
switch (choice)
{
case 1 :
printf ("Enter element to be inserted :");
scanf("%d",&x);
add(&q,x);
break;
case 2 :
delete();
break;
}
}
add(y)
{
++q.rear;
if (q.rear < QUEUE_LENGTH)
q.element[q.rear] = y;
else
printf("Queue overflow")
}
delete()
{
if q.front > q.rear printf("Queue empty");
else{
x = q.element[q.front];
q.front++;
}
retrun x;
}