diff --git a/queues/README.md b/queues/README.md index 958080a3..0f6db19e 100644 --- a/queues/README.md +++ b/queues/README.md @@ -5,3 +5,8 @@ 1. [Queue using linked list](c-or-cpp/queue-linked-list.cpp) 2. [Circular Queue using linked list](c-or-cpp/circular-queue-linked-list.cpp) 3. [Double Ended Queue (using arrays)](c-or-cpp/double-ended-queue-using-array.c) + +### Java + +1. [Queue using linked list](java/queue-linked-list.java) +2. [Circular Queue using linked list](java/circular-queue-linked-list.java) \ No newline at end of file diff --git a/queues/java/circular-queue-linked-list.java b/queues/java/circular-queue-linked-list.java new file mode 100644 index 00000000..0d182717 --- /dev/null +++ b/queues/java/circular-queue-linked-list.java @@ -0,0 +1,96 @@ +// Java program for insertion and +// deletion in Circular Queue +import java.util.*; + +class Solution { + + // Structure of a Node + static class Node { + int data; + Node link; + } + + static class Queue { + Node front, rear; + } + + // Function to create Circular queue + static void enQueue(Queue q, int value) + { + Node temp = new Node(); + temp.data = value; + if (q.front == null) + q.front = temp; + else + q.rear.link = temp; + + q.rear = temp; + q.rear.link = q.front; + } + + // Function to delete element from Circular Queue + static int deQueue(Queue q) + { + if (q.front == null) { + System.out.printf("Queue is empty"); + return Integer.MIN_VALUE; + } + + // If this is the last node to be deleted + int value; // Value to be dequeued + if (q.front == q.rear) { + value = q.front.data; + q.front = null; + q.rear = null; + } + else // There are more than one nodes + { + Node temp = q.front; + value = temp.data; + q.front = q.front.link; + q.rear.link = q.front; + } + + return value; + } + + // Function displaying the elements of Circular Queue + static void displayQueue(Queue q) + { + Node temp = q.front; + System.out.printf("\nElements in Circular Queue are: "); + while (temp.link != q.front) { + System.out.printf("%d ", temp.data); + temp = temp.link; + } + System.out.printf("%d", temp.data); + } + + /* Driver of the program */ + public static void main(String args[]) + { + // Create a queue and initialize front and rear + Queue q = new Queue(); + q.front = q.rear = null; + + // Inserting elements in Circular Queue + enQueue(q, 14); + enQueue(q, 22); + enQueue(q, 6); + + // Display elements present in Circular Queue + displayQueue(q); + + // Deleting elements from Circular Queue + System.out.printf("\nDeleted value = %d", deQueue(q)); + System.out.printf("\nDeleted value = %d", deQueue(q)); + + // Remaining elements in Circular Queue + displayQueue(q); + + enQueue(q, 9); + enQueue(q, 20); + displayQueue(q); + } +} + \ No newline at end of file diff --git a/queues/java/queue-linked-list.java b/queues/java/queue-linked-list.java new file mode 100644 index 00000000..560eb22c --- /dev/null +++ b/queues/java/queue-linked-list.java @@ -0,0 +1,78 @@ +// Java program for linked-list implementation of queue + +// A linked list (LL) node to store a queue entry +class QNode { + int key; + QNode next; + + // constructor to create a new linked list node + public QNode(int key) + { + this.key = key; + this.next = null; + } +} + +// A class to represent a queue +// The queue, front stores the front node of LL and rear stores the +// last node of LL +class Queue { + QNode front, rear; + + public Queue() + { + this.front = this.rear = null; + } + + // Method to add an key to the queue. + void enqueue(int key) + { + + // Create a new LL node + QNode temp = new QNode(key); + + // If queue is empty, then new node is front and rear both + if (this.rear == null) { + this.front = this.rear = temp; + return; + } + + // Add the new node at the end of queue and change rear + this.rear.next = temp; + this.rear = temp; + } + + // Method to remove an key from queue. + void dequeue() + { + // If queue is empty, return NULL. + if (this.front == null) + return; + + // Store previous front and move front one node ahead + QNode temp = this.front; + this.front = this.front.next; + + // If front becomes NULL, then change rear also as NULL + if (this.front == null) + this.rear = null; + } +} + +// Driver class +public class Test { + public static void main(String[] args) + { + Queue q = new Queue(); + q.enqueue(10); + q.enqueue(20); + q.dequeue(); + q.dequeue(); + q.enqueue(30); + q.enqueue(40); + q.enqueue(50); + q.dequeue(); + System.out.println("Queue Front : " + q.front.key); + System.out.println("Queue Rear : " + q.rear.key); + } +} \ No newline at end of file