How does this Linked List work?

Hi.

I wonder how this linked list work

import java.util.Scanner;

publicclass LinkedList{

publicstaticvoid main(String[] args){

Scanner in =new Scanner(System.in);

Elem front =null;// First element of list.

Elem back =null;// Last element of list.

while (in.hasNext()){

String word = in.next();

Elem e =new Elem();// Create a new list element.

e.data = word;// Set the data field.

if (front ==null){

front = e;// Back element will be set below.

}else{

back.next = e;// Link last elem to new element.

}

back = e;

}

System.out.println("*** Print words in order of entry");

Elem curr = front;

while (curr !=null){

System.out.println(curr.data);

curr = curr.next;

}

}

}

class Elem{

public Elem next;// Link to next element in the list.

public String data;// Reference to the data.

}

Does anyone know how this allocates memory?

I feel strange because it's just front = e

and back = e

but it never says anything like front.next = back or something..

Can anyone help me on this?

[2470 byte] By [gomastera] at [2007-10-3 2:09:49]
# 1

> Does anyone know how this allocates memory?

Memory is allocated on this line:Elem e = new Elem();// Create a new list element.

> I feel strange because it's just front = e

> and back = e

> but it never says anything like front.next = back or something..

Well, it doesn't need to. Setting .next is taken care of after the initial element is added. It never explicitly says front.next = back because front.next is set when the second element is added. It's set implicitly, on this line:back.next = e;// Link last elem to new element.

It's implicit in that at that point front and back point to the same element.

paulcwa at 2007-7-14 19:08:43 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi PaulDo you mean that on second round of while back.next = e; is same as front.next = back.next = e;?I'm just assuming if it has only to the second round and no more
gomastera at 2007-7-14 19:08:43 > top of Java-index,Java Essentials,Java Programming...
# 3

No, I'm saying that on the second round, "front" and "back" point to the same object. So you don't have to assign .next on both.

Before the first round, "front" and "back" both point to null.

After the first pass through the loop, a new element is created with .next set to null, and both "front" and "back" point to that same object.

During the second pass, a new element is created, and back.next is set to that new object. But at this point, since back and front are the same object, this means that front.next is also set to that new object. Then, still during the second pass, back is set to that new object.

Now, front.next points to the same value as back.

From now on, front.next is never modified. Only back.next is modified, because new elements are added to the end of the list.

paulcwa at 2007-7-14 19:08:43 > top of Java-index,Java Essentials,Java Programming...
# 4
Thanks a lot :)I appreciate
gomastera at 2007-7-14 19:08:43 > top of Java-index,Java Essentials,Java Programming...