堆栈(Stack)是一种常见的数据结构,符合后进先出(First In Last Out)原则,通常用于实现对象存放顺序的逆序。栈的基本操作有push(添加到堆栈),pop(从堆栈删除),peek(检测栈顶元素且不删除)。
第一种实现方式:普通数组实现
/**
* Created by Frank
*/
public class ToyStack {
/**
* 栈的最大深度
**/
protected int MAX_DEPTH = 10;
/**
* 栈的当前深度
*/
protected int depth = 0;
/**
* 实际的栈
*/
protected int[] stack = new int[MAX_DEPTH];
/**
* push,向栈中添加一个元素
*
* @param n 待添加的整数
*/
protected void push(int n) {
if (depth == MAX_DEPTH - 1) {
throw new RuntimeException("栈已满,无法再添加元素。");
}
stack[depth++] = n;
}
/**
* pop,返回栈顶元素并从栈中删除
*
* @return 栈顶元素
*/
protected int pop() {
if (depth == 0) {
throw new RuntimeException("栈中元素已经被取完,无法再取。");
}
// --depth,dept先减去1再赋值给变量dept,这样整个栈的深度就减1了(相当于从栈中删除)。
return stack[--depth];
}
/**
* peek,返回栈顶元素但不从栈中删除
*
* @return
*/
protected int peek() {
if (depth == 0) {
throw new RuntimeException("栈中元素已经被取完,无法再取。");
}
return stack[depth - 1];
}
}
第二种实现方式:deque双向队列
importjava.util.ArrayDeque;importjava.util.Deque;public classIntegerStack {private Deque data = new ArrayDeque();public voidpush(Integer element) {
data.addFirst(element);
}publicInteger pop() {returndata.removeFirst();
}publicInteger peek() {returndata.peekFirst();
}publicString toString() {returndata.toString();
}public static voidmain(String[] args) {
IntegerStack stack= newIntegerStack();for (int i = 0; i < 5; i++) {
stack.push(i);
}
System.out.println("After pushing 5 elements: " +stack);int m =stack.pop();
System.out.println("Popped element = " +m);
System.out.println("After popping 1 element : " +stack);int n =stack.peek();
System.out.println("Peeked element = " +n);
System.out.println("After peeking 1 element : " +stack);
}
}/*输出
After pushing 5 elements: [4, 3, 2, 1, 0]
Popped element = 4
After popping 1 element : [3, 2, 1, 0]
Peeked element = 3
After peeking 1 element : [3, 2, 1, 0]*/
ArrayDeque源码
//java.util.ArrayDeque的源码:
private transientE[] elements;private transient inthead;private transient inttail;/*此处存放e的位置是从elements数组最后的位置开始存储的*/
public voidaddFirst(E e) {if (e == null)throw newNullPointerException();
elements[head= (head - 1) & (elements.length - 1)] = e;//首次数组容量默认为16,head=(0-1)&(16-1)=15
if (head ==tail)
doubleCapacity();
}/*每次扩容都按插入的先后顺序重新放入一个新的数组中,最新插入的放在数组的第一个位置。*/
private voiddoubleCapacity() {assert head ==tail;int p =head;int n =elements.length;int r = n - p; //number of elements to the right of p
int newCapacity = n << 1;if (newCapacity < 0)throw new IllegalStateException("Sorry, deque too big");
Object[] a= newObject[newCapacity];
System.arraycopy(elements, p, a,0, r);
System.arraycopy(elements,0, a, r, p);
elements=(E[])a;
head= 0;
tail=n;
}publicE removeFirst() {
E x=pollFirst();if (x == null)throw newNoSuchElementException();returnx;
}publicE pollFirst() {int h =head;
E result= elements[h]; //Element is null if deque empty
if (result == null)return null;
elements[h]= null; //重新设置数组中的这个位置为null,方便垃圾回收。
head = (h + 1) & (elements.length - 1);//将head的值回退,相当于将栈的指针又向下移动一格。例如,12--〉13
returnresult;
}publicE peekFirst() {return elements[head]; //elements[head] is null if deque empty
}