i understand what the code does. but i dont understand a few lines:

package il.co;

public class LinkedListEx2 {
	public static void main(String[] args) {
		LinkedList list=new LinkedList();
		list.add(new Car("BMW", 150000));
		list.add(new Car("SUBARU", 60000));
		list.add(new Car("TOYOTA", 58000));
		list.print();
	}
}

class LinkedList{
	private Unit head;
	public void add (Car data){
		
		Unit newUnit =new Unit(data);
		if(head==null)
			head=newUnit;
		else{
			Unit current=head;
			while (current.next!=null)// current.next, doesnt current and next refer to the same object/class? isnt current.next = null from the begining. whats the value of current.next?
			current=current.next;// why is that line importnant . why to swap current next
			current.next=newUnit;
		}
	}

	
	public void  print() {
		print(head);
	}
	private void print(Unit unit){
		if(unit==null)
			return;
		System.out.println(unit.info);
		print(unit.next);
	}
}


class Unit{
	Car info;
	Unit next; 
	public Unit(Car info) {
		this.info=info;
	}
}



class Car {
	String model;
	int price;
	public Car(String model, int price) {
		this.model = model;
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car [model=" + model + ", price=" + price + "]";
	}
	
}

Each node (Unit in your code) in a linked list knows what node follows it, that is the node knows it's "next" node. The lines of this code that you have indicated traverse the list from the start (head) node until we get to the end of the list (when current.next is null), and finally we assign the last node's "next" node to be the new node, effectively adding it to the end of the list.

Hope this helps :)

Edited 6 Years Ago by darkagn: n/a

whats current.next
how can you refer to an object twice|
why does it pop up in the code from the while loop\|
isnt it null to start with|

alright i think i got it.

when does this method called inside the add class?

private void print(Unit unit){
		if(unit==null)	// if this is true. then return both of the statements\|???	
	return;		
System.out.println(unit.info);	
	print(unit.next);	}}

i am confused about this syntax

Edited 6 Years Ago by NewOrder: n/a

NowOrder: Linked lists are a well-described area of computer science, usually the first thing covered in your second-year Data Structures and Algorithms class. They underlie a number of standard data structures and understanding them is fundamental to understanding serious programming.

What follows will be a very brief overview, but you will find that your questions are well-answered by a bit of searching - "linked list" and "data structure" would be good terms to start with. Even better would be to have a look at one of the many good books which cover this material - Lafore, Weiss, or Sedgewick are all good.
Since this information is so very available, I will not respond to anything further unless your questions demonstrate that you have done this research.

That being said:

A linked list is a data structure in which each data element is linked to the next, so your only access within the list is by linear traversal. Variations include doubly-linked lists, in which elements are linked both to the following and preceding elements, and circular lists, in which the last element in the list is linked to the first. The linked list is often used to implement the "stack" and "queue" data structures, but it is not identical with them - this is a point of confusion to avoid.
A linked list consists of a series of "nodes". In the simplest case, each node consists of two elements: the data and the link. The data can be anything, the list is completely agnostic on this. The link is simply a reference to a node object. *This is confusing for some* so pay attention: the definition of a Node includes a reference to a Node. This is perfectly legal, and it causes no problems. Why? Because a reference to a Node is not a Node. If I try to put one of my shoes inside the other, it won't fit because they're the same size - one cannot contain the other. However, I could easily put a slip of paper saying "my other shoe" into one of my shoes, and walk around all day with that paper in my shoe. A shoe can contain a reference to a shoe, but it cannot contain a shoe. A Node works the same way:

public class Node
{
  private String data;
  private Node next;  // this is not a Node, it is a REFERENCE to a Node!
}

However, we talk about a Node "containing" a String, so it's easy to see how this becomes confusing: it contains pointers to Objects of various sorts on the heap. Each pointer is a single memory address.

A linked list starts with a pointer to the "head" of the list, which is our entry point. That pointer and the node structure allow us to connect a chain of data as long as we like, limited only by the amount of memory in the machine: to build a list, you can simply create a new Node with the data you want to store, and point the last Node in the list to the new Node. In order to find a piece of data, you start at the head of the list, and follow the pointers to the next Node, examining each Node to see if it's the one you want.
If you were to do this linear traversal, you would need a place to keep a reference to the Node you're currently looking at: this is universally referred to as "current". Hence, "current.next()".


Of course, it's not that simple -there are a lot of details that I won't cover. For example, you want to be able to add or delete any arbitrary node. That is, you want your LinkedList class to have methods

public void add(Node n) {}
and
public Node delete(Node n) {} // return the deleted Node

which really should be
public void add(Data d) {} //create and add a Node for the data
and
public Node delete(Data d) {} // find the node containing the requested data and return it or null if not found

Or you might want to implement one of the variations on this, or there are lots of other things to look at here.
That should get you started on this. Again, please do the reading. Google is your friend.
And please try to actually write the implementations for the basic linked lists - singly-linked, doubly-linked, and circular, with find(), add(), and remove() methods - you can't understand them properly without having implemented them.

private Unit head;
#
private void print(Unit unit){
#
if(unit==null)
#
return;
#
System.out.println(unit.info);
#
print(unit.next);
#
}

why are those two private?

in your original code it was:

public void print() {
print(head);
}
private void print(Unit unit){
if(unit==null)
return;
System.out.println(unit.info);
print(unit.next);
}

// edit fixed code tag
what this does is let a user use the print method that is public then it calls the private method with a variable that a method of the class would know, the current head or start of the list.

really there is no reason the public print couldn't do this work, but i've used public methods that connect to another method because i've sometimes wanted my internal method to be called in different ways. the public print always prints from start, but no reason another method of mine coudlnt call the private print the way its written and print a partial list, by feeding it a different unit.

Mike

Edited 6 Years Ago by adams161: n/a

Also actually the user calls print once, and the private print function is indeed called multiple times with different heads. a form of recursion.

This article has been dead for over six months. Start a new discussion instead.