【问题标题】:Having issues traversing Doubly Linked List in reverse反向遍历双向链表时遇到问题
【发布时间】:2013-12-05 04:41:55
【问题描述】:

我真的很难为此修复我的代码。我创建了一个双向链接列表,我试图反向遍历。

有什么想法吗?

这是我的代码:Node.java:

public class Node {
String data;
Node next;


public Node(String data) {
    super();
    this.data = data;
    this.next = null;
}

public String getData() {
    return data;
}

public void setData(String data) {
    this.data = data;
}

public Node getNext() {
    if (this.next != null)
        return this.next;
    else
        return null;
}

public void setNext(Node a) {
    this.next = a;
}

@Override
public String toString() {          //CHECK
    if(data== null){
        return "null";
    }
    else
        return data  + "-->";
}

}

这是第二个类“DNode.java”:

public class DNode extends Node {

Node prev;

public DNode(String data) {
    super(data);
    this.prev = null;
}

public Node getPrev() {
    return prev;
}

public void setPrev(Node prev) {
    this.prev = prev;
}

}

最后,这里是DoublyLinkedList.java:(从另一个类“Linked List”重写“add”和“remove”方法)

公共类 DoublyLinkedList 扩展 LinkedList {

DNode head, tail,current; 

public DoublyLinkedList() {

    this.head = this.tail = this.current = null; 
}



public void add(DNode a) {   //CHECK
    if (this.head == null) {
        this.head = tail = current= a;
        this.head.prev = null;
    }
    else{
        //a.setPrev(this.current);
        this.current.next= a;
        a.setPrev(this.current);
        this.current = this.tail = a;
    }
}
@Override
public void remove(String removestring) { 
    this.current = head;
    this.current.prev = head;
    while (this.current.getData() != removestring) {

        if (this.current.next == null) {

            System.out.println("not found");
            return;
        } else {
            this.current.prev = this.current;
            this.current = (DNode) current.next;
        }
    }
    if (this.current == head) {

        head = (DNode) head.next;

    } else {
        this.current.prev.next = (DNode) this.current.next;

    }
}



public void printList() {
    DNode temp = this.head;
    while (temp != null) {
        System.out.println(temp);
        temp = (DNode) temp.getNext();
    }

}

    public void reverseList(){
            this.current = this.tail;
    this.current.setNext(this.current.getPrev());
    this.current.setPrev(null);
    this.current = (DNode) this.current.getNext();


    while(this.current.getPrev()!= null){
        if(this.current.getNext() == null){
            this.current.setNext((this.current).getPrev()); 
            this.current.setPrev(null);
            this.current = (DNode)this.current.getNext();
        }
        else{
            DNode tempprev = (DNode) this.current.getNext();
            this.current.setNext(this.current.getPrev()); 
            this.current.setPrev(tempprev);
            this.current = (DNode) this.current.getNext();
        }
        DNode temp2 = this.tail;
        this.head = this.tail;
        this.tail = temp2;
    }

}

我可以向前打印列表,但向后打印我会陷入无限循环。有什么想法吗?

谢谢!

【问题讨论】:

  • 为什么你会有两个 Node 类??
  • 这是作业。我的教授分配了它,后来意识到这没有什么意义......但我们仍然必须这样做......这很痛苦

标签: java doubly-linked-list


【解决方案1】:

好吧,我们已经有了前进的方法。因为这是双重链接的,所以我们可以逐行转换此代码,而不是移动下一个(向前)我们移动上一个(向后)。我们也从尾部而不是头部开始。

这是您打印转发的旧方法:

public void printList() {
    DNode temp = this.head;
    while (temp != null) {
        System.out.println(temp);
        temp = (DNode) temp.getNext();
    }

}

这可能是您向后打印的新方法:

public void printBackwardsList() {
    DNode temp = this.tail;
    while(temp != null) {
        System.out.println(temp);
        temp = (DNode) temp.getPrev();
    }
}

请注意,它们几乎完全相同,只是我们将 tail 替换为 head,将 getNext 替换为 getPrev。

【讨论】:

  • 这让事情变得容易多了。我刚开始学习基本的数据结构,例如单链表和双链表。我知道这是一个非常基本的问题。感谢您的回答!
  • 没问题,很高兴为您提供帮助。向后思考非常困难,我们的思想不是那样做的,但向前思考很容易。请按绿色复选标记选择这三个答案之一,告诉其他人这是正确的解决方案:)
  • @VineetKosaraju 赞成以友好的方式回答一个非常基本的问题。
【解决方案2】:

您的类模型似乎过于复杂。您根本不需要 DNode 类来执行此操作。你的反向打印列表的方法应该和正常打印列表的方法一样简单

public void printListReverse() {
    Node temp = this.tail;
    while (temp != null) {
        System.out.println(temp);
        temp = temp.getPrevious();
    }
}

假设您正确构建和维护列表。

【讨论】:

  • 我认为他的 Node 没有 getPrevious 方法,这就是他使用 DNode 的原因。
  • 然后他可以在Node上添加那个方法
  • @AmirAfghani 因为这是家庭作业,这很可能不是一个选择
  • 太糟糕了——很抱歉你的老师不知道如何对数据建模。如果是我,我会和教授争论给定的课程
  • 谢谢!这很奇怪,因为额外的不必要的类。这绝对是一种更简单的处理方式。
【解决方案3】:

while(this.current.getPrev()!= null)

替换为

while(this.current.getPrev()!= head)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-17
    • 2015-09-24
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多