简体   繁体   English

插入java中的双循环链表

[英]Insertion in a doubly circular linked list in java

For homework I am supposed to create a circularly linked list using nodes and pointers. 对于家庭作业,我应该使用节点和指针创建循环链接列表。
This is my node class 这是我的节点类

class Node implements Serializable {
    public String theName; //the wrapped name
    public Node next;      //the next node in the sequence
    public Node prev;      //the previous node in the sequence

    public Node(Node p, String s, Node n){
        prev = p;
        theName = s;
        next = n;
    }
}

I am trying to insert strings (names) at the front of the list and then print them out using the traverse method. 我试图在列表的前面插入字符串(名称),然后使用遍历方法将它们打印出来。
So far these are my traverse and insert methods... 到目前为止,这些是我的遍历和插入方法......

public class ListImpl /*implements List*/ {
    Node list;

    public ListImpl() {
        list = new Node(list, null, list);
    }

    public void insert(String s) {


        if (list == null) {
            list = new Node(list, s, list);
        } else {
            list = new Node(list.prev, s, list);
            list.next.prev = list;
            list.next.next = list;



        }
    }

    public void traverse(ASCIIDisplayer out) {

        Node p = new Node(null, null, null);
        p = list;
        if (list != null) {

            while(true) {
                out.writeString(p.theName);
                p = p.next;
            }
        } else {
            throw new EmptyListException();
        }
    }
}

I understand that my traverse method will me a continuous loop and I have to figure out how to make it stop when it gets back to the beginning of the list, but that is not my problem. 我知道我的遍历方法将是一个连续的循环,我必须弄清楚当它回到列表的开头时如何停止,但这不是我的问题。
I believe my problem lies in the insert method because my output is not as expected when I run this code (the main): 我相信我的问题在于insert方法,因为当我运行此代码(主要)时,我的输出不是预期的:

public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        ASCIIDisplayer a = new ASCIIDisplayer();
        ListImpl List;

        List = new ListImpl();
        List.insert("Steve");
        List.insert("Kuba");
        List.insert("Taylor");
        List.insert("Jane");

        List.traverse(a);
    }
}

The output I get is: Taylor Jane Taylor Jane Taylor Jane Taylor Jane... repeated. 我得到的输出是:泰勒简泰勒简泰勒简泰勒简......重复。
I expected the output to be: Steve Kuba Taylor Jane Steve Kuba Taylor Jane...repeated. 我预计输出会是:史蒂夫库巴泰勒简史蒂夫库巴泰勒简...重复。

This is why I think that the problem is in the insert method, my pointer must be pointing to the wrong nodes but I just can't figure out what I did wrong. 这就是为什么我认为问题出在insert方法中,我的指针必须指向错误的节点,但我无法弄清楚我做错了什么。

Sorry for the long question, hopefully there is enough information for you to help me! 很抱歉这个问题很长,希望有足够的信息帮助我! Thanks in advance! 提前致谢!

Replace this: 替换这个:

    list = new Node(list.prev, s, list);
    list.next.prev = list;
    list.next.next = list;

with this: 有了这个:

    Node tmpList = new Node(list.prev, s, list);
    list.next.prev = tmpList;
    list.prev.next = tmpList;
    list = tmpList;

As for figuring out when you repeat a node, you can do that by saving the first node and comparing if it's the same as the next node, when that happens you're reached the end of the list. 至于确定何时重复一个节点,你可以通过保存第一个节点并比较它是否与下一个节点相同来实现,当发生这种情况时,你就到达了列表的末尾。

Also, when you insert the first node you do this: 此外,当您插入第一个节点时,您执行此操作:

if (list == null) {
    list = new Node(list, s, list);

In that case both of the list parameters being passed on to your constructor are null , you should fix that either by making a new constructor that only takes the String value and then references itself or by setting prev and next manually. 在这种情况下,传递给构造函数的两个list参数都为null ,您应该通过创建一个只接受String值然后引用自身的新构造函数或手动设置prevnext来修复它。

Here's the constructor solution: 这是构造函数解决方案:

class Node implements Serializable {

public String theName; //the wrapped name
public Node next;      //the next node in the sequence
public Node prev;      //the previous node in the sequence

public Node(Node p, String s, Node n){
    prev = p;
    theName = s;
    next = n;
}
public Node(String s){
    prev = this;
    theName = s;
    next = this;
}}

You do have an issue in your insert method: 您的插入方法确实存在问题:

    list.next.prev = list;
    list.next.next = list;

Anything seem asymmetic there? 有什么看起来像asymmetic吗?

Unrelated, but think about the questions: can list ever be null ? 不相关,但想想问题:可以list永远null吗? Should it be? 应该是吗?

This part of your insert looks incorrect 插入的这一部分看起来不正确

} else {
    list = new Node(list.prev, s, list);
    list.next.prev = list;
    list.next.next = list;

The last line should be: 最后一行应该是:

    list.prev.next = list

仔细查看insert函数中的else分支,并考虑是否确实要设置list.next.next=list

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM