【问题标题】:How to access different element of each node in the linked-list如何访问链表中每个节点的不同元素
【发布时间】:2019-03-25 17:23:40
【问题描述】:

我有一个包含 n 个节点的链表,每个节点包含多个元素。我正在尝试编写一种允许我搜索节点的方法和另一种允许我搜索节点内元素的方法。我不知道我应该如何访问链表节点的内部元素。所以我想我真正想知道的是,您如何使用链表的节点来引用/访问每个单独的元素?

试图创建一个允许创建链接列表的程序,其中链接列表中的节点数量取决于用户。该列表应该允许搜索节点和元素,并且还应该进行排序。

    package nodelist;

    public class NodeList {

public int nodeid;
public int nodestate;
public int x_cord;
public int y_cord;
public int direction;

public NodeList next;

public NodeList(int nodeid, int nodestate, int x_cord, int y_cord, int direction){
    this.nodeid = nodeid;
    this.nodestate = nodestate;
    this.x_cord = x_cord;
    this.y_cord = y_cord;
    this.direction = direction;
}

public void display(){
    System.out.println("nodeid: "+nodeid + " state: " +nodestate+ " x: " +x_cord+ " y: " +y_cord+ " direction: " +direction);
}

//@Override
public String toString(){     
    return String.valueOf(this.nodeid); // Needed to convert int nodeid to string for printing
}

public static void main(String[] args) {
    // TODO code application logic here
    LinkList theLinkedList = new LinkList();

        // Insert Link and add a reference to the book Link added just prior
        // to the field next
        System.out.println("Enter the number of nodes to deploy");
                    int nodecount = 5;
                    int nodeid = 5000;
                    for(int i=0; i<nodecount;i++){
                        theLinkedList.insertFirstLink(nodeid, 0,0,0,0);

                        nodeid++;
                    }
                    /*
        theLinkedList.insertFirstLink("5000", 0,0,0,0);
        theLinkedList.insertFirstLink("5001", 1,1,1,1);
        theLinkedList.insertFirstLink("5002", 2,2,2,2);
        theLinkedList.insertFirstLink("5003", 3,3,3,3);
        */
        theLinkedList.display();

        System.out.println("Value of first in LinkedList " + theLinkedList.firstLink + "\n");

        // Removes the last Link entered

        theLinkedList.removeFirst();

        theLinkedList.display();

        //System.out.println(theLinkedList.find("The Lord of the Rings").bookName + " Was Found");

        //theLinkedList.removeNodeList("A Tale of Two Cities");

        System.out.println("\nA Tale of Two Cities Removed\n");

        theLinkedList.display();

}

    }

    public class LinkList {
// Reference to first Link in list
    // The last Link added to the LinkedList

    public NodeList firstLink; 

    LinkList(){

        // Here to show the first Link always starts as null

        firstLink = null;

    }

    // Returns true if LinkList is empty

    public boolean isEmpty(){

        return(firstLink == null);

    }

    public void insertFirstLink(int nodeid, int nodestate, int x_cord, int y_cord, int direction){

        NodeList newLink = new NodeList(nodeid, nodestate, x_cord, y_cord, direction);

        // Connects the firstLink field to the new Link 

        newLink.next = firstLink;

        firstLink = newLink;

    }

    public NodeList removeFirst(){

        NodeList linkReference = firstLink;

        if(!isEmpty()){

            // Removes the Link from the List

            firstLink = firstLink.next;

        } else {

            System.out.println("Empty LinkedList");

        }

        return linkReference;

    }

            public NodeList removeNodeList(){

        NodeList linkReference = firstLink;

        if(!isEmpty()){

            // Removes the Link from the List

            firstLink = firstLink.next;

        } else {

            System.out.println("Empty LinkedList");

        }

        return linkReference;

    }

    public void display(){

        NodeList theLink = firstLink;

        // Start at the reference stored in firstLink and
        // keep getting the references stored in next for
        // every Link until next returns null

        while(theLink != null){

            theLink.display();

            System.out.println("Next Link: " + theLink.next);

            theLink = theLink.next;

            System.out.println();

        }
      }
    }

【问题讨论】:

标签: java data-structures linked-list


【解决方案1】:

只有在节点列表中有相同数量的元素时才能执行此操作。 作为单节点使用仅声明一次的结构,因此它的内存已经确定。所以你不能在运行时增加节点的大小。 下面我提到了C++代码。

struct Node{
int list[5];
Node *node;
}

【讨论】:

  • 你提到的代码是用C语言编写的。我正在研究java。并且所有节点确实具有相同数量的元素。
  • 你也可以在 cpp 中使用 struct。抱歉,Java 帮不了你
【解决方案2】:

你可以做到这一点。 将 temp 节点指向头节点,然后迭代 temp。

System.out.println(temp.your_element_name);

【讨论】:

    猜你喜欢
    • 2018-10-02
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 2014-04-25
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多