棧(stack)又名堆棧,它是一種運算受限的線性表。其限制是僅容許在表的一端進行插入或者刪除運算。後進先出(Last In First Out)。git
棧中的數據操做主要有push(壓入)和pop(彈出)操做。github
實際上,棧就能夠用數組來實現,也能夠用鏈表來實現。用數組實現的叫作順序棧,用鏈表實現的叫作鏈式棧。數組
爲了簡單起見,咱們的棧不支持泛型。瀏覽器
public class StackBasedArray implements StackInterface{
private String[] values; // 存儲棧中元素的數組
private int capacity;// 棧的容量
private int count;// 棧中已有數據個數
// 初始化棧,容量肯定。
public StackBasedArray(int capacity) {
this.values = new String[capacity];
this.capacity = capacity;
this.count = 0;
}
/**
* 入棧操做
*
* @param value
* @return
*/
public Boolean push(String value) {
// 數組空間不夠了
if (capacity == count) {
return false;
}
values[count] = value;
count++;
return true;
}
/**
* 出棧操做
*
* @return
*/
public String pop() {
// 棧爲空,則直接返回null
if (0 == count) {
return null;
}
String tmp = values[count - 1];
--count;
return tmp;
}
@Override
public String toString() {
return "StackBasedArray{" +
"values=" + Arrays.toString(values) +
", capacity=" + capacity +
", count=" + count +
'}';
}
}
複製代碼
測試代碼:bash
StackBasedArray as = new StackBasedArray(10);
System.out.println(as);
Boolean r1 = as.push("000");
System.out.println(as + ",r1:" + r1);
for (int i = 1; i < 11; i++) {
boolean r2 = as.push("" + i + i + i);
System.out.println(as + ",r2:" + r2);
}
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
System.out.println("as.pop():" + as.pop() + "," + as);
複製代碼
輸出結果:符合預期app
StackBasedArray{values=[null, null, null, null, null, null, null, null, null, null], capacity=10, count=0}
StackBasedArray{values=[000, null, null, null, null, null, null, null, null, null], capacity=10, count=1},r1:true
StackBasedArray{values=[000, 111, null, null, null, null, null, null, null, null], capacity=10, count=2},r2:true
StackBasedArray{values=[000, 111, 222, null, null, null, null, null, null, null], capacity=10, count=3},r2:true
StackBasedArray{values=[000, 111, 222, 333, null, null, null, null, null, null], capacity=10, count=4},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, null, null, null, null, null], capacity=10, count=5},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, null, null, null, null], capacity=10, count=6},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, null, null, null], capacity=10, count=7},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, null, null], capacity=10, count=8},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, null], capacity=10, count=9},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=10},r2:true
StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=10},r2:false
as.pop():999,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=9}
as.pop():888,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=8}
as.pop():777,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=7}
as.pop():666,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=6}
as.pop():555,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=5}
as.pop():444,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=4}
as.pop():333,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=3}
as.pop():222,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=2}
as.pop():111,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=1}
as.pop():000,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=0}
as.pop():null,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=0}
as.pop():null,StackBasedArray{values=[000, 111, 222, 333, 444, 555, 666, 777, 888, 999], capacity=10, count=0}
複製代碼
使用鏈表來實現棧,push進的數據添加至鏈表的頭結點,pop數據時取的也是鏈表的頭結點,這樣就實現了棧的後進先出。代碼以下:ide
public class StackBasedLinkedList implements StackInterface {
private Node first;
/**
* 每次添加數據都向鏈表頭結點添加。
*
* @param value
* @return
*/
@Override
public Boolean push(String value) {
Node newNode = new Node(value, null);
if (null == first) {
first = newNode;
} else {
newNode.next = first;
first = newNode;
}
return true;
}
/**
* 每次獲取數據都從鏈表頭結點獲取。
*
* @return
*/
@Override
public String pop() {
if (null == first) {
return null;
} else {
String tmp = first.getValue();
first = first.next;
return tmp;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append('[');
if (first != null) {
Node curr = first;
while (curr != null) {
sb.append(String.valueOf(curr));
if (curr.next != null) {
sb.append(",").append(" ");
}
curr = curr.next;
}
}
sb.append("]");
return sb.toString();
}
private static class Node {
Node next;
String value;
public Node(String value, Node next) {
this.next = next;
this.value = value;
}
public String getValue() {
return value;
}
@Override
public String toString() {
return value;
}
}
}
複製代碼
測試代碼:測試
StackBasedLinkedList sbll = new StackBasedLinkedList();
System.out.println(sbll);
// 添加數據
for (int i = 0; i < 10; i++) {
sbll.push("" + i + i + i);
}
System.out.println(sbll);
// 獲取數據
for (int i = 0; i < 11; i++) {
System.out.println("sbll.pop():" + sbll.pop());
}
複製代碼
輸出結果:符合預期。ui
[]
[999, 888, 777, 666, 555, 444, 333, 222, 111, 000]
sbll.pop():999
sbll.pop():888
sbll.pop():777
sbll.pop():666
sbll.pop():555
sbll.pop():444
sbll.pop():333
sbll.pop():222
sbll.pop():111
sbll.pop():000
sbll.pop():null
複製代碼
項目中搜索SingleLinkedList便可。this
github傳送門 github.com/tinyvampire…
gitee傳送門 gitee.com/tinytongton…
參考: 如何實現瀏覽器的前進和後退功能?