PROGRAM 8
OBJECTIVE
Implementation of queue using array.
THEORY
A queue is a non-primitive linear data structure.
A new element is added at one end called the rear end and the existing element is deleted from
the other end called the front end.
This mechanism is called First-In-First-Out.
A queue can be implemented using array as well as linked list.
Functions on the queue are enqueue, dequeue, peek, IsEmpty, IsFull, display.
PSEUDO CODE
ALGORITHM Enqueue(value)
BEGIN:
Queue[100]
IF rear=size-1 THEN
WRITE (Queue Overflow)
ELSE
IF front=-1 THEN
front=0
rear=rear+1
queue[rear]=value
END;
ALGORITHM Dequeue()
BEGIN:
IF front==-1 THEN
WRITE(Queue Underflow)
ELSE
IF front==rear THEN
front=-1
rear=-1
ELSE
front=front+1
END;
ALGORITHM Print()
BEGIN:
IF front==-1 THEN
WRITE(The Queue is empty)
ELSE
WRITE(The Queue is:)
FOR i=front TO i<=rear DO
WRITE(queue[i])
END;
SOURCE CODE
#include<stdio.h>
#define size 100
int queue[size];
int front = -1, rear = -1;
void enqueue (int value)
if (rear == size - 1)
printf ("Queue Overflow\n");
else
if (front == -1)
front = 0;
queue[++rear] = value;
printf ("%d was enqueued to circular queue\n", value);
void dequeue ()
int variable;
if (front == -1)
printf ("Queue Underflow\n");
else
{
variable = queue[front];
if (front == rear)
front = rear = -1;
else
front++;
printf ("%d was dequeued from circular queue\n", variable);
void print ()
int i;
if (front == -1)
printf ("The queue is empty.\n");
else
printf ("The Queue is: \n");
for (i = front; i <= rear; i++)
printf ("%d\n", queue[i]);
}
}
int main()
dequeue();
enqueue(15);
enqueue(20);
enqueue(25);
enqueue(30);
enqueue(35);
print();
dequeue();
dequeue();
print();
enqueue(40);
enqueue(45);
enqueue(50);
enqueue(55);
print();
return 0;
}
OUTPUT
Queue Underflow
15 was enqueued to circular queue
20 was enqueued to circular queue
25 was enqueued to circular queue
30 was enqueued to circular queue
35 was enqueued to circular queue
The Queue is:
15
20
25
30
35
15 was dequeued from circular queue
20 was dequeued from circular queue
The Queue is:
25
30
35
40 was enqueued to circular queue
45 was enqueued to circular queue
50 was enqueued to circular queue
55 was enqueued to circular queue
The Queue is:
25
30
35
40
45
50
55
TIME COMPLEXITY: O(1)+O(1)+O(n)
SPACE COMPLEXITY: O(1)