Queue Using Linked List
# initialize a nodeclass Node: def __init__(self, value): self.data = value self.next = None
class Queue: def __init__(self): self.front = None self.rear = None
def do_enqueue(self, value): # Create a new node new_node = Node(value) # If queue is empty, set # new node as front and rear if self.front is None: self.front = new_node self.rear = new_node else: # If queue is not empty, # add new node at the rear self.rear.next = new_node self.rear = new_node
def display(self): # Display the elements in the queue current = self.front if current is None: print("Queue is empty") return while current is not None: print(current.data, end=" < - ") current = current.next print("rear")
def do_dequeue(self): # Check if queue is empty if self.front is None: print("Queue is empty, cannot dequeue") return None else: # dequeue the front element temp = self.front self.front = self.front.next if self.front is None: self.rear = None return temp.data
# Example usequeue = Queue()queue.do_enqueue(1)queue.do_enqueue(2)queue.do_enqueue(3)
# Display the elements in the queuequeue.display() dequeued_value = queue.do_dequeue()print(f"Dequeued value: {dequeued_value}")
# Display the elements after do_dequeuequeue.display()
Output
1 <- 2 <- 3 <- rear
Dequeued value: 1
2 <- 3 <- rear
Implementing Queue using Linked List in C++
In the code below, we have implemented a queue using a linked list in C++. Comments have been added for better readability.
#include < iostream >
// the node classclass Node {public: int data; Node* next;
Node(int value) : data(value), next(nullptr) {}};
// the queueclass Queue {private: Node* front; Node* rear;
public: Queue() : front(nullptr), rear(nullptr) {}
void do_enqueue(int value) { // Create a new node Node* new_node = new Node(value);
// If queue is empty, set // new node as front and rear if (front == nullptr) { front = new_node; rear = new_node; } else { // If queue is not empty, // add new node at the rear rear- > next = new_node; rear = new_node; } }
void display() { // Display the elements in the queue Node* current = front; if (current == nullptr) { std::cout < < "Queue is empty" < < std::endl; return; } while (current != nullptr) { std::cout < < current- > data < < " < - "; current = current- > next; } std::cout < < "rear" < < std::endl; }
int do_dequeue() { // Check if queue is empty if (front == nullptr) { std::cout < < "Queue is empty, cannot dequeue" < < std::endl; // Return a default value to // indicate empty queue return -1; } else { // Dequeue the front element Node* temp = front; front = front- > next; if (front == nullptr) { rear = nullptr; } int dequeued_value = temp- > data; // Free the memory delete temp; return dequeued_value; } }};
int main() { Queue queue; queue.do_enqueue(1); queue.do_enqueue(2); queue.do_enqueue(3);
// Display the elements in the queue queue.display();
int dequeued_value = queue.do_dequeue(); std::cout < < "Dequeued value: " < < dequeued_value < < std::endl;
// Display the elements after dequeue queue.display(); return 0;}
class Node { int data; Node next;
public Node(int value) { data = value; next = null; }}
class Queue { private Node front; private Node rear;
public Queue() { front = null; rear = null; }
public void do_enqueue(int value) { Node new_node = new Node(value); if (front == null) { front = new_node; rear = new_node; } else { rear.next = new_node; rear = new_node; } }
public void display() { Node current = front; if (current == null) { System.out.println("Queue is empty"); return; } while (current != null) { System.out.print(current.data + " < - "); current = current.next; } System.out.println("rear"); }
public int do_dequeue() { if (front == null) { System.out.println("Queue is empty, cannot dequeue"); return -1; } else { Node temp = front; front = front.next; if (front == null) { rear = null; } int dequeuedValue = temp.data; return dequeuedValue; } }}
class Main { public static void main(String[] args) { Queue queue = new Queue(); queue.do_enqueue(1); queue.do_enqueue(2); queue.do_enqueue(3);
queue.display();
int dequeuedValue = queue.do_dequeue(); System.out.println("Dequeued value: " + dequeuedValue);
queue.display(); }}
Output
1 <- 2 <- 3 <- rear
Dequeued value: 1
2 <- 3 <- rear
Real-World Application of Queue
Below listed are some real-world use cases of the queue data structure
- Operating Systems: Queues are often used to schedule processes to be executed by the CPU. Queues are used to prioritize tasks by various scheduling algorithms within the operating system. It is also used to manage the I/O requests in operating systems.
- Data Traffic Management: Devices like Routers and switches use queues to manage data buffers for data packet transmission.
- Web Servers: Web servers use a queue data structure to manage and optimize incoming requests. All requests to the server is added to the queue and are processed using the FIFO principle.
- Printers: Printers, while printing multiple documents, use queues to manage the workload and process each printing task based on the queue.
- Banking Systems: Banking transactions like RTGS are managed using queues, as the bank receives many customer transaction requests. Each such request is put into a queue and is processed one at a time.
- Call Center Systems: The queue data structure manages customer connect requests to the support representative. It works on a first-come, first-serve basis.
- Television Broadcast: Television stations manage the broadcast queue, ensuring programs or advertisements are played in the intended sequence.
Conclusion
Thus, implementing a queue using a linked list is a fundamental and efficient approach in data structure and algorithm design. This method utilizes the dynamic nature of linked lists to manage data in a FIFO (First In, First Out) manner, ensuring a easy and flexible operation of enqueue (adding) and dequeue (removing) elements.
Contributed by: Raju Kumar
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio