ITeye博客

堆栈浅析

sunwch · 2010-05-18 14:48 · javase · 编程语言 · Markdown

public class Stack {

long[] stackArr;
int nEleme;
int top;

// initialization
public Stack(int n){
	this.nEleme = n;
	stackArr = new long[this.nEleme];
	top = -1;
}

// isEmpty
public boolean isEmpty(){
	return (top == -1) ? true : false;
}

// push
public void push(int value){
	if(isFull())
		return;
	stackArr[++top] = value;
}

// pop
public long pop(){
	if(top < 0)
		return 0L;
	return stackArr[top--];
}

public int getSize(){
	return nEleme;
}

public boolean isFull(){
	return (top == nEleme) ? true : false;
}

public static void main(String[] args){
	Stack sk = new Stack(5);
	sk.push(10);
	sk.push(20);
	sk.push(30);
	sk.push(40);
	sk.push(50);
	System.out.println(sk.isEmpty() + "\tSize=" + sk.getSize());
	int size = sk.getSize();
	for(int i=0;i<size;i++){
		System.out.print(sk.pop() + "\t");
	}
}

} 其为单通道数据输出,顺序为后进先出(LIFO)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。