62,623
社区成员
发帖
与我相关
我的任务
分享public class LinStack
{
Node head; //堆栈头
int size; //结点个数
public void LinStack(){ //构造函数
head = null;
size = 0;
}
public void push(Object obj){ //入栈
head = new Node(obj,head); //新结点作为新栈顶
size++;
}
public Object pop() throws Exception{ //出栈
if(size == 0)
throw new Exception("堆栈已空!");
Object obj = head.element; //原栈顶数据元素
head = head.next; //原栈顶结点脱链
size --;
return obj;
}
public boolean notEmpty(){ //非空否
return head != null;
}
public Object getTop(){
return head.element;
}
}class IntStack {
private int top;
private int data[];
public IntStack() {
top = 0;
data = new int[100];
}
public IntStack(int n){
top = 0;
data = new int[n];
}
public void push(int val) {
data[top++] = val;
}
public int pop() {
return data[--top];
}
public int peek() {
return data[top-1];
}
}