0% found this document useful (0 votes)
26 views4 pages

Msc:Mphil GROUP 4 Queue

Uploaded by

akdel
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)
26 views4 pages

Msc:Mphil GROUP 4 Queue

Uploaded by

akdel
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
You are on page 1/ 4

GROUP 4

Queue Implementation Using Singly Linked Lists in Java

1. Introduction

A queue is a linear data structure that follows the First-In-First-Out (FIFO) principle,
where elements are added (enqueued) at the end and removed (dequeued) from the
front. Implementing a queue using a singly linked list provides flexibility in memory
allocation and ensures efficient operations.

2. Implementation

Here's a simple implementation of a queue using a singly linked list in Java:

Node.java Class
public class Node {
int data; // Data stored in the node
Node next; // Reference to the next node in the list

public Node(int data) {


this.data = data;
this.next = null;
}
}

Queue.java Class
// Queue class using a singly linked list
public class Queue {
private Node front, rear; // Pointers to the front and rear of the queue
private int size; // Number of elements in the queue

public Queue() {
this.front = this.rear = null; // Initially, both front and rear are null
this.size = 0;
}

// Enqueue operation - Adds an element to the end of the queue


public void enqueue(int data) {
Node newNodes = new Node(data); // Create a new node with the given data

// If the queue is empty, the new node becomes both the front and rear
if (this.rear == null) {
this.front = this.rear = newNodes;
} else {
this.rear.next = newNodes; // Link the new node at the end of the queue
this.rear = newNodes; // Update the rear pointer
}
size++;
}

// Dequeue operation - Removes and returns the front element of the queue
public int dequeue() {
if (isEmpty()) {
throw new IllegalStateException("Queue is empty");
}

int data = this.front.data; // Get the data from the front node
this.front = this.front.next; // Move the front pointer to the next node

// If the queue becomes empty, update the rear pointer to null


if (this.front == null) {
this.rear = null;
}
size--;
return data; // Return the dequeued data
}

// isEmpty operation - Checks if the queue is empty


public boolean isEmpty() {
return this.front == null;
}

// size operation - Returns the number of elements in the queue


public int size() {
return this.size;
}
}

App.java Class
public class App {
public static void main(String[] args) {
// Create a Queue instance
Queue queue = new Queue();

// Enqueue some elements


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

// Print the queue size


System.out.println("Queue size: " + queue.size());

// Dequeue an element and print it


int dequeuedElement = queue.dequeue();
System.out.println("Dequeued element: " + dequeuedElement);

// Print the queue size after dequeue


System.out.println("Queue size after dequeue: " + queue.size());

// Check if the queue is empty


System.out.println("Is the queue empty? " + queue.isEmpty());
}
}

3. Explanation

● Node Class: Represents each element in the queue. Each node contains
data and a reference (next) to the next node in the list.
● Queue Class: Manages the queue using two pointers, front and rear, to keep
track of the front and rear of the queue, respectively.
○ enqueue: Adds a new element to the rear of the queue. If the queue is
empty, the new node becomes both the front and rear. Otherwise, it
links the new node to the rear and updates the rear pointer.
○ dequeue: Removes and returns the front element of the queue. The
front pointer is updated to the next node. If the queue becomes empty,
the rear pointer is set to null.
○ isEmpty: Returns true if the queue has no elements; otherwise, false.
○ size: Returns the number of elements in the queue

4. Time Complexity Analysis


● enqueue:
○ Operation: Adds an element to the rear of the queue.
○ Time Complexity: O(1) - The operation involves updating pointers,
which takes constant time.
● dequeue:
○ Operation: Removes the element at the front of the queue.
○ Time Complexity: O(1) - Similar to enqueue, it updates pointers in
constant time.
● isEmpty:
○ Operation: Checks if the queue is empty.
○ Time Complexity: O(1) - Simply checks if the front pointer is null.
● size:
○ Operation: Returns the size of the queue.
○ Time Complexity: O(1) - Directly returns the size variable.

5. Challenges and Benefits of Using Linked Lists for Queue Implementation

● Challenges:
○ Memory Usage: Each element requires extra memory for the pointer
(next), leading to higher memory consumption compared to arrays.
○ Complexity: Linked list implementation is slightly more complex due to
pointer management.
● Benefits:
○ Dynamic Size: Unlike arrays, linked lists do not require a fixed size,
allowing the queue to grow and shrink as needed without reallocating
memory.
○ Efficient Operations: Both enqueue and dequeue operations are
efficient (O(1)) because no shifting of elements is required, as would be
the case in an array-based implementation.

6. Conclusion

Implementing a queue using a singly linked list in Java is a straightforward approach


that offers dynamic sizing and efficient operations. While it may involve slightly more
complex pointer management, it avoids the need for preallocated memory, making it
ideal for situations where the queue size is unpredictable.

You might also like