单链表实施

时间:2015-04-11 22:32:19

标签: java singly-linked-list

好吧,我正试图通过我的教科书(Goodrich& Tamassia,Algorithm Design,2001)实现一个(单一)链接列表,到目前为止一直很好。

现在,我遇到的问题是,我无法正确测试它。例如,如果我通过insertFirst方法插入一个节点,我怎么还能够检索它以便能够使用它像swapElements这样的方法?

我想过通过元素工作,但是当我有相同元素的节点时,我会遇到问题。那么,这应该如何运作?我很抱歉,如果我的问题相对容易或模糊,那么请告诉我如何改进它,因为我对数据结构还不熟悉。

这是我的代码;

public class Node<E> implements Position<E> {

    private Node<E> next;
    private E element;

    public Node(Node<E> next, E element) {
        this.next = next;
        this.element = element;
    }

    public Node(E element) {
        this.element = element;
    }

    public void setNext(Node<E> next) {
        this.next = next;
    }

    public Node<E> getNext() {
        return next;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public E element() {
        return element;
    }

    public String toString() {
        return ("Element: " + element);
    }

}

public class SinglyLinkedListImp<E> implements List<E> {

    private Node<E> head;
    private int size;

    public SinglyLinkedListImp() {
        this.head = null;
        this.size = 0;
    }

    public Node<E> first() {
        return head;
    }

    public Node<E> last() {
        Node<E> current = head;
        while (current.getNext() != null) {
            current = current.getNext();
        }
        return current;
    }

    public boolean isFirst(Node<E> n) {
        return (head == n);
    }

    public boolean isLast(Node<E> n) {
        return (n.getNext() == null);
    }

    public Node<E> before(Node<E> n) {
        Node<E> current = head;
        while (current.getNext() != n) {
            current = current.getNext();
        }
        return current;
    }

    public Node<E> after(Node<E> n) {
        return n.getNext();
    }

    public Node<E> replaceElements(Node<E> n, E element) {
        Node<E> current = head;
        Node<E> previous = null;
        while (current != n) {
            previous = current;
            current = current.getNext();
        }
        Node<E> newLink = new Node<E>(current.getNext(), element);
        previous.setNext(newLink);
        return current;
    }

    public void swapElements(Node<E> n, Node<E> k) {
        E tmp = n.element();
        n.setElement(k.element());
        k.setElement(tmp);
    }

    public void insertFirst(E element) {
        head = new Node<E>(head, element);
        size++;
    }

    public void insertLast(E element) {
        if (head == null) {
            head = new Node<E>(head, element);
        } else {
            Node<E> current = head;
            while (current.getNext() != null) {
                current = current.getNext();
            }
            current.setNext(new Node<E>(null, element));
        }
        size++;
    }

    public void insertBefore(Node<E> n, E element) {
        Node<E> current = head;
        Node<E> previous = null;
        while (current.getNext() != n) {
            previous = current;
            current = current.getNext();
        }
        previous.setNext(n);
    }

    public void insertAfter(Node<E> n, E element) {
        Node<E> current = head;
        while (current != n) {
            current = current.getNext();
        }
        current.setNext(n);
    }

    public void remove(Node<E> n) {
        Node<E> current = head;
        Node<E> previous = null;
        while (current != n) {
            previous = current;
            current = current.getNext();
        }
        previous.setNext(current.getNext());
        size--;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return (size == 0);
    }

    public void display() {
        if (head == null) {
            System.out.println("Empty list.");
        } else {
            Node<E> current = head;
            while (current != null) {
                System.out.println(current.toString());
                current = current.getNext();
            }
        }
    }

}

请注意,SinglyLinkedListImp类尚未完全完成(如果列表为空,某些方法会出错)。 我不认为需要为这两个接口提供代码,但如果是这样,请告诉我。

1 个答案:

答案 0 :(得分:0)

在您的实现中,您设置了一些可用于迭代集合的方法(如getNext等)。我能想到的一个场景是在一次操作中检索了列表的任何元素,然后根据元素(如swapElements)对集合进行编辑。

我建议您做什么(并且可能会清楚地说明)是通过索引添加元素的检索方法: 方法get(int index)将返回放置在作为参数给出的索引上的元素。实际上,Java中的LinkedList集合标准API具有这样的方法。这背后的逻辑很简单:获取下一个节点,直到迭代周期数达到索引号。

更新:为了应用元素交换,显然Node涉及的MUST是列表的一部分,否则就没有意义了。还有人建议,swapElements可能基本上用于课堂目的,所以除非你有充分的理由,否则请将其声明为私有。