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

Link List Implementation of Queue

The document describes the implementation of a queue using a linked list, detailing the structure of nodes and the operations for insertion and deletion. It includes an algorithm for enqueueing and dequeueing elements, as well as methods for peeking at the front element and printing the queue. A sample main class demonstrates the functionality of the queue with various operations.

Uploaded by

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

Link List Implementation of Queue

The document describes the implementation of a queue using a linked list, detailing the structure of nodes and the operations for insertion and deletion. It includes an algorithm for enqueueing and dequeueing elements, as well as methods for peeking at the front element and printing the queue. A sample main class demonstrates the functionality of the queue with various operations.

Uploaded by

zorod0055
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
Syst intin(" '[Link]("'Item deleted from queue: " + [Link]()); 3 3 iked list Implementation of Queue in the linked list-based ; queue, each node contains two parts: the pointer to the next node. The queue has two main ointers:. See 7 . Front: The front can point to the first node of the linked list. . Rear: Rear can point to the last node of the linked list. at} 4a 4a) i front 7 Linked Queue Algorithm Insertion Step 1: Create a new linked list node with the given value. Step 2: If the rear value is None then set the front and rear to the newly created node. Step 3: Else set next of rear to the newly created node and move rear to that newly created node Deletion Step 1: If the front is NULL then return ‘queue is empty’ Step 2: else return the value at the front node and move the front to next of it. Step 3: If the front is Null then set the rear to Null class Node { int data; Node next; public Node(int data) { [Link] = data; [Link] = null; } } class Queue { Node front, rear; int length; public QueneQ { [Link] = [Link] = null; [Link]=0; ¥ void enqueue(int key) { [Link]++; Node temp = new Node(key); if ([Link] { [Link] = [Link] = temp; return; } null) [Link]-next = temp; [Link] = temp; } void dequeQ) { if ([Link] == null) return; Node temp = [Link]; [Link] = [Link]; if ([Link] == null) [Link] = null; [Link] = null; } int peek() { if ([Link] != null) return [Link]; return Integer.MIN_VALUE; } int sizeQ) { return [Link]; 3 void printQueue() { Node temp=[Link]; [Link]("Element of Queue : "); while(temp!=null) ee "; temp=[Link]; } [Link](); } } public class Main { public static void main(String[] args) { Queue q = new Queue(); q-enqueue(1 0); q-enqueue(20); q-printQueue(); [Link]("Size 2" + [Link])s [Link]); [Link]); q-enqueue(30); q-enqueue(40); q-enqueue(50); q-printQueue(); [Link]("'Size 2" + [Link])s [Link]("Front item is: " + q-peekQ); } }

You might also like