Home Data Structures Queue Data Structue Tutorial and Implementation

Queue Data Structue Tutorial and Implementation

The Queue is a Data Structure, supports the insertion and deletion of elements in particular order, i.e, FIFO (First In First Out). Whenever an element is inserted it goes to the top of the queue and the element which is inserted at the last (element at the top of the queue) will be removed at first.

Note: Unlike stack in a queue, we maintain two pointers: head and rear.
1. head pointer point to the start of the queue.
2. rear pointer points to the last element of the queue.

Queue Data Structure with front and rear pointer pointing at the front and the end of the queue
Queue

Basic Queue Operation:

Enqueue: Enqueue operation inserts an element at the rear of the queue. Every time an element is inserted, the rear pointer is incremented.

Enqueue(var):
    If(rear == maxsize)
        print “queue is full”
    else
        queue[rear] = var
        rear++

Dequeue: Dequeue operation deletes an element from the front of the queue. Every time an element is deleted, the front pointer is incremented. If the front becomes equal to the rear, then the queue is empty.

Algorithm:

Dequeue():
    If(front == rear)
        print “queue is empty”
    else
        var = queue[front]
        queue[front] = 0
        front –;
    return var;

Time Complexity Of Stack Operation

  1. Enqueue Operation: O(1)
  2. Dequeue Operation: O(1)
  3. Search an Element: O(N)

Implementation of Queue Data Structure in CPP

Output:
Queue content is:
1 5 7 9 10 12
Queue content is:
7 9 10 12
Queue content is:
7 9 10 12 15

Subscribe to our weekly newsletter

Join our community of 1000+ developers and stay updated with the fast moving world of computer science

We promise, we won't spam
Even we hate spam as much as you hate them

LEAVE A REPLY

Please enter your comment!
Please enter your name here