使用陣列和連結串列的方式實現棧
阿新 • • 發佈:2019-01-04
使用 陣列和連結串列 的方式實現 棧
陣列實現棧:
public class Stack<Item> implements Iterable<Item> {
private Item[] a; // 陣列表示棧,棧頂在最大的下標。
private int n; // 棧內元素的個數
/**
* 初始化一個空棧
*/
public Stack() {
a = (Item[]) new Object[2];
n = 0;
}
/**
* 判斷棧內是否有元素
*/
public boolean isEmpty() {
return n == 0;
}
/**
* 返回棧內元素個數
*/
public int size() {
return n;
}
// 改變棧的大小
private void resize(int capacity) {
assert capacity >= n;
// 注意不能直接建立泛型陣列
Item[] temp = (Item[]) new Object[capacity];
for (int i = 0 ; i < n; i++) {
temp[i] = a[i];
}
a = temp;
// 也可以選擇下面這種方式改變陣列大小
// a = java.util.Arrays.copyOf(a, capacity);
}
/**
* 壓入元素
*/
public void push(Item item) {
//先判斷n的大小,如果棧滿則改變棧的大小
if (n == a.length) resize(2*a.length);
a[n++] = item;
}
/**
* 彈出並返回元素
*/
public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = a[n-1];
a[n-1] = null; //防止物件遊離
n--;
// 如果有必要則調整棧的大小
if (n > 0 && n == a.length/4) resize(a.length/2);
return item;
}
/**
* 返回但不彈出棧頂元素
*/
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return a[n-1];
}
/**
* 返回一個可以進行先進後出迭代的迭代器
*/
public Iterator<Item> iterator() {
return new ReverseArrayIterator();
}
// 用內部類實現迭代器介面,實現從棧頂往棧底的先進後出迭代,沒有實現remove()方法。
private class ReverseArrayIterator implements Iterator<Item> {
private int i;
public ReverseArrayIterator() {
i = n-1;
}
public boolean hasNext() {
return i >= 0;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
return a[i--];
}
}
/**
* 測試
*/
public static void main(String[] args) {
Stack<String> stack = new Stack<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) stack.push(item);
else if (!stack.isEmpty()) StdOut.print(stack.pop() + " ");
}
StdOut.println("(" + stack.size() + " left on stack)");
}
}
連結串列實現棧:
public class Stack<Item> implements Iterable<Item> {
private Node<Item> first; //棧頂節點
private int N; // 棧內元素數量
// 輔助類Node,用於形成連結串列
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
/**
* 初始化棧
*/
public Stack() {
first = null;
N = 0;
}
/**
* 判斷棧是否為空
*/
public boolean isEmpty() {
return first == null;
//return N == 0;
}
/**
* 返回棧內元素數量
*/
public int size() {
return N;
}
/**
* 壓入元素
*/
public void push(Item item) {
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
N++;
}
/**
* 彈出元素
*/
public Item pop() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
Item item = first.item; // 需彈出的元素
first = first.next; // 刪除頭節點
N--;
return item;
}
/**
* 返回但不彈出元素
*/
public Item peek() {
if (isEmpty()) throw new NoSuchElementException("Stack underflow");
return first.item;
}
/**
* 從棧頂到棧底列印元素
*/
public String toString() {
StringBuilder s = new StringBuilder();
for (Item item : this)
s.append(item + " ");
return s.toString();
}
/**
* 實現Iterable介面
*/
public Iterator<Item> iterator() {
return new ListIterator<Item>(first);
}
// 實現Iterator介面用於迭代,沒有實現remove方法
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> current;
//初始化時,current指向棧頂
public ListIterator(Node<Item> first) {
current = first;
}
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next() {
if (!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
/**
* 測試
*/
public static void main(String[] args) {
Stack<String> s = new Stack<String>();
while (!StdIn.isEmpty()) {
String item = StdIn.readString();
if (!item.equals("-")) s.push(item);
else if (!s.isEmpty()) StdOut.print(s.pop() + " ");
}
StdOut.println("(" + s.size() + " left on stack)");
}
}