下压(LIFO)栈(能够动态调整数据大小的实现)
import java.util.Iterator;
public class ResizingArrayStack<Item> implements Iterable<Item>{
private Item[] a = (Item[]) new Object[1];
private int N = 0;
public boolean isEmpty() { return N == 0; }
public int size() { return N; }
private void resize(int max) {
Item[] temp = (Item[]) new Object[max];
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() {
Item item = a[--N];
a[N] = null;
if(N > 0 && N == a.length/4) resize(a.length/2);
return item;
}
public Iterator<Item> iterator(){ return new ReverseArrayIterator(); }
private class ReverseArrayIterator implements Iterator<Item>{
private int i = N;
public boolean hasNext() { return i > 0; }
public Item next() { return a[--i]; }
public void remove() { }
}
}
下压堆栈(链表实现)
package wang.ex_1_3;
import java.util.Iterator;
public class Stack<Item> implements Iterable<Item>{
private Node first;
private int N;
private class Node{
Item item;
Node next;
}
public boolean isEmpty() { return first == null; }
public int size() { return N; }
public void push(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
N++;
}
public Item pop() {
Item item = first.item;
first = first.next;
N--;
return item;
}
public Item getTop() {
return first.item;
}
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>{
private Node current = first;
public boolean hasNext() { return current != null; }
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
public void remove() { }
}
}
先进先出队列
import java.util.Iterator;
public class Queue<Item> implements Iterable<Item>{
private Node first;
private Node last;
int N = 0;
private class Node {
Item item;
Node next;
}
public boolean isEmpty() { return first == null; }
public int size() { return N; }
public void enqueue(Item item) {
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if(isEmpty()) last = first;
else oldlast.next = last;
N++;
}
public Item dequeue() {
Item item = first.item;
first = first.next;
if(isEmpty()) last = null;
N--;
return item;
}
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>{
private Node current = first;
public boolean hasNext() { return current != null; }
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
public void remove() { }
}
}
背包
import java.util.Iterator;
public class Bag<Item> implements Iterable<Item>{
private Node first;
private int N;
private class Node{
Item item;
Node next;
}
public void add(Item item) {
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
N++;
}
public Iterator<Item> iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator<Item>{
private Node current = first;
public boolean hasNext() { return current != null; }
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
public void remove() { }
}
}