• Post Reply Bookmark Topic Watch Topic
  • New Topic

understanding linked list program  RSS feed

 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to my understading of linked list. It consists of nodes each node has data, and a referece to the next node in the list. so by this i am tryindg to understand a program which is as follows



here is program that uses above program


so here like i said node is having data and a reference to next element to it. so in code form there is a class named Element which have data, next reference variable which will have the address of next element.

then we create a class with name LinkedList1 which has two Element type reference variables front, back(what is the need of them? what is purpose of LinkedList1 class). yeah it has methods that manages nodes that were added,fine.

list is the object of LinkedList1 class.
for the first statement of calling to add method with "abc" as argument, call will goes to the add method in LinkedList1 class then object is created for Element class and we are assigning obj to the data by e1.data=obj statement.
and my doubt starts from next statement i.e from if statement
it says if(front==null) it will assign e1 to front. what does it mean? i think for every call to add method object to Element class is created and front will be null rite?
and unser the else statement it has back.next=e1 what does this mean?
what does back=e1 means?
and the iterator method is also total confusing for me. this is my first understanding of linkedlist concept. answer me with little patience.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably understand that much better if you draw a diagram of the linked list. Use circles for the nodes, spots for the values, blue arrows for the links to next and red arrows for the links to previous. In a singly‑linked list, you can dispense with previous and won't need colours.
By the way, that iterator method is completely different from what you usually have as an iterator method.
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and one more question is that front, back reference variables will have first and last elements of the list right?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct, that front and back refer to the ends of the List. If it has ≤ 1 element, then front and back will point to the same reference, which might be null.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not just use the Java API java.util.LinkedList class?

Did you try compiling and running the code? What's the output?

If you are trying to understand the concept of linked list then that program simplifies things so much such that it only concerns with 2 nodes (front and back) at any given time.

The LinkedList1 program checks if the first node is null, if not put it the element at the first index of the (linked) list, else append it (indicated by the else part). After that this element also assigned to the back node. Making both front and back nodes the same element.

The iterator method just loop through the list.


From my analysis, first add "abc" will make the front element "abc" because initially front is null. Then back element becomes "abc" as well. At this point both front and back elements are "abc".

Next add "hello". Now is the front element null (the if statement)? No because it has "abc" from previous add call. So "hello" appends to the list (else part). The back element becomes "hello". At this point, front="abc" and back="hello".

Third add "world". Again is front null? No still "abc" so append again. Then back element becomes "world". At this point front="abc", back="world".

Then print the output. It checks the front element which is "abc" so the while loop is true. It prints "abc" then "hello" then "world"
 
kiran kumar reddy
Ranch Hand
Posts: 94
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
K. Tsang wrote:Why not just use the Java API java.util.LinkedList class?

Did you try compiling and running the code? What's the output?


yes we can use Java API java.util.LinkedList class, but for understanding concept, i just read it. and yes it ran succesfully output was
abc
hello
world
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!