161k views
1 vote
Populate a stack with ten random integers and empty it by showing that the order of the elements is reversed, from last to first.

CODE IN JAVA.

Thanks a Lot!

1 Answer

6 votes

Answer:

class Main {

public static void main(String args[]) {

Deque<Integer> stack = new ArrayDeque<Integer>();

Random rand = new Random();

for(int i=0; i<10; i++) {

int n = rand.nextInt(100);

System.out.printf("%02d ", n);

stack.push(n);

}

System.out.println();

while(stack.size() > 0) {

System.out.printf("%02d ", stack.removeFirst());

}

}

}

Step-by-step explanation:

example output:

31 18 11 42 24 44 84 51 03 17

17 03 51 84 44 24 42 11 18 31

User Sam Stern
by
7.0k points