Return to the lecture notes index

Lecture #26 (Monday, Nov 13, 2006)

LinkedLists

Today we worked on coding LinkedLists. We created a LinkedList.java LinkedList.java

First we want to make the class Node, but since this is a part of linkedlist we want to make the class Node private to linkedlist so we nest it inside.

class LinkedList{

// linklist's instance variables would normally be here but for the sake
// of discussion are further down in the class

 private class Node{
 
//what are the two important elements of a node?
// a pointer to the next node, and a reference to the object itself 

  private Object item;
  private Node next; 

/*
 *As far as java access protections are concerned the node class has private
 *declarations, but since it is within linkedlist, linkedlist can access node's 
 *private elements.  LinkedList can play with it but we call it private to 
 *show that we dont want linkedlist to play with it directly.
*/

public Node(Object item){
   this.item = item;
   next=null;
 }

//What is the this reference?
//Since there are two variables being refered too as item
//it tells the computer that we want the one that was an instance variable
//for the class instead of the one that was passed in as a parameter. 

 public Node(Object item, Node next){
 this.item= item;
 this. next = next;
 }

//We are going to have accessors and other methods.

 public Node getNext()
 {
	return next;
 }

 public Object getItem()
 { 
	return item;
 }

 public void setNext(Node next)
 {
	this.next=next;
 }

}
// Now we have the basic framework of a node. I did not put in a setItem method
// because there is no reason to set an item on a node. A Nodes whole purpoes 
// is to organize an item. If you need a new item use a new node, if you want 
// to swap items swap nodes. Setting an item within a node would just make
// the code very confusing.

// Now we are going to write contructor for linked list
// these would normally be at the top of the class

private Node head;
private Node tail;
private int count;

// general constructor
public LinkedList(){
  count=0;
  head = tail = null; 

}

// one method we need is to be able to add an object at the beginning of the 
//list we do this by an addFirst method

public void addFirst(Object item){

head = new Node(item, head);// this creates a new node and has it point to head
count++;                    // which was the old start of the list. Then it sets
 			    // head to the new node which becomes the first item
			    // in the list
if(tail=null){  // this is a special case for when the item we are adding will
 tail=head;    // be the first item in the list
 }
}

// addLast adds the new object at the end of the list

public void addLast(Object item)
{
if(tail==null){ //first we take care of the special case where the list is empty
 addHead(item); //addHead changes count
}
else{
Node newNode = new Node(item);  //create a new node
 tail.setNext(newNode);     	// set the tail equal to the new node
 tail = newNode;                //or tail=tail.getNext();
 count++;
 }
}