-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
45 lines (40 loc) · 848 Bytes
/
ArrayStack.java
File metadata and controls
45 lines (40 loc) · 848 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
package com.briup.hsp.shujijiegou.stack;
public class ArrayStack {
private int maxSize;//栈的大小
private int[] stack;//数组模拟栈 数据放在该数组
private int top = -1;//表示栈底
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
public boolean isFull() {
return top == maxSize-1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int value) {
if(isFull()) {
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
public int pop() {
if(isEmpty()) {
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
public void list() {
if(isEmpty()) {
System.out.println("栈空");
}
for(int i=top;i>=0;i--) {
System.out.printf("stack[%d]=%d\n",i,stack[i]);
}
}
}