📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Queue Implementation using Linked List
/**
* Class demonstrates the declaration for a linked list.
* @author Ramesh Fadatare
*
*/
public class ListNode{
public ListNode next;
public int data;
// Creates an empty node.
public ListNode(){
next = null;
data = Integer.MIN_VALUE;
}
// Creates a node storing the specified data.
public ListNode (int data){
next = null;
this.data = data;
}
// Returns the node that follows this one.
public ListNode getNext(){
return next;
}
// Sets the node that follows this one.
public void setNext (ListNode node){
next = node;
}
// Returns the data stored in this node.
public int getData(){
return data;
}
// Sets the data stored in this node.
public void setdata (int elem){
data = elem;
}
// Sets the data stored in this node.
public String toString (){
return Integer.toString(data);
}
}
/**
* Queue Implementation using Linked List
* @author Ramesh Fadatare
*
*/
public class LinkedQueue{
private int length;
private ListNode front, rear;
// Creates an empty queue.
public LinkedQueue(){
length = 0;
front = rear = null;
}
// Adds the specified data to the rear of the queue.
public void enQueue (int data){
ListNode node = new ListNode(data);
if (isEmpty())
front = node;
else
rear.setNext (node);
rear = node;
length++;
}
// Removes the data at the front of the queue and returns a
// reference to it. Throws an Exception if the
// queue is empty.
public int deQueue() throws Exception{
if (isEmpty())
throw new Exception ("queue");
int result = front.getData();
front = front.getNext();
length--;
if (isEmpty())
rear = null;
return result;
}
// Returns a reference to the data at the front of the queue.
// The data is not removed from the queue. Throws an
// Exception if the queue is empty.
public int first() throws Exception{
if (isEmpty())
throw new Exception();
return front.getData();
}
// Returns true if this queue is empty and false otherwise.
public boolean isEmpty(){
return (length == 0);
}
// Returns the number of elements in this queue.
public int size(){
return length;
}
// Returns a string representation of this queue.
public String toString(){
String result = "";
ListNode current = front;
while (current != null){
result = result + current.toString() + "\n";
current = current.getNext();
}
return result;
}
public static void main(String[] args) throws Exception {
LinkedQueue arrayQueue = new LinkedQueue();
arrayQueue.enQueue(10);
arrayQueue.enQueue(20);
arrayQueue.enQueue(30);
arrayQueue.enQueue(40);
arrayQueue.enQueue(50);
arrayQueue.enQueue(60);
arrayQueue.enQueue(70);
arrayQueue.enQueue(80);
arrayQueue.enQueue(90);
arrayQueue.deQueue();
System.out.println(arrayQueue.toString());
}
}
20
30
40
50
60
70
80
90
Performance
- Space Complexity (for n enQueue operations) O(n)
- Time Complexity of enQueue() O(1) (Average)
- Time Complexity of deQueue() O(1)
- Time Complexity of isEmpty() O(1)
- Time Complexity of deleteQueue() O(1)
Comments
Post a Comment
Leave Comment