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

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

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

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

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

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

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

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

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

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

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

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

《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;
		}
	}
}

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

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

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

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

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

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

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

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

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

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

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

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

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

顺序队列实现:

package LineStructure;

//顺序队列 使用数组 缺点:数组占用空间 长度不可变
public class Queue<T> extends Object {
	// https://www.cnblogs.com/smyhvae/p/4793339.html
	//注意队头队尾的位置 
	private int Max_length = 10;

	private int front = 0;
	
	private int rear = 0;

	T[] queue;

	public Queue() {
		this.Max_length = Max_length;
		this.queue = (T[]) new Object[Max_length];
		this.front = 0;
	}

	public Queue(int length) {
		this.Max_length = length;
		this.queue = (T[]) new Object[Max_length];
		this.front = 0;
	}

	// 入队 增加队尾
	public void append(T value) {
		if (null != value && Max_length > 0) {
			queue[rear++] = value;
		}
	};

	// 出队 注意修改队头
	public T delete() {
		T value = queue[0];
		queue[0] = null;
		front++;
		return value;

	};

	// 队头
	public T getFront() {
		T value = queue[front];
		return value;

	};

	// isnull
	public boolean isEmpty() {
		return queue.length == 0;
	};
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(queue[front]);
		if(queue.length > 2) {
			for(int i = front+1; i < rear; i++) {
				sb.append(","+queue[i]);
			}
		}
		return sb.toString();
	}

}