Skip to content

Commit

Permalink
minor modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
ritvij14 committed Mar 9, 2021
1 parent 4d1ef04 commit f3b4f24
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions Java/ds/StackUsingQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ void pop() {
if (qu2.isEmpty()) {
return;
}

// remove element and reduce size if qu2 not empty
qu2.remove();
currentSize--;
}
Expand All @@ -41,6 +43,8 @@ int top() {
if (qu2.isEmpty()) {
return -1;
}

// return the top element
return qu2.peek();
}

Expand All @@ -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();
Expand All @@ -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());
Expand Down Expand Up @@ -93,4 +95,4 @@ public static void main(String[] args) {
*
* Time complexity: O(n) for push, O(1) for pop
* Space complexity: O(n)
*/
*/

0 comments on commit f3b4f24

Please sign in to comment.