0% found this document useful (0 votes)
98 views

Experiment No.: 4 Implement Linear Queue ADT Using Array

The document describes implementing a linear queue using an array data structure. It defines queue operations like enqueue, dequeue, and display. Enqueue adds an element to the rear of the queue if not full. Dequeue removes an element from the front if not empty. Display prints all elements from front to rear if the queue is not empty. The implementation creates an array of a fixed size, and uses front and rear pointers to track the first and last elements.

Uploaded by

Harsh Alashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Experiment No.: 4 Implement Linear Queue ADT Using Array

The document describes implementing a linear queue using an array data structure. It defines queue operations like enqueue, dequeue, and display. Enqueue adds an element to the rear of the queue if not full. Dequeue removes an element from the front if not empty. Display prints all elements from front to rear if the queue is not empty. The implementation creates an array of a fixed size, and uses front and rear pointers to track the first and last elements.

Uploaded by

Harsh Alashi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

EXPERIMENT NO.

: 4
Implement Linear Queue ADT using array.

NAME: HARSH KISHOR ALASHI

CLASS: SE COMPS

DIVISION: A

BATCH: A1

ROLL NO.: CEA304


AIM: To implement Linear Queue ADT using array.

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'.

Now, some of the implementation of queue operations are as follows:

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.

Queue Operations using Array

Queue data structure using arrays can be implemented as follows...


Before we implement actual operations, first follow the below steps to create an empty
queue.

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.

enQueue(value) - Inserting value into the 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 1 - Check whether queue is FULL. (rear == SIZE-1)

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 1 - Check whether queue is EMPTY. (front == rear)

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).

display() - Displays the elements of a Queue

We can use the following steps to display the elements of a queue...

Step 1 - Check whether queue is EMPTY. (front == rear)

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:

//CEA304_HARSH KISHOR ALASHI

//EXPERIMENT-4

#include <stdio.h>

#include <stdlib.h>

#define QUEUESIZE 20

typedef struct queue

int item[QUEUESIZE], front, rear;

} queue;

queue createqueue()

queue q;

q.front = 0;

q.rear = -1;

return q;

int isfull(queue *q)

if (q->front == (QUEUESIZE - 1))

return 1;

else

return 0;
}

int isempty(queue *q)

if ((q->rear) < (q->front))

return 1;

else

return 0;

void enqueue(queue *q, int n)

q->item[++q->rear] = n;

int dequeue(queue *q)

return q->item[q->front++];

int peek(queue *q)

return q->item[q->front];

}
OUTPUT:
CONCLUSION:

Thus, we have successfully Implemented Linear Queue ADT using array.

You might also like