I am using such linked list:

private Class Node
{
    public int Num1;
    public int Num2;
}

LinkedList<Node> list = new LnkedList<Node>();

Node n = new Node();
n.Num1 = 10;
n.Num2 = 100;
list.Add(n);

but now I don't know how to access Num1 and Num2 using my LinkedList. For example, I want to sort my linked list according to Num1 using Collections.sort(?) but I do not know how to specify that.

Recommended Answers

All 4 Replies

If you have a reference to a Node class object you can use that reference to access the public members of the Node class.
There are several versions of the sort() method. Use the one that takes a Comparator that you provide.

What exactly do you mean by a reference to a node class?

Node aRefToNode = new Node();

aRefToNode is a reference to an instance of a Node class object.

aRefToNode.num1
will access tthe num1 class member

yes do what NormR1 saud. for any class you have, for example your Node class, you can acess the data members by placing a "." without quotations after your object or you could always use a get method to return the data depending if your data is private, protected, or public

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.