0% found this document useful (0 votes)
12 views8 pages

Queue Using Linked List

The document explains the implementation of a queue using a linked list, detailing its structure, operations (enqueue, dequeue, peek, isEmpty), and advantages over array implementation. It also discusses multiple queues, their applications in various fields such as CPU scheduling, disk scheduling, and network management, and includes a C++ program demonstrating the queue functionality. The linked list approach offers dynamic sizing, efficient memory usage, and avoids element shifting during deletions.

Uploaded by

ketaki.mali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

Queue Using Linked List

The document explains the implementation of a queue using a linked list, detailing its structure, operations (enqueue, dequeue, peek, isEmpty), and advantages over array implementation. It also discusses multiple queues, their applications in various fields such as CPU scheduling, disk scheduling, and network management, and includes a C++ program demonstrating the queue functionality. The linked list approach offers dynamic sizing, efficient memory usage, and avoids element shifting during deletions.

Uploaded by

ketaki.mali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Queue Using Linked List

1. Introduction
 A Queue is a linear data structure that follows the FIFO
(First In, First Out) principle.
 In linked list implementation, queue elements are stored
in nodes connected using pointers.
 Each node contains:
o Data (the element stored)

o Pointer (next) (address of next node)

2. Structure of Queue Node


struct Node {
int data;
Node* next;
};
 Front pointer → points to the first node (for deletion).
 Rear pointer → points to the last node (for insertion).

3. Queue Operations (Linked List Implementation)


1. Enqueue (Insertion):
o Create a new node.

o If queue is empty, make both front and rear point to

the new node.


o Else, insert the new node at the end (rear->next =

newNode) and update rear.


2. Dequeue (Deletion):
o If queue is empty → underflow condition.
o Otherwise, remove the node pointed by front and
move front = front->next.
3. Peek (Front element):
o Return the value of front without deletion.

4. isEmpty():
o Returns true if front == NULL.

4. Advantages of Linked List Implementation over Array


 Dynamic Size: No need to define a fixed size (as in arrays).
 Efficient Memory Usage: Memory is allocated as needed
(no wastage like array’s unused slots).
 No Shifting Required: In arrays, deletion from the front
requires shifting elements, but linked list avoids this.
 Overflow Rare: Possible only when system memory is
exhausted, unlike arrays with a fixed size.

Multiple Queues
1. Definition
 Multiple queues refer to having two or more queues
implemented simultaneously.
 They may be required in:
o Multiprogramming (separate queues for each process

priority).
o Operating systems (ready queue, waiting queue,

device queues).
o Simulation systems (queues for different

servers/lines).
2. Implementation Approaches
 Using Arrays: Divide a single array into multiple parts for
different queues.
 Using Linked Lists: Each queue maintains its own front
and rear pointers independently.

Applications of Queue
1. CPU Scheduling (Operating Systems):
o Ready queue, waiting queue, and device queues.

o Process scheduling uses queues.

2. Disk Scheduling:
o Requests to access disk sectors are managed using

queues.
3. Printer Queue (Spooling):
o Print jobs are queued in the order they are received.

4. Network Applications:
o Packets waiting to be transmitted or processed are

stored in queues.
5. Simulation of Real-life Queues:
o Ticket booking counters, supermarket billing systems.

6. Data Buffering:
o Used in I/O devices for temporary storage (e.g.,

keyboard buffer).
7. Breadth-First Search (BFS) Algorithm:
o Graph traversal uses queue.

8. Call Centers / Customer Service:


o Calls are answered in the order they are received

(FIFO).
Queue Using Linked List

Diagram – Queue Representation

Initial Queue (Empty):


Front = NULL, Rear = NULL
After Enqueue(10), Enqueue(20), Enqueue(30):
Front → [10 | *] → [20 | *] → [30 | NULL] ← Rear
After Dequeue():
Front → [20 | *] → [30 | NULL] ← Rear
(10 is removed)

C++ Program – Queue using Linked List


#include <iostream>
using namespace std;

// Node structure
struct Node {
int data;
Node* next;
};

// Queue class using Linked List


class Queue {
private:
Node* front; // points to front element
Node* rear; // points to rear element

public:
Queue() {
front = rear = NULL;
}

// Enqueue: Insert element at the end


void enqueue(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;

if (rear == NULL) { // Empty queue


front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
cout << value << " inserted into queue." << endl;
}

// Dequeue: Remove element from front


void dequeue() {
if (front == NULL) {
cout << "Queue Underflow (empty queue)." << endl;
return;
}

Node* temp = front;


cout << "Deleted: " << temp->data << endl;
front = front->next;

if (front == NULL) // If queue becomes empty


rear = NULL;

delete temp;
}

// Peek: Get front element


void peek() {
if (front == NULL) {
cout << "Queue is empty." << endl;
} else {
cout << "Front element: " << front->data << endl;
}
}

// Display all elements


void display() {
if (front == NULL) {
cout << "Queue is empty." << endl;
return;
}

Node* temp = front;


cout << "Queue elements: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

// Main Function
int main() {
Queue q;

q.enqueue(10);
q.enqueue(20);
q.enqueue(30);

q.display();
q.peek();

q.dequeue();
q.display();

q.dequeue();
q.dequeue();
q.dequeue(); // underflow case

return 0;
}

Output
10 inserted into queue.
20 inserted into queue.
30 inserted into queue.
Queue elements: 10 20 30
Front element: 10
Deleted: 10
Queue elements: 20 30
Deleted: 20
Deleted: 30
Queue Underflow (empty queue).

Advantages of Queue using Linked List over Array

 No fixed size limitation.


 No element shifting needed during deletion.
 Memory allocated dynamically.
 Overflow occurs only if memory is full.

Applications of Queue

 CPU Scheduling
 Printer Spooling
 Disk Scheduling
 Network packet management
 BFS traversal in graphs
 Customer service systems

You might also like