1

嘿,我目前卡在我的 DoublyLinkedList 的反向方法上。除了反向方法之外,一切都运行良好(不知何故)。我没有收到任何错误 -System.out.println(list.reverse())根本没有输出。

有什么建议么?非常感谢您提前。:)

好的:我现在已经编辑了我的代码。到目前为止一切正常。但是,递归方法只是以相同的顺序打印列表,而不是实际反转它。

更新代码:

public class DoublyLinkedStringList {

private String content;
private DoublyLinkedStringList prev;
private DoublyLinkedStringList next;

public DoublyLinkedStringList(String info) {
    content = info;
    prev = null;
    next = null;
}

private DoublyLinkedStringList(String content, DoublyLinkedStringList prev, DoublyLinkedStringList next) {
    this.content = content;
    this.prev = prev;
    this.next = next;
}

public DoublyLinkedStringList prepend(String info) {
    DoublyLinkedStringList newNode = new DoublyLinkedStringList(info);
    prev = newNode;
    newNode.next = this;

    return newNode;
}

public DoublyLinkedStringList delete(int index) {
    DoublyLinkedStringList curr = this;

    if (index == 0) {
        next.prev = null;
        return next;
    }

    for (int i = 0; i < index; i++) {
        curr = curr.next;
    }

    curr.prev.next = curr.next;
    if (curr.prev.next != null) {              
        curr.prev.next.prev = curr.prev;
    }
    return this;
}

public DoublyLinkedStringList reverse() {
    DoublyLinkedStringList currNode = this;

    while (currNode != null) {
        DoublyLinkedStringList temp = currNode.next;
        currNode.next = currNode.prev;
        currNode.prev = temp;

        if (currNode.prev != null) {
            currNode = currNode.prev;
        }
    }

    return this;
}

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();

    for (DoublyLinkedStringList currNode = this; currNode != null; currNode = currNode.next) {
        sb.append(currNode.content);
        if (currNode.next != null) {
            sb.append(", ");
        }
    }
    return sb.toString();
}

public static void main(String argv[]) {
    DoublyLinkedStringList list = new DoublyLinkedStringList("Testliste");
    list = list.prepend("6");
    list = list.prepend("5");
    list = list.prepend("4");
    list = list.prepend("3");
    list = list.prepend("2");
    list = list.prepend("1");
    list = list.prepend("0");

    list = list.delete(1);
    System.out.println(list);

    list = list.reverse();
    System.out.println(list);
}

}

4

6 回答 6

2

您的设计将遇到的问题之一是,当您反转列表时,头部变为尾部,尾部变为头部。但是客户端指向的是头部,而不是尾部。即使您 100% 正确执行此操作,您也无法更改客户端的引用。您要做的是将作为对象的列表的概念与构成该对象的节点分开(目前您已将这两个概念组合在一起,因为节点是列表,反之亦然)。通过将它们分开,对列表的引用总是相同的,无论其中有什么、顺序等。列表包含头和尾引用,节点只包含下一个/上一个。现在,您列表中的每个节点都有头和尾,如果您不这样做,可能会弹出令人讨厌的错误 t 每当头/尾发生变化时替换每个引用(即前置、删除或反转)。如果您将这两个实例移出每个节点,那么您不必对更改列表进行太多维护。我认为,如果您这样做,那么您会发现实现反向要容易得多。

你的错误正是我要说的问题。最后你返回这个,那么客户端的引用就是头部(即这个)。但是,在迭代并反转所有内容之后,头部现在是尾部,因此您通过返回 this 返回了新尾部。而尾部的 toString() 什么都不是。

于 2012-12-16T19:48:12.727 回答
1

通常我会实现接口Iteratable并使用 anIterator来反转列表,但我的修订版与您当前的模型保持一致。Node我将'sgetNext()和方法的返回类型更改getPrev()为依赖于forward变量。现在列表在“反转”时永远不会改变链接,但它通过变量getNext()getPrev()行为以相反的顺序遍历。

IDEONE 代码链接

考虑这个编辑:

class DoublyLinkedStringList {

private Node head, tail;
boolean forward;

/**
 * Diese Klasse repraesentiert einen Knoten in der Doubly Linked List der
 * Klasse
 * <code>DoublyLinkedStringList</code>
 *
 */
private class Node {
    private String content;
    private Node next;
    private Node prev;

    public Node(String content) { this.content = content; }

    public Node(String content, Node next) {
        this.content = content;
        if(forward) { this.next = next; }                     //EDITED
        else        { this.prev = next; }                     //EDITED
    }

    public Node getNext() { return (forward) ? next : prev; } //EDITED
    public Node getPrev() { return (forward) ? prev : next; } //EDITED

    public void setNext(Node next) {
        if(forward) { this.next = next; }                     //EDITED
        else        { this.prev = next; }                     //EDITED
    }

