《java常用算法手册》 第二章 数据结构 栈 队列

 顺序栈的实现:

package LineStructure;
//参考 https://blog.csdn.net/icarus_wang/article/details/79561863

public class Stack<T> extends Object{
	
	int MaxLength = 10;
	
	T[] Array;
	
	int top = -1;//栈顶 初始为-1
	
	//默认构造
	public Stack(){
		this.MaxLength = MaxLength;
		this.Array = (T[]) new Object[MaxLength];//初始化数组
		this.top = -1;
	}
	
	//长度构造
	public Stack(int maxLength){
		this.MaxLength = maxLength;
		this.Array = (T[]) new Object[MaxLength];
		this.top = -1;
	}
	
	//push value
	public void push(T value) {
		
		this.Array[++top] = value;
	}
	
	//pop
	public T pop() {
		
		T result = Array[top];
		Array[top] = null;
		top--;
		return result;
		
	}
	
	//length
	public int getLength() {
		return this.top+1;
	}
	
	//getTop
	public T getTop() {
		return Array[top];
	}
	
	public Boolean isFull() {
		if (top == MaxLength) {
			return true;
		}else {
			return false;
		}
	}
}

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/weixin_41395565/article/details/83106461