Return to the lecture notes index

Lecture #7 (September 14, 2005)

You should have recieved an email with a grade and have feedback for lab 2. If something does not look right with your grade you can always come talk to us. If you come to us either we made a mistake and we will correct it, or you will understand why it was right, and you learn. Either way you are better off so feel free to come talk to us, that is why we are here.

Last time we wrote the basic simple methods for LinkedList today we are going to carve out slightly more complicated methods. Here is the revised java file

LinkedList.java

We are going to write the method removeNth which will remove a node from anywhere in the list.

// the head is the zeroth item of the list, just a reminder
public Object removeNth(long n){
  // lets take care of the special case first the list is empty
  // this is the case if there are no objects in the linkedlist 
  if (count==0){
	return null;
   }
   // special case if we remove the first object in the list
   if (n==0){
   return removeFirst();// this also includes the case if there is only one item
  }

  // special case if we remove the last item in the list
  if(n==(count-1)){
    return removeLast();
   }

Now that we have taken care of all the special cases we can code the meat of this method. Basic logic tells us to step through the list and when we get to the right place we stop. If we want to remove a node we cant stand on the node we want. We have to stop at the node in front of it in order to remove it from the list. Therefore if we want to remove the 5th item we want to go to the fourth item. If we want to remove n we have to stop at (n-1).

This generates the special cases. If there is no objects in the linkedlist then this process will not work. Also if we want to remove the first node in the linkedlist then we have to rename the head. Likewise if we want to remove the last item in the array, we have to move the tail.


 Node posn = head;
 // the comma operator is the sequence operator, it is almost like a semicolon
 //this is really dense code which we do advocate. Dense code is not always 
//better

for(long index=0; index<(n-1); index++,posn=posn.getNext())
 ;
Object tempObj  = posn.getNext().getItem();

posn.setNext(posn.getNext().getNext());
  count--;

return tempObj;

 }

}

Now lets say we want to do another method, this one is similar but instead of knowing the position we are passed the item as a parameter.


public void removeObject(Object item){

// special case if the LinkedList is empty
if(count==0)
return;

//special case is we want to remove the first item in the list
if(head.getItem().equals(item))
{removeFirst();
return;
}

// special case if we want to remove the last item in the list
if(tail.getItem().equals(item)){
removeLast();
return;
}

// we know it is either in the middle or not in the list at all

Node index = head;
while(index !=tail  &&(!index.getNext().getItem().equals(item))){
  index=index.getNext();
}  
// the loop either got us to the right place or it is not in the list
// if it is not in the list we return and are done
  if(index==tail)
  return;
// if it was in the list we are at the right place so we cut it out.
 index.setNext(index.getNext().getNext());
 return;

}

It is becoming apparent that Kesden is pushing us towards a particular style of programming. We should clear away the special cases first because that factors it out of the solution. Everything extra in the solution makes it a more complicated solution and not necesarily linearly. The complexity of the code increases quadradically. It is like coordinating a dinner or a meeting. With two or three people coordinating a meeting is fairly easy but by the time you are organizing 8 or 9 people it is impossible to get everyone together. So when you can take something out of a solution, handle it and call it dead it makes the code much simpler. By taking those special cases out of the while loop we had an easy while loop that just had to move forward. So this code is very well structured and it is easy to see the special cases. In writing the code we should comment the cases to make it very apparent what they are. This makes it really easy to follow the logic of the code. Even people with very limited programmming skills can understand this code.