简体   繁体   English

双向链接列表在列表的开头和结尾均带有空对象

[英]Doubly Linked List with null objects at the beginning and end of the list

I was trying to create a doubly linked list with null objects at the beginnig and end of the list. 我试图创建一个双向链接列表,在列表的beginnig和end处使用空对象。 What does null objects means at the beginning and end of the list. 空对象在列表的开头和结尾意味着什么。 Does creating firstNode =null and lastNode == null will solve this problem or it means something different? 创建firstNode = null和lastNode == null是否可以解决此问题,或者意味着有所不同? Any suggestions will be appreciated. 任何建议将不胜感激。

// Creating a doubly linked list.
    doubleLinkedList = new DoubleLinkedList();

class DoubleLinkedList {

    private NewLink firstNode;
    private NewLink lastNode;
    private NewLink rootNode;

    // Initializing values in the Constructor for DoubleLinkedList
    public DoubleLinkedList() {

        rootNode  = null;
        firstNode = null;
        lastNode  = null;

    }

}



class NewLink {

    public String  data;
    public NewLink nextPointer;
    public NewLink previousPointer;

    public NewLink(String id) {

        data = id;

    }

    // Overriding toString method to return the actual data of the node
    public String toString() {

        return "{" + data + "} ";

    }
}

You didn't provide enough information to determine if you were using the standard "LinkedList" collection, or implementing your own. 您没有提供足够的信息来确定您是使用标准的“ LinkedList”集合还是实现自己的集合。 You also didn't give any idea exactly what you're trying to do, or what's going wrong. 您也没有确切地知道您要做什么或出了什么问题。

ANYWAY: 无论如何:

Assuming you're interested in the standard LinkedList, here are some good tutorials: 假设您对标准的LinkedList感兴趣,下面是一些不错的教程:

And to your specific question: No. There's seldom any need to explicitly set anything to "null". 对于您的特定问题:不。几乎不需要将任何内容显式设置为“ null”。 Just create your container list, add stuff to it, and perform operations on the list items. 只需创建您的容器列表,向其中添加内容,然后对列表项执行操作即可。

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

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