About Me

Queue Data Structure

Queue follows First-In-First-Out methodology, i.e., the data item stored first will be accessed first.

  • Queue Representation:

    • Queue: the name of the array storing queue elements.
    • Front: the index where the first element is stored in the array representing the queue.
    • Rear: the index where the last element is stored in an array representing the queue



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

Implementation of peek() function in C programming language −

Example

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

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 −

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 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;
}
  • Enqueue Operation

    Queues maintain 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 overflow error and exit.

    • Step 3 − If the queue is not full, increment rear pointer to point the next empty space.

    • Step 4 − Add 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

Dequeue Operation

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

  • Step 1 − Check if the queue is empty.

  • Step 2 − If the queue is empty, produce underflow error and exit.

  • Step 3 − If the queue is not empty, access the data where front is pointing.

  • Step 4 − Increment front pointer to point to the next available data element.

  • Step 5 − Return success.

Remove Operation

Algorithm for dequeue operation

procedure dequeue
   
   if queue is empty
      return underflow
   end if

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

end procedure







Memory Representation of Queues

Like Stacks, Queues can also be represented in memory in two ways.

Using the contiguous memory like an array
Using the non-contiguous memory like a linked list

Using the Contiguous Memory like an Array

In this representation the Queue is implemented using the array. Variables used in this case are

QUEUE- the name of the array storing queue elements.
FRONT- the index where the first element is stored in the array representing the queue.
REAR- the index where the last element is stored in array representing the queue.
MAX- defining that how many elements (maximum count) can be stored in the array representing the  queue.





Using the Non-Contiguous Memory like a Linked List

In this representation the queue is implemented using the dynamic data structure Linked List. Using linked list for creating a queue makes it flexible in terms of size and storage. You don’t have to define the maximum number of elements in the queue.

Pointers (links) to store addresses of nodes for defining a queue are.

FRONT- address of the first element of the Linked list storing the Queue.
REAR- address of the last element of the Linked list storing the Queue.



Queue Applications

Queues are used in various applications in Computer Science-

  • Job scheduling tasks of CPU.
  • Printer’s Buffer to store printing commands initiated by a user.
  • Input commands sent to CPU by  devices like Keyboard and Mouse.
  • Document downloading from internet.
  • User Requests for  call center services.
  • Order Queue for Online Food Delivery Chains.
  • Online Cab Booking applications.

Characteristics of Queue:

  • Queue can handle multiple data.
  • We can access both ends.
  • They are fast and flexible. 

Post a Comment

0 Comments