-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample8.java
More file actions
35 lines (26 loc) · 803 Bytes
/
Example8.java
File metadata and controls
35 lines (26 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package applications.algorithms;
import datastructs.adt.ArrayStack;
/**
* Demonstrates how to reverse an array using a stack
* Such an approach takes O(N) time but it requires
* O(N) extra space
*/
public class Example8 {
public static void run(String[] args){
int[] array = new int[20];
for(int i=0; i<array.length; ++i){
array[i] = i;
}
ArrayStack<Integer> stack = new ArrayStack<Integer>(array.length);
for(int i=0; i<stack.size(); ++i){
stack.push(array[i]);
}
for(int i=0; i<stack.size(); ++i){
array[i] = stack.pop();
System.out.println("At position: "+i+" array[i] "+array[i]);
}
}
public static void main(String[] args){
Example8.run(args);
}
}