Queue : Data Structure and Algorithm

                                                        Unit 3: Queue

 

Table of Content : 


2.1    Definition
2.1    Queue as Abstract Data Type (ADT)
2.3    Primitive operations in Queue: Enqueue and Dequque
2.4    Linear queue, circular queue, priority queue.

Lab: Write a program to implement linear and circular queue operations.

Definition:

The queue is an abstract data structure that is somehow similar to stacks. Unlike stacks, a queue is open at both its 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.

queue-example

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

Representation of Queue:

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

queue-diagram


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 a one-dimensional array.

Basic Operations in Queue:

Queue operations may involve initialing 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 a queue, we always dequeue (or access) data, pointed by a front pointer, and while enquiring (or storing) data in the queue we take the help of the 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() is as follows:-

Algorithm
begin procedure peek
   return queue[front]
end procedure
Implementation of peek() function in C programming language:-

Example

int peek() {
   return queue[front];
}
isfull()

As we are using dimension array to implement a 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 is full() function -

Algorithm

begin procedure isfull

   if rear equals to MAXSIZE
      return true
   else
      return false
   endif
   
end procedure
Implementation of isfull() function in C  programming language - 

Example

bool isfull() {
   if(rear == MAXSIZE - 1)
      return true;
   else
      return false;
}

isempty()

Algorithm of isempty() function-

Algorithm

begin procedure isempty

   if front is less than MIN  OR front is greater than rear
      return true
   else
      return false
   endif
   
end procedure

If the value of the front is less than MIN or 0, it tells that the queue is not yet initialized, hence empty.

Here's the C programming code-

Example

bool isempty() {
   if(front < 0 || front > rear) 
      return true;
   else
      return false;
}

Primitive Operations in Queue:

Enqueue Operation:

Queue maintains two data pointers, front, and rear. Therefore, its operations are comparatively difficult to implement than that 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 a data element to the queue location, where the rear is pointing.
  • Step 5 - return success.
queue-enqueue diagram


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
Implementation of enqueue() in C programming language-

Example

int enqueue(int data)      
   if(isfull())
      return 0;
   
   rear = rear + 1;
   queue[rear] = data;
   
   return 1;
end procedure

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 a dequeue operation - 

  • Step 1 - check if the queue is empty
  • Step 2 - If the queue is empty, produce an overflow 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.
queue-dequeue diagram

Algorithm for dequeue operation

procedure dequeue
   
   if queue is empty
      return underflow
   end if

   data = queue[front]
   front  front + 1
   return true

end procedure
Implementation of dequeue() in C programming language-

Example

int dequeue() {
   if(isempty())
      return 0;

   int data = queue[front];
   front = front + 1;

   return data;
}

Program to implement Queue Operations: 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

#define MAX 6

int intArray[MAX];
int front = 0;
int rear = -1;
int itemCount = 0;

int peek() {
   return intArray[front];
}

bool isEmpty() {
   return itemCount == 0;
}

bool isFull() {
   return itemCount == MAX;
}

int size() {
   return itemCount;
}  

void insert(int data) {

   if(!isFull()) {
	
      if(rear == MAX-1) {
         rear = -1;            
      }       

      intArray[++rear] = data;
      itemCount++;
   }
}

int removeData() {
   int data = intArray[front++];
	
   if(front == MAX) {
      front = 0;
   }
	
   itemCount--;
   return data;  
}

int main() {
   /* insert 5 items */
   insert(3);
   insert(5);
   insert(9);
   insert(1);
   insert(12);

   // front : 0
   // rear  : 4
   // ------------------
   // index : 0 1 2 3 4 
   // ------------------
   // queue : 3 5 9 1 12
   insert(15);

   // front : 0
   // rear  : 5
   // ---------------------
   // index : 0 1 2 3 4  5 
   // ---------------------
   // queue : 3 5 9 1 12 15
	
   if(isFull()) {
      printf("Queue is full!\n");   
   }

   // remove one item 
   int num = removeData();
	
   printf("Element removed: %d\n",num);
   // front : 1
   // rear  : 5
   // -------------------
   // index : 1 2 3 4  5
   // -------------------
   // queue : 5 9 1 12 15

   // insert more items
   insert(16);

   // front : 1
   // rear  : -1
   // ----------------------
   // index : 0  1 2 3 4  5
   // ----------------------
   // queue : 16 5 9 1 12 15

   // As queue is full, elements will not be inserted. 
   insert(17);
   insert(18);

   // ----------------------
   // index : 0  1 2 3 4  5
   // ----------------------
   // queue : 16 5 9 1 12 15
   printf("Element at front: %d\n",peek());

   printf("----------------------\n");
   printf("index : 5 4 3 2  1  0\n");
   printf("----------------------\n");
   printf("Queue:  ");
	
   while(!isEmpty()) {
      int n = removeData();           
      printf("%d ",n);
   }   
}

If we compile and run the above program, it will produce the following result-

Output

Queue is full!
Element removed: 3
Element at front: 5
----------------------
index : 5 4 3 2 1 0
----------------------
Queue: 5 9 1 12 15 16
Views

Post a Comment

0 Comments