-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMyStack.java
53 lines (43 loc) · 988 Bytes
/
MyStack.java
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
package midterm;
public class MyStack {
int[] info;
int top;
public MyStack(int size) {
info = new int[size];
}
public void push(int info) {
if (!isFull()) {
this.info[top++] = info;
} else {
System.out.println("Stack is full!");
}
}
public int pop() {
if (!isEmpty()) {
return info[--top];
} else {
System.out.println("Nothing to pop from stack!");
return Integer.MAX_VALUE;
}
}
public int peek() {
if (!isEmpty()) {
return info[top - 1];
} else {
System.out.println("Nothing to peek at stack!");
return Integer.MAX_VALUE;
}
}
public boolean isEmpty() {
return top == 0;
}
public boolean isFull() {
return top == info.length;
}
public int size() {
return top;
}
public void clear() {
top = 0;
}
}