0

my question is perhaps too simple: maybe that's the reason i didn't manage to find a written answer. i am trying to understand the way linked lists are built, and there is one thing i can't understand: usually, when i create an object of some kind of class, i need to give it a name, like that:

 cat kitten =new cat();

at the same time when i do that, the computer gives it an address, something like dfe@fggv3444. when i want to use the created object in some method, i address it by the name i gave it: in our case :"cat". the logic is that if i name 2 objects in the same name "cat", the computer wouldn't know which object to access- or perhaps, both. and now the question: when we use a linked list, the number of used nodes changes a lot throughout the program. so how this objects of node class type are getting their names? obviously they must have a name, but i am not there to give each node its name...

how it works?!

thanks in advance. i am sure that something in this question must be silly, but can't figure out what.

6
  • What you call a "name" is just a human-readable form of a memory address. If you have a linked list, you normally get hold on that list through a head node, i.e. the start of the list. From there on, you use the next-references of each node to jump from node to node. Commented Jun 11, 2018 at 18:55
  • You CANNOT -- repeat CANNOT -- give the same name to two objects... in the same scope. If one is in the main method, and the other one inside a method, then yes, you can do that, but never in the same scope. Commented Jun 11, 2018 at 18:56
  • A List (An arraylist or linkedlist) need to be iterated trough its elements, you don't need a reference on code for each position. Commented Jun 11, 2018 at 18:57
  • Tip: Go for arrays and loops before LinkedLists Commented Jun 11, 2018 at 18:58
  • 1
    Objects don't have names, variables do. Commented Jun 11, 2018 at 19:09

5 Answers 5

1

Each cat name is essentially (as @Turing85 pointed out in the comments) just a memory address to a cat instance in a human-readable format.

A linked list at its core is a set of nodes, each with a value and the next node (next being the variable that connects the nodes to form the list). Each cat would be referred to as simply node.value, where value is the "name" pointing to the address of the node's cat. Each node's next variable points to the next node, which has its own value.

You can't name two objects the same thing within the same scope - you can have cat1 and cat2 both of type cat, and each points to the memory address of a different cat. The reason linked lists can do this is because each node has its own scope, that no other node sees. Hence all nodes can have a next and a value, referred to as node.next and node.value.

      node1        +--------> node2        +--------> node3
+--------------+   |    +--------------+   |    +--------------+
| value = cat1*|   |    | value = cat2 |   |    | value = cat3 |
| next = node2-|---+    | next = node3-|---+    | next = null  |
+--------------+        +--------------+        +--------------+

*---> cat1
+--------------+
| name = "tom" |
| col = "grey" |  ...and likewise for the other nodes
| ............ |
Sign up to request clarification or add additional context in comments.

Comments

0

You cannot give the same name to any two variables in Java, within the same scope of visibility (while both variables still exist, and not trashed by the Java Garbage collector. more info : http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html), otherwise the compiler wouldn't know which value or object you're referring to.

To answer the second part of your question, an object exists as long as it has a reference (way to access the object). You are right to understand that obviously, an object added to the linked list will have to be accessed. Suppose the first node is named head, and you have to find a reference to the third node in a linked list. head.next.next will be a reference to the third node in the linked list, it does not necessarily have to have a name like 'head', but will exist as long as it has a reference.

Hope this answers your question!

2 Comments

thank you...if i understand you properly- when i delete the reference of a certain node by changing the _next data field to null or something else- this action would make the computer to delete the node? no trace of the information it had is left behind?
Yes. If you set head.next.next to null, or anything else, the third node won't have a reference and so, it will be completely deleted by the Java Garbage collector.
0

Java has a built in class for linked lists java.util.LinkedList When you instantiate this object ie.

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

You are creating a reference, or in your case name "list" to the object (your linked list)

The entire list has a name "list" which can act as a starting point for your traversal. There are no "names" for each node in the linked list. In java, you can traverse the list somewhat similar to how you would an array; through an index.

list.get(someIndex)

This method basically traverses the list until someIndex, and returns the node there. You can manually set a name for that node by doing;

Node node = list.get(someIndex)

But in general, they do not have a "name", instead you can access them through their index, or many other methods.

Check https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html for more details on the built in class

Comments

0

In linked list, each element is represented by nodes. Those nodes hold onto the other elements by their addresses in memory. This could be pseudo-name, but in reality it's just a way to represent where it is in memory.

What might make it confusing is that you do not define the object directly like you would do in this code.

Animal cow = new Animal();

In this instance we have assigned the address of new object's data in the variable cow. This allows us to easily access it.

In contrast to that, linked list node objects hold the address of the next node, but just like any other object, they are a contained as just that node's attribute.

Comments

0

A variable name is only for readability. Java compiler translates source code into "machine-readable" code, which contains no variable name. Values are stored in memory with specific addresses, such as 0x00000000 in hexdecimal.

Each node contains a value and the memory address of the next node. When you call cat.next();, your computer gets the address of the next node instead of a variable name, and it knows where this address is.

Note:

  1. Values may also be stored in register, cache, or disk. They also refer to addresses for data.
  2. Java LinkedList is actually doubly-linked, where each node contains address of its previous node as well. We use singly-linked list here for simplification.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.