支持泛型和迭代的背包、队列和栈的链表实现

优点

链表的使用达到了最优设计的目标。

  • 它可以处理任意类型的数据;
  • 所需的空间总是和集合的大小成正比;
  • 操作所需的时间总是和集合的大小无关。

import java.util.Iterator;

public class Stack<Item> implements Iterator<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 Iterator<Item> iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator<Item> {
        private Node current = first;

        public boolean hasNext() {
            return current != null;
        }

        public void remove() {
            // do nothing
        }

        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}

队列

import java.util.Iterator;

public class Queue<Item> implements Iterator<Item> {
    private Node first;     // 指向最早添加的结点的链接
    private Node last;        // 指向最近添加额结点的链接
    private int N;            // 队列中的元素数量

    private class Node {    //定义了结点的嵌套类
        Item item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public void size() {
        return N;
    }

    public void enqueue(Item item) {     // 向表尾添加元素
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;

        if(isEmpty()) {
            first = last;
        } 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 void remove() {
            // do nothing
        }

        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}

import java.util.Iterator;

public class Bag<Item> implements Iterator<Item> {
    private Node first;     // 链表的首结点

    private class Node {
        Item item;
        Node next;
    }

    public void add(Item item) {    // 和Stack的push()方法完全相同
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }

    public Iterator<Item> iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator<Item> {
        private Node current = first;

        public boolean hasNext() {
            return current != null;
        }

        public void remove() {
            // do nothing
        }

        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }
    }
}