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 + */