From 7500e7c9d1c34f2010c0bd520308226422cb20b2 Mon Sep 17 00:00:00 2001 From: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:23:37 +0530 Subject: [PATCH 1/3] Create StackUsingQueue.java Added a java implementation of Stack using Queue Signed-off-by: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> --- JAVA/StackUsingQueue.java | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 JAVA/StackUsingQueue.java diff --git a/JAVA/StackUsingQueue.java b/JAVA/StackUsingQueue.java new file mode 100644 index 0000000..f4ff1fa --- /dev/null +++ b/JAVA/StackUsingQueue.java @@ -0,0 +1,52 @@ +import java.util.LinkedList; +import java.util.Queue; + +public class StackUsingQueue { + private Queue queue1; + private Queue queue2; + + public StackUsingQueue() { + queue1 = new LinkedList<>(); + queue2 = new LinkedList<>(); + } + + public void push(int element) { + queue2.offer(element); + while (!queue1.isEmpty()) { + queue2.offer(queue1.poll()); + } + Queue temp = queue1; + queue1 = queue2; + queue2 = temp; + } + + public int pop() { + if (queue1.isEmpty()) { + return -1; // Stack is empty + } + return queue1.poll(); + } + + public int top() { + if (queue1.isEmpty()) { + return -1; // Stack is empty + } + return queue1.peek(); + } + + public boolean isEmpty() { + return queue1.isEmpty() && queue2.isEmpty(); + } + + public static void main(String[] args) { + StackUsingQueue stack = new StackUsingQueue(); + stack.push(1); + stack.push(2); + stack.push(3); + + System.out.println(stack.top()); // Output: 3 + System.out.println(stack.pop()); // Output: 3 + System.out.println(stack.pop()); // Output: 2 + System.out.println(stack.isEmpty()); // Output: false + } +} From 57c3a3392df5691bc3235affac395b13e843e6b0 Mon Sep 17 00:00:00 2001 From: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> Date: Sun, 22 Oct 2023 20:29:40 +0530 Subject: [PATCH 2/3] Create ReverseStringUsingStack.java Signed-off-by: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> --- JAVA/ReverseStringUsingStack.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 JAVA/ReverseStringUsingStack.java diff --git a/JAVA/ReverseStringUsingStack.java b/JAVA/ReverseStringUsingStack.java new file mode 100644 index 0000000..07cca29 --- /dev/null +++ b/JAVA/ReverseStringUsingStack.java @@ -0,0 +1,27 @@ +import java.util.Stack; + +public class ReverseStringUsingStack { + public static String reverseString(String input) { + Stack stack = new Stack<>(); + + // Push each character of the input string onto the stack + for (char c : input.toCharArray()) { + stack.push(c); + } + + // Pop the characters from the stack to build the reversed string + StringBuilder reversedString = new StringBuilder(); + while (!stack.isEmpty()) { + reversedString.append(stack.pop()); + } + + return reversedString.toString(); + } + + public static void main(String[] args) { + String input = "Hello, World!"; + String reversed = reverseString(input); + System.out.println("Original String: " + input); + System.out.println("Reversed String: " + reversed); + } +} From e76b9aaa133ee1d58c8722736cf29ddb83877427 Mon Sep 17 00:00:00 2001 From: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> Date: Sun, 22 Oct 2023 23:42:03 +0530 Subject: [PATCH 3/3] Update StackUsingQueue.java Signed-off-by: Hvr123 <133820482+HarshVR1947@users.noreply.github.com> --- JAVA/StackUsingQueue.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/JAVA/StackUsingQueue.java b/JAVA/StackUsingQueue.java index f4ff1fa..4c12592 100644 --- a/JAVA/StackUsingQueue.java +++ b/JAVA/StackUsingQueue.java @@ -37,16 +37,4 @@ public int top() { public boolean isEmpty() { return queue1.isEmpty() && queue2.isEmpty(); } - - public static void main(String[] args) { - StackUsingQueue stack = new StackUsingQueue(); - stack.push(1); - stack.push(2); - stack.push(3); - - System.out.println(stack.top()); // Output: 3 - System.out.println(stack.pop()); // Output: 3 - System.out.println(stack.pop()); // Output: 2 - System.out.println(stack.isEmpty()); // Output: false - } }