Lab No.
08
Objectives:
To construct Queue abstract data type using Arrays
Introduction
A Queue is an ordered collection of items from which items may be deleted at one end (called
the front of the queue) and inserted at the other end (the rear of the queue). Queues
remember things in first-in-first-out (FIFO) order. The basic operations in a queue are: Enqueue
- Adds an item to the end of queue. Dequeue - Removes an item from the front. A queue is
implemented using a one dimensional array. FRONT is an integer value, which contains the
array index of the front element of the array. REAR is an integer value, which contains the array
index of the rear element of the array. When an element is deleted from the queue, the value
of HEAD is increased by one, i.e. HEAD = HEAD + 1. When an element is inserted into the queue,
the value of TAIL is increased by one, i.e. TAIL = TAIL + 1.
Figure 1: Representation of a Queue
Algorithm:
1. Initialize an array of size n, and the two variables associated with queue, front and rear to -1.
2. To insert an element into the queue:
a. First check whether the queue is full by the condition rear = max - 1; if it is, then print an
error message and exit.
b. Check whether the queue is initially empty by the condition front = -1 or front > rear: if it is,
then increment front by 1.
c. Increment rear by 1 and insert the element in the rear position.
3. To delete an element from the queue:
a. First check whether the queue is empty by the condition front = -1 or front > rear; if it is, then
print an error message and exit.
b. Increment front by 1 to delete the first inserted element.
4. Display the queue contents by printing the elements from front to rear.
Tasks:
1. Implement a queue using the algorithm provided above.
Code:
Output: