What are Queues ? - data structure tutorial



Today we are going to discuss about What are Queues ? - data structure tutorial .
A Queue is a linear structure that follows a particular order in which the operations are performed. Queues follow the order of First In First Out (FIFO). A good example of a queue is that the process which came into the main memory first will execute first. The difference between stacks and queues is in removing. In a stack we remove the item the most recently added; in a queue, we remove the item the least recently added.



What are Queues ? - data structure tutorial


Basic Operations


The fundamental operations of a queue are −

·      enqueue() − adds an element to the queue.

·      dequeue() − removes an element from the queue.

Some other helpful operations of a queue are −

·      peek() – returns an element at the front of the queue.

·      isfull() − Checks if the queue is full.

·      isempty() − Checks if the queue is empty.


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


peek()



This function returns the element at the front of the queue. The algorithm of peek() function is as follows −


Implementation of peek() function is as follows −






isfull()


To check if a queue is full or not, we just compare rear with the size of the array that we are using to store elements. If rear is greater or equals to the size of the array then the queue is full.


Implementation of isfull() function is as follows −




isempty()

To check if a queue is empty or not we check if front and rear are less than 0 or not.


Actually, the value of front and rear are assigned as -1 in the beginning. So, if no value was added or deleted then the queue is empty or value of front and rear remain -1.


Here's the C programming code −




Enqueue Operation

  1. First of all, Check if the queue is full or not.
  2. If the queue is full, then print a message of queue overflow and exit.
  3. If the queue is not full, then increment the rear pointer and allocate the value to new position of rear in array.


Implementation of enqueue() is as follows −




Algorithm for DEQUEUE operation

  1. First of all, check if the queue is empty or not.
  2. If the queue is empty, then print a message of queue underflow and exit.
  3. If the queue is not empty, then just increment the value of front.


Implementation of dequeue() is as follows −


Algorithm for Display operation



To print the values of queue just iterate a for loop from front till rear and print values within the loop.



Implementation of display() is as follows:



Complete program for enqueue, dequeue and display of a queue.







This was all about What are Queues ? - data structure tutorial. In the upcoming post we will learn about circular queues.

Till then, try these programs and comment if you were unable to solve any of these:
  •   WAP to implement queue using stacks.
  •   WAP to delete alternate elements of queue.
  •   WAP to swap first and last node of queue.
  •   WAP to sort a queue.


Post a Comment (0)
Previous Post Next Post