    public void setPrev(Node prev) {
        if(forward) { this.prev = prev; }                     //EDITED
        else        { this.next = prev; }                     //EDITED
    }
}

public DoublyLinkedStringList() {
    this.head = null;
    this.tail = null;
}

public Node prepend(String info) {
    Node newNode = new Node(info);
    newNode.setPrev(null);
    newNode.setNext(getHead());
    if(newNode.getNext()!=null) { 
      newNode.getNext().setPrev(newNode);                     //EDITED
    } 
    if(forward) { head = newNode; }                           //EDITED
    else        { tail = newNode; }                           //EDITED
    if(getTail() == null) {                                   //EDITED
      if(forward) { tail = newNode; }                         //EDITED
      else        { head = newNode; }                         //EDITED
    }
    return head;
}

public Node delete(int index) {
    Node currNode = getHead();
    int count = 0;

    if (index == 0) {
        if(forward) { head = head.next; }                     //EDITED
        else        { tail = tail.prev; }                     //EDITED
        return head;
    }

    while (currNode != null) {
        if (count + 1 == index) {
            currNode.next.prev = currNode.prev; 
            currNode.prev.next = currNode.next;               //EDITED
            break;
        }
        currNode = currNode.getNext();                        //EDITED
        count++;
    }
    return currNode;
}

private Node next() {
    Node currNode = head;

    if (forward) {
        return currNode.getNext();
    } else {
        return currNode.getPrev();
    }
}

public Node getHead() { return (forward) ? head : tail; }     //EDITED
public Node getTail() { return (forward) ? tail : head; }     //EDITED
public DoublyLinkedStringList reverse() { forward = !forward; return this; }

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    //EDITED LOOP STRUCTURE
    for (Node currNode = getHead(); currNode != null; currNode = currNode.getNext()) {
        sb.append(currNode.content);
        if (currNode.getNext() != null) {
            sb.append(", ");
        }
    }
    return sb.toString();
}

public static void main(String argv[]) {
    DoublyLinkedStringList list = new DoublyLinkedStringList();
    list.prepend("6");
    list.prepend("5");
    list.prepend("4");
    list.prepend("3");
    list.prepend("2");
    list.prepend("1");
    list.prepend("0");
    list.delete(3);
    System.out.println(list);
    System.out.println(list.reverse());
}
}
于 2012-12-16T21:37:29.127 回答
0

这只是我的解决方案。不幸的是,我没有更多时间来做解释性说明。

public class DoublyLinkedStringList {
    private String info;
    private DoublyLinkedStringList prev;
    private DoublyLinkedStringList next;

public DoublyLinkedStringList(String pInfo)
{
    info = pInfo;
    prev = null;
    next = null;
}

private DoublyLinkedStringList(String pInfo, DoublyLinkedStringList pPrev, DoublyLinkedStringList pNext)
{
    info = pInfo;
    prev = pPrev;
    next = pNext;
}

public DoublyLinkedStringList prepend(String info)
{
    DoublyLinkedStringList n = new DoublyLinkedStringList(info);
    prev = n;
    n.next = this;

    return n;
}

public DoublyLinkedStringList delete(int index)
{   
    if (index == 0)
    {
        next.prev = null;
        return next;
    }

    DoublyLinkedStringList d = this;

    for (int i = 0; i<index; i++)
        d = d.next;

    // d is now the node which should be deleted

    // after delete(x) "next" schould be on pos x

    d.prev.next = d.next;   // take the next of the prev and set the new next to the next of d

    if (d.prev.next != null)    // if the next of d was not set to null, it must get to know his new prev (d's prev)
        d.prev.next.prev = d.prev;

    return this;
}


public DoublyLinkedStringList reverse() // moe or less less similar to my implementation in IntList.java
{
    DoublyLinkedStringList oldLast = getLast();
    next.reverse(this);
    prev = next;
    next = null;
    return oldLast;
}

public void reverse(DoublyLinkedStringList last)
{
    if (next != null)
        next.reverse(this);
    prev = next;
    next = last;
}

public DoublyLinkedStringList getLast()
{
    if (next == null)
        return this;
    return next.getLast();
}

@Override
public String toString()
{
    String r = "";

    for (DoublyLinkedStringList i = this; i != null; i = i.next)
    {
        r += i.info;
        if (i.next != null)
            r += ", ";
    }
    return r;
}

public String reverseToString() // uses prev; just for testing issues :)
{
    String r = "";

    for (DoublyLinkedStringList i = getLast(); i != null; i = i.prev)
    {
        r += i.info;
        if (i.prev != null)
            r += ", ";
    }
    return r;
}

public static void main(String argv[])
{
    DoublyLinkedStringList list = new DoublyLinkedStringList("Test");
    list = list.prepend("6");
    list = list.prepend("5");
    list = list.prepend("4");
    list = list.prepend("3");
    list = list.prepend("2");
    list = list.prepend("1");
    list = list.prepend("0");
    list = list.delete(1);
    System.out.println(list);
    System.out.println(list.reverseToString()+"\n");

    list = list.reverse();
    System.out.println(list);
    System.out.println(list.reverseToString());

    list = list.delete(6);
    list = list.delete(0);
    System.out.println(list);
    list = list.reverse();
    list = list.prepend("1");
    System.out.println(list);
}
于 2012-12-16T21:45:43.403 回答
0

由于您有一个 DoublyLinkedStringList 作为返回类型,我认为您想返回一个对象。在这种情况下,我建议您循环访问您的对象并使用prepend您已经实现的方法构建一个新列表(无论如何都有其他错误)。您可以从一个空列表开始,并且在扫描原始对象时,将当前元素添加到前面。

否则,如果你想“就地”反转列表,你应该 return void,用最后一个元素更改头部,并且,由于是双链接的,你应该做任何其他事情,因为在两个方向都有指向节点的指针。

于 2012-12-16T19:58:19.227 回答
0

试试这个反向方法:

public class DoublyLinkedList {
  Node first, current;
  boolean forward;
      //constructors... methods...

  private Node next() {
        if(forward) return current.next();
        else return current.previous();
  }

  public void reverse() {
    while(true) {
          if(next() == null) {
        first = current;
        forward = !forward;
        return;
      }
      current = next(); 
    }
  }
}
于 2012-12-16T19:59:07.710 回答
0

你也只需要设置头部和尾部。那么它应该可以工作。但请参阅 chubbsondubs 的答案以获得进一步的改进!

于 2012-12-16T19:50:07.890 回答