使用 数组和链表 的方式实现 栈
数组实现栈:
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;
}
/**
* 压入元素
*/
public void push(Item item) {
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();
}
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;
private static class Node<Item> {
private Item item;
private Node<Item> next;
}
/**
* 初始化栈
*/
public Stack() {
first = null;
N = 0;
}
/**
* 判断栈是否为空
*/
public boolean isEmpty() {
return first == null;
}
/**
* 返回栈内元素数量
*/
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);
}
private class ListIterator<Item> implements Iterator<Item> {
private Node<Item> 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)");
}
}