-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStackArray.java
More file actions
56 lines (45 loc) · 1.07 KB
/
StackArray.java
File metadata and controls
56 lines (45 loc) · 1.07 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package techinterview;
public class StackArray {
private int size;
private int[] array;
private int top;
public StackArray(int size) {
this.size = size;
array = new int[size];
top = -1;
}
public StackArray() {
this(10);
}
public void push(int value) {
if (top < size - 1)
array[++top] = value;
}
public int pop() {
if (top > -1)
return array[top--];
return -1;
}
public int peek() {
if (top > -1)
return array[top];
return -1;
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == size - 1;
}
public static void main(String[] args) {
StackArray stack = new StackArray(10);
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
while (!stack.isEmpty())
System.out.print(stack.pop() + " ");
System.out.println();
}
}