-3
public void addToHead(IntNode node) {
    IntNode temp = _head;
    _head = node;
    node.setNext(temp);
}

edit:I searched youtube, Nothig there about linkedlist and the heap When does the garbage collector wipe temp? I know it should, but can't see where.

I'm having a hard time understanding it. Intuitively I'd just write

_head = node;

I know it's not right, but I feel I need to understand what's going on there with the objects and addresses to get the point...

first line: I create a temp, and point it to the same adress the _head points
seconds line:Now head points to the adress node points, (node.next equals head.next)
third line: now node.next becomes temp..
am i right

trincot
  • 317,000
  • 35
  • 244
  • 286
  • `_head = node;` but then what happens to the old head? Think of chaining paperclips together. You can't just drop the existing chain every time you get a new clip. You have to attach the existing chain to the new paperclip. It works the same way here: you have to attach your existing node chain to the new head. – cf- Jun 16 '16 at 16:34
  • I understand that, but whats going on with the heap, and adresses.? temp stays there? every time I use this method another temp stays there? When it wipes out? how it works... – Arik Shalito Jun 16 '16 at 16:37

1 Answers1

0

This works as follows:

  1. node is on the stack and references the heap-object (A), which you want to insert
  2. _head is presumably a member of a heap-object (the list) and references the heap-object (B) that is currently at the head of the list.
  3. You create the reference temp on the stack, which then also references that object (B)
  4. You change the reference _head to the heap-object referenced by node, so _head now references (A)
  5. You now modify (A), which is referenced by both _head and node, to have its next-member reference (B)
  6. Both (A) and (B) are have a reference count of at least one all the time, so GC does not run on either one.
  7. After exiting the function the stack-memory for the references is reclaimed, and the heap object persist as long as they are referenced.
  8. After retuning from the function, as long as your list still exists, (A) is referenced by the list-head, (B) is referenced by (A)'s next-member, and so on and so on, so nothing gets claimed by the garbage collection.
midor
  • 5,487
  • 2
  • 23
  • 52
  • Thanks midor I got it, just couple more questions. -Basically every time I add a new node to a list, all those nodes have a temp pointing on them? I guese thats what confused me, ("temporary"). -If node and _head now references (A), Can I write _head._next = temp; ? – Arik Shalito Jun 16 '16 at 16:57
  • You can, but then you create a reference to yourself, and you will end up in a cycle when iterating. You create temp to keep a reference to (B), while you assign head to point to (A). Otherwise you would no longer have a way to find (B). The more logical thing to do would be to change the last line to _head.setNext(temp). Essentially what you want to do is go from this (B)->.... to this (A)->(B)->..... Your list is quite unusual because you pass in the list-node, and thus you can theoretically eliminate the temp, and do node.next = _head; _head = node; – midor Jun 16 '16 at 17:04