-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStack_intArr.java
More file actions
49 lines (41 loc) · 896 Bytes
/
Copy pathStack_intArr.java
File metadata and controls
49 lines (41 loc) · 896 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
36
37
38
39
40
41
42
43
44
45
46
47
48
// Use int array to implement Stack
/**
* Created by xiaoyaoworm on 4/16/16.
*/
public class Stack {
int maxSize;
int[] arr;
int top;
public Stack(int size){
maxSize = size;
arr = new int[size];
top = -1;
}
public void push(int num){
if(top == arr.length - 1){
System.out.println("Stack is full, cannot push.");
} else {
top++;
arr[top] = num;
}
}
public int pop(){
if(top == -1){
System.out.println("Stack is empty, cannot pop.");
return -1;
}
else{
int res = arr[top];
arr[top] = 0;
top--;
return res;
}
}
public boolean isEmpty(){
if(top == -1) return true;
else return false;
}
public int peek(){
return arr[top];
}
}