Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Implement Queue using Linked List Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Implement Queue using Linked List

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Implement a queue using a Linked List as the underlying container.

  • The Queue class should support the following methods:
  • int size()
  • boolean isEmpty()
  • int front()
  • int back()
  • void push(int element)
  • void pop()

Approach

We keep two linked-list pointers that point to the front and the rear of the queue. We also keep track of the size and capacity of the queue. We move the front and the rear pointers accordingly based on the push and pop operations to perform these operations in constant time. Also, adjust the size of the queue as required during these operations. Refer to the implementation for more details.

Analysis

  • Time Complexity: O(1) for all the operations
  • Space Complexity: O(1) for all the operations

Implementation

C++
/* This is the ListNode class definition

class ListNode {
public:
	int data;
	ListNode* next;

	ListNode(int data) {
		this->data = data;
		this->next = NULL;
	}
};
*/

// Implement the Queue class
class Queue {
public:
	ListNode* queueFront;
	ListNode* rear;
	int queueSize = 0;
	int total = 0;
	Queue (int capacity) {
		queueFront = rear = NULL;
		queueSize = 0;
		total = capacity;
	}

	bool isEmpty() {
		return rear == NULL;
	}
	
	int size() {
		return queueSize;
	}
	
	int front() {
		if(queueFront == NULL) {
			return -1;
		}
		return queueFront->data;
	}
	
	int back() {
		if(rear == NULL) {
			return -1;
		}
		return rear->data;
	}
	
	void push(int element) {
		ListNode* temp = new ListNode(element);
		if (rear == NULL) {
            queueFront = rear = temp;
			queueSize++;
            return;
        }
		rear->next = temp;
        rear = temp;
		queueSize++;
	}
	
	void pop() {
		if (this->queueFront == NULL) {
            return;
		}
        ListNode* temp = this->queueFront;
        queueFront = queueFront->next;
        if (queueFront == NULL) {
            rear = NULL;
		}
		queueSize--;
	}
};
Java
/* This is the ListNode class definition
class ListNode {
	int data;
	ListNode next;
	
	ListNode(int data) {
		this.data = data;
		this.next = null;
	}
}
*/

// Implement the Queue class
class Queue{
	ListNode front;
	ListNode rear;
	int size = 0;
	int total = 0;
	
	Queue (int capacity) {
		front = rear = null;
		size = 0;
		total = capacity;

	}

	boolean isEmpty() {
		return rear == null;
	}
	
	int size() {
		return size;
	}
	
	int front() {
		if(front == null) {
			return -1;
		}
		return front.data;
	}
	
	int back() {
		if(rear == null) {
			return -1;
		}
		return rear.data;

	}
	
	void push(int element) {
		ListNode temp = new ListNode(element);
		if (rear == null) {
            front = rear = temp;
			size++;
            return;
        }
		rear.next = temp;
        rear = temp;
		size++;

	}
	
	void pop() {
		if (this.front == null) {
            return;
		}
        ListNode temp = this.front;
        front = front.next;
        if (front == null) {
            rear = null;
		}
		size--;
	}
}
Related Content
Implement Queue using Array
Implement Queue using Stacks
Implement Stack using Queues
Rotting Apples
Sliding Window Maximum
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet