Implementing Queues using Linked List

To implement a queue using a Linked List,

  • Maintain two pointers, front and rear. The front points to the first item of the queue and rear points to the last item.
    • enQueue(): This operation adds a new node after the rear and moves the rear to the next node.
    • deQueue(): This operation removes the front node and moves the front to the next node.

Now, some of the implementations of queue operations are as follows: 

  • Create a class QNode with data members integer data and QNode* next (pointer to the next node)
  • Create a class Queue with data members QNode front and rear
  • Enqueue Operation with parameter x:
    • Initialize QNode* temp with data = x
    • If the rear is set to NULL then set the front and rear to temp and return (Base Case)
    • Else set rear next to temp and then move rear to temp
    • Enqueue has O(1) time complexity.
  • Dequeue Operation:
    • If the front is set to NULL return (Base Case)
    • Initialize QNode temp with front and set front to its next
    • If the front is equal to NULL then set the rear to NULL
    • Delete temp from the memory.
    • Dequeue has O(1) time complexity.
  • Overall Space Complexity: O(N)

Queue Notes for GATE Exam [2024]

A Queue is defined as a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) order. Queue is a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.  The element, which is first pushed into the order, the operation is first performed on that.

FIFO Property in Queue

Table of Content

  • Implementing Queues using Arrays:
  • Implementing Queues using Linked List:
  • Implementation of Queue using Stack:
  • Circular Queue:
  • Priority Queue:
  • Double Ended Queue:
  • GATE Archives – Previous Years Questions on Queue:

Similar Reads

Implementing Queues using Arrays:

To implement a queue using an array,...

Implementing Queues using Linked List:

To implement a queue using a Linked List,...

Implementation of Queue using Stack:

...

Circular Queue:

A Circular Queue is an extended version of a normal queue where the last element of the queue is connected to the first element of the queue forming a circle. The operations are performed based on FIFO (First In First Out) principle. It is also called ‘Ring Buffer’....

Priority Queue:

A priority queue is a type of queue that arranges elements based on their priority values. Elements with higher priority values are typically retrieved before elements with lower priority values.If we add an element with a high priority value to a priority queue, it may be inserted near the front of the queue, while an element with a low priority value may be inserted near the back....

Double Ended Queue:

Deque or Double Ended Queue is a generalized version of Queue data structure that allows insert and delete at both ends....

GATE Archives – Previous Years Questions on Queue:

Q1. Following is C like pseudo-code of a function that takes a Queue as an argument, and uses a stack S to do processing....

Contact Us