简体   繁体   中英

How to access different element of each node in the linked-list

I Have a linked-list of n nodes and each nodes holds more than one element. I am trying to write a method that allows me to search for a node and another method that allows me to search for an element inside the node. I cant figure out how am I suppose to access the inside elements of the nodes of linked list. So I guess what I really want to know is that how do you refer/access each individual element with a node of a linked-list?

Trying to create a program that allows the creation of a linked-list where the number of nodes within that linked-list is user dependent. The list should allow searching for nodes and elements and should also be sorted.

    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();

        }
      }
    }

You can only do this if you have same number of element in list of a node. As single node use struct which is declared only once so it's memory has been already decided. So you can't increase the size of node at run-time. Below i have mentioned C++ code.

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

Simply you can do this. Point temp node to head node and then Iterate temp.

System.out.println(temp.your_element_name);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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