+ 2

Why are 2 stack push statement needed here ??

public class stack { static void stack_push(Stack<Integer> stack){         System.out.println("Push Operation:"); for(int i = 0; i < 5; i++) { stack.push(i);             System.out.print(" "+i);             }         System.out.print(" \n"); } // Popping element from the top of the stack static void stack_pop(Stack<Integer> stack) { System.out.println("Pop Operation:"); for(int i = 0; i < 5; i++) { Integer y = (Integer) stack.pop(); System.out.print(" "+y);             }System.out.print(" \n");     } // Displaying element on the top of the stack static void stack_peek(Stack<Integer> stack) { Integer element = (Integer) stack.peek(); System.out.println("Element on stack top: " + element); } static void stack_search(Stack<Integer> stack, int element) { Integer pos = (Integer) stack.search(element);} public static void main (String[] args) { Stack<Integer> stack = new Stack<Integer>(); stack_push(stack); stack_pop(stack); stack_push(stack); stack_peek(stack); stack_search(sta

15th Sep 2021, 1:11 PM
Tanu Jain
1 Antwort
+ 2
Once u popped the value from stack, the stack is empty. So again push value to the stack to perform other operations
15th Sep 2021, 1:13 PM
Parith (Faree)
Parith (Faree) - avatar