在某些数据之后插入数据(双向链表)

时间:2010-11-13 15:50:47

标签: java linked-list

嘿,所以我想在输入数据之后插入一个数据(不是索引)。 我已经尝试了,但它总是在最后,我想插入的数据最终在链接列表的dront ...

  

** public static void insertAfter(Object o,Object c){

    Node newN = new Node();
    Node help = new Node();
    Node help2 = new Node();
    newN.data = o;
    help = head.next;
    if(isEmpty()){
        head = newN;
        newN.next=head;
        newN.prev=head;
    }
    else{
        do{
            help=help.next;
            System.out.println(help);
        }while(help.next!=head || !help.data.equals(c));
        help2 = help.next;
        newN.next = help2;
        help2.prev = newN;
        help.next=newN;
        newN.prev=help;
    }**
有人可以帮忙吗? 一堆!

3 个答案:

答案 0 :(得分:1)

我认为你应该尝试另一种结局条件:

while(help.next!=head && !help.data.equals(c));

顺便说一句,我只能建议你避免做...而没有严重的理由,并且使用吸气剂和制定者。

您的代码也应该有不同的结构。你为什么不编写一个只插入插入的私有方法,即你的最后5行?一切都会更具可读性和可重用性。

此外,您的变量需要清晰且有意义的名称。

答案 1 :(得分:1)

您要比较的对象是什么?如果它们不是字符串,那么你必须覆盖equals()方法才能得到正确的比较。

答案 2 :(得分:1)

自己动手

我开始修复你的解决方案但是在想要测试它时结束了写一个全新的实现......所以这里有:

public class DoubleLinkedList<T> {

    private class Node {
        private Node prev;
        private Node next;
        private T data;

        Node(T data) {
            this.data = data;
        }
    }

    Node head;

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

    public void insertAfter(T afterThis, T objectToAdd) {

        // cannot insert after in a empty list?!
        if(isEmpty())
            throw new NoSuchElementException("list is empty?");

        // find the node where we want to insert the element
        Node after = findNodeByObject(afterThis);

        // create the node and update the links
        addAfter(after, new Node(objectToAdd));
    }

    private void add(T objectToAdd) {
        if (isEmpty()) {
            head = new Node(objectToAdd);
            head.next = head;
            head.prev = head;
        }
        else {
            addAfter(head.prev, new Node(objectToAdd));
        }
    }

    private void addAfter(Node after, Node toAdd) {

        Node afterAfter = after.next;

        after.next = toAdd;
        afterAfter.prev = toAdd;

        toAdd.prev = after;
        toAdd.next = afterAfter;
    }

    private Node findNodeByObject(T object) {

        Node current = head;

        while (true) {
            if (current.data.equals(object))
                return current;

            if (current.next == head)
                break;

            current = current.next;
        }

        throw new NoSuchElementException("" + object);
    }

    @Override
    public String toString() {

        List<T> printList = new LinkedList<T>();

        Node current = head;

        while (true) {
            printList.add(current.data);

            if (current.next == head)
                break;

            current = current.next;
        }

        return printList.toString();
    }


    public static void main(String[] args) throws Exception {
        DoubleLinkedList<String> list = new DoubleLinkedList<String>();

        list.add("first");
        list.add("third");
        list.insertAfter("first", "second");

        System.out.println(list);
    }
}

扩展LinkedList

...并添加如下的insertAfter方法:

import java.util.LinkedList;
import java.util.ListIterator;

public class MyList<T> extends LinkedList<T> {

    private void insertAfter(T first, T second) {

        ListIterator<T> iterator = listIterator();
        while (iterator.hasNext()) {
            if (iterator.next().equals(first)) {
                iterator.add(second);
                return;
            }
        }

        throw new IndexOutOfBoundsException("Could not find " + first);
    }


    public static void main(String[] args) throws Exception {
        MyList<String> list = new MyList<String>();
        list.add("first");
        list.add("third");
        list.insertAfter("first", "second");

        System.out.println(list); // prints "[first, second, third]"
    }
}