Experiment No.: 4 Implement Linear Queue ADT Using Array
Experiment No.: 4 Implement Linear Queue ADT Using Array
: 4
Implement Linear Queue ADT using array.
CLASS: SE COMPS
DIVISION: A
BATCH: A1
THEORY:
A queue data structure can be implemented using a one dimensional array. The queue
implemented using arrays stores only a fixed number of data values. The implementation of
queue data structure using arrays is very simple. Just define a one dimensional array of specific
size and insert or delete the values into that array by using FIFO (First In First Out) principle
with the help of variables 'front' and 'rear'.
Enqueue: Addition of an element to the queue. Adding an element will be performed after
checking whether the queue is full or not. If rear < n which indicates that the array is not full
then store the element at arr[rear] and increment rear by 1 but if rear == n then it is said to be
an Overflow condition as the array is full.
Dequeue: Removal of an element from the queue. An element can only be deleted when there
is at least an element to delete i.e. rear > 0. Now, elements at arr[front] can be deleted but all
the remaining elements have to be shifted to the left by one position in order for the dequeue
operation to delete the second element from the left on another dequeue operation.
Front: Get the front element from the queue i.e. arr[front] if the queue is not empty.
Display: Print all elements of the queue. If the queue is non-empty, traverse and print all the
elements from index front to rear.
Initially both 'front' and 'rear' are set to -1. Whenever, we want to insert a new value into the
queue, increment the 'rear' value by one and then insert at that position. Whenever we want
to delete a value from the queue, then delete the element which is at 'front' position and
increment 'front' value by one.
Step 1 - Include all the header files which are used in the program and define a constant
'SIZE' with specific value.
Step 2 - Declare all the user defined functions which are used in queue implementation.
Step 3 - Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4 - Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int
front = -1, rear = -1)
Step 5 - Then implement the main method by displaying a menu of operations list and
make suitable function calls to perform operations selected by the user on queue.
In a queue data structure, enQueue() is a function used to insert a new element into the
queue. In a queue, the new element is always inserted at the rear position. The enQueue()
function takes one integer value as a parameter and inserts that value into the queue. We can
use the following steps to insert an element into the queue...
Step 2 - If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and
terminate the function.
Step 3 - If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear]
= value.
deQueue() - Deleting a value from the Queue
In a queue data structure, deQueue() is a function used to delete an element from the queue.
In a queue, the element is always deleted from front position. The deQueue() function does
not take any value as parameter. We can use the following steps to delete an element from
the queue...
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 3 - If it is NOT EMPTY, then increment the front value by one (front ++). Then
display queue[front] as deleted element. Then check whether both front and rear are
equal (front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
Step 2 - If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3 - If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front+1'.
Step 4 - Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same
until 'i' value reaches to rear (i <= rear)
PROGRAM:
//EXPERIMENT-4
#include <stdio.h>
#include <stdlib.h>
#define QUEUESIZE 20
} queue;
queue createqueue()
queue q;
q.front = 0;
q.rear = -1;
return q;
return 1;
else
return 0;
}
return 1;
else
return 0;
q->item[++q->rear] = n;
return q->item[q->front++];
return q->item[q->front];
}
OUTPUT:
CONCLUSION: