Write a C Program to Implement Queue Operation Using Array

Aim:

Write a C Program to Implement Queue Operation Using Array.

Theory:

  • The queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both ends. One end is always used to insert data (enqueue) and the other is used to remove data (dequeue). Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

  • A real-world example of queue can be a single-lane one-way road, where the vehicle enters first, exits first. More real-world examples can be seen as queues at the ticket windows and bus-stops.

Queue Representation

As we now understand that in queue, we access both ends for different reasons. The following diagram given below tries to explain queue representation as data structure −

As in stacks, a queue can also be implemented using Arrays, Linked-lists, Pointers and Structures. For the sake of simplicity, we shall implement queues using one-dimensional array.

Basic Operations

Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from the memory. Here we shall try to understand the basic operations associated with queues −

  • enqueue() − add (store) an item to the queue.
  • dequeue() − remove (access) an item from the queue.

Few more functions are required to make the above-mentioned queue operation efficient. These are −

  • peek() − Gets the element at the front of the queue without removing it.
  • isfull() − Checks if the queue is full.
  • isempty() − Checks if the queue is empty.

In queue, we always dequeue (or access) data, pointed by front pointer and while enqueing (or storing) data in the queue we take help of rear pointer.

Let’s first learn about supportive functions of a queue − peek()

This function helps to see the data at the front of the queue. The algorithm of peek() function is as follows −

Algorithm

Begin procedure peek return queue[front] end procedure
Code language: JavaScript (javascript)

Implementation of peek() function in C programming language −

Example

int peek() { return queue[front]; }
Code language: JavaScript (javascript)

isfull()

  • As we are using single dimension array to implement queue, we just check for the rear pointer to reach at MAXSIZE to determine that the queue is full.
  • In case we maintain the queue in a circular linked list, the algorithm will differ.
  • Algorithm of isfull() function −

Enqueue Operation

  • Queues maintain two data pointers, front and rear.
  • Therefore, its operations are comparatively difficult to implement than of stacks.

The following steps should be taken to enqueue (insert) data into a queue −

  • Step 1 − Check if the queue is full.
  • Step 2 − If the queue is full, produce an overflow error and exit.
  • Step 3 − If the queue is not full, increment the rear pointer to point to the next empty space.
  • Step 4 − Add the data element to the queue location, where the rear is pointing.
  • Step 5 − return success.

Sometimes, we also check to see if a queue is initialized or not, to handle any unforeseen situations.

Algorithm for enqueue operation:

procedure enqueue(data) if queue is full return overflow endif rear ← rear + 1 queue[rear] ← data return true end procedure
Code language: PHP (php)

Dequeue Operation

Accessing data from the queue is a process of two tasks − access the data where the front is pointing and remove the data after access. The following steps are taken to perform the dequeue operation −

  • Step 1 − Check if the queue is empty.
  • Step 2 − If the queue is empty, produce an underflow error and exit.
  • Step 3 − If the queue is not empty, access the data where the front is pointing.
  • Step 4 − Increment front pointer to point to the next available data element.
  • Step 5 − Return success.

Algorithm for dequeue operation

Procedure dequeue if queue is empty return underflow end if data = queue[front] front ← front + 1 return true end procedure
Code language: PHP (php)

Algorithm:

  • Step 1 – Include all the header files which are used in the program and define a constant ‘SIZE’ with a specific value.
  • Step 2 – Declare all the user-defined functions which are used in queue implementation.
  • Step 3 – Create a one-dimensional array with the 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 the operations list and make suitable function calls to perform the operation selected by the user on queue.

Program:

#include <stdio.h>
#include<stdlib.h>
#define MAX 6
void insert();
void remov();
void display();
int queue[MAX], rear=-1, front=-1, item;
main()
{
int ch;
do
{
printf("\n\n1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
remov();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid entry. Please try again...\n");
}
} while(ch<=4);
}
void insert()
{
if(rear == MAX-1)
printf("\nQueue is full.");
else
{
printf("\n\nEnter ITEM:");
scanf("%d", &item);
if (rear == -1 && front == -1)
{
rear = 0;
front = 0;
}
else
rear++;
queue[rear] = item;
printf("\n\nItem inserted: %d", item);
}
}
void remov()
{
if(front == -1)
printf("\n\nQueue is empty.");
else
{
item = queue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else
front++;
printf("\n\nItem deleted: %d", item);
}
}
void display()
{
int i;
if(front == -1)
printf("\n\nQueue is empty.");
else

{
printf("\n\n");
for(i=front; i<=rear; i++)
printf( "%d", queue[i]);
}
}

Execution:

Input:
1 22 1 33 1 44 2 3 4

Output:

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

Enter ITEM:

Item inserted: 22

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

Enter ITEM:

Item inserted: 33

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

Enter ITEM:

Item inserted: 44

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

Item deleted: 22

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

3344

1. Insert
2. Delete
3. Display
4. Exit

Enter your choice:

Result:

Thus, Implement queue using array was executed successfully.

Leave a Comment