From 4d1ef04414966763b6918fa5dbad7ee01befcdf5 Mon Sep 17 00:00:00 2001 From: ritvij14 Date: Tue, 9 Mar 2021 09:52:21 +0530 Subject: [PATCH 1/2] added implementation of stack using Queue in Java --- Java/README.md | 1 + Java/ds/StackUsingQueue.java | 96 ++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 Java/ds/StackUsingQueue.java diff --git a/Java/README.md b/Java/README.md index 0b4d532484..035f37043f 100644 --- a/Java/README.md +++ b/Java/README.md @@ -104,6 +104,7 @@ _add list here_ - [Sparse Matrix](ds/SparseMatrix.java) - [Stack ~ Linked List Implementation](ds/Stack.java) - [Stack](ds/Stackll.java) Implementation-of-queue-using-stack +- [Implementation of Stack using Queue](ds/StackUsingQueue.java) - [Tree Traversal(preorder traversal)](ds/preOrderTraversal.java) - [Tree Traversal(postorder traversal)](ds/postorder_Traversal.java) - [Priority Queue](ds/PriorityQueueEg.java) diff --git a/Java/ds/StackUsingQueue.java b/Java/ds/StackUsingQueue.java new file mode 100644 index 0000000000..c5bd17fef4 --- /dev/null +++ b/Java/ds/StackUsingQueue.java @@ -0,0 +1,96 @@ +/* Java Program to implement a stack using two queues +by making the push operation costly*/ + +import java.util.*; + +public class StackUsingQueue { + static class Stack { + // the 2 queues + Queue qu1 = new LinkedList<>(); + Queue qu2 = new LinkedList<>(); + + // storing current size + int currentSize; + + Stack() { + currentSize = 0; + } + + void push(int num) { + currentSize++; + + // first, push num into an empty qu2 + qu2.add(num); + + // now we push the other elements from qu1 to qu2 + while (!qu1.isEmpty()) { + qu2.add(qu1.peek()); + qu1.remove(); + } + } + + void pop() { + if (qu2.isEmpty()) { + return; + } + qu2.remove(); + currentSize--; + } + + int top() { + if (qu2.isEmpty()) { + return -1; + } + return qu2.peek(); + } + + int size() { + return currentSize; + } + } + + public static void main(String[] args) { + Stack stack = new Stack(); + + // taking input + Scanner sc = new Scanner(System.in); + System.out.println("Enter size:"); + int size = sc.nextInt(); + System.out.println("Enter the numbers:"); + for (int i = 0; i < size; i++) { + stack.push(sc.nextInt()); + } + sc.close(); + + // giving output + System.out.println("Current Size:" + stack.size()); + for (int i = size; i > 0; i--) { + System.out.println(stack.top()); + stack.pop(); + } + System.out.println("Current Size:" + stack.size()); + } +} + +/** + * Sample input/output: + * + * Enter size: + * 5 + * Enter the numbers: + * 5 + * 4 + * 3 + * 2 + * 1 + * Current Size:5 + * 5 + * 4 + * 3 + * 2 + * 1 + * Current Size:0 + * + * Time complexity: O(n) for push, O(1) for pop + * Space complexity: O(n) + */ \ No newline at end of file From f3b4f24fa6cc2fe9b029ed5374941a6c357fd063 Mon Sep 17 00:00:00 2001 From: ritvij14 Date: Tue, 9 Mar 2021 11:17:37 +0530 Subject: [PATCH 2/2] minor modifications --- Java/ds/StackUsingQueue.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Java/ds/StackUsingQueue.java b/Java/ds/StackUsingQueue.java index c5bd17fef4..dc5ec2fdf3 100644 --- a/Java/ds/StackUsingQueue.java +++ b/Java/ds/StackUsingQueue.java @@ -33,6 +33,8 @@ void pop() { if (qu2.isEmpty()) { return; } + + // remove element and reduce size if qu2 not empty qu2.remove(); currentSize--; } @@ -41,6 +43,8 @@ int top() { if (qu2.isEmpty()) { return -1; } + + // return the top element return qu2.peek(); } @@ -52,7 +56,6 @@ int size() { public static void main(String[] args) { Stack stack = new Stack(); - // taking input Scanner sc = new Scanner(System.in); System.out.println("Enter size:"); int size = sc.nextInt(); @@ -62,7 +65,6 @@ public static void main(String[] args) { } sc.close(); - // giving output System.out.println("Current Size:" + stack.size()); for (int i = size; i > 0; i--) { System.out.println(stack.top()); @@ -93,4 +95,4 @@ public static void main(String[] args) { * * Time complexity: O(n) for push, O(1) for pop * Space complexity: O(n) - */ \ No newline at end of file + */