CSC 241- Set #6 (Starting October 29th, 1997)

A List class

intList class maintains a list of integers. I have included some of the functionality that you get with the Vector Class, here is the definition of the class:


public class intList {
  public intList()
  public boolean empty()
  public int find(int val)
  public void append(int val) 
  public int addBefore(int pos, int val) 
  public int addAfter(int pos, int val) 
  public int remove(int pos) 
  public int valAt (int pos)
  public int last() 
  public String dump ()
}

Certainly, the definition does not give away the fact that we implemented it with a linked list, as you can see, the concept of position is used everywhere. The Node class used is basically the same as the one we used for intStack, so, we have a simple singlely-linked list.

What is the rationale for having a class defined that implies we are using an array, but, we actually implement with a linked-list? I'll give you four reasons:

  1. Providing functionality such finding elements based on their position regardless of data structures used is useful.
  2. You need more experience with linked-lists.
  3. It is important to see implementation that seem out of the ordinary, just so that you know there is never only one way to do something.
  4. Last, but not least, this example enables me to reiterates the impact data structures choices on the performance of algorithms.

Details of the implementation

Let us consider the code for a few of the methods written for intList:

Lets first look at valAt:

  public int valAt (int pos) {
    if (head == null) return -1;
    Node temp;
    int last;
    for (temp=head,last=0;temp != null;temp=temp.next,last++)
        if (last == pos)
           return temp.element;
    return -1;
  }

To understand the code here, we need to make sure the for statement is understood. In parenthesis, there are three parts separated by ;.

The first part performs initializations. temp=head sets a temporary variable (temp) reference the first node in the list. last=0 starts a counter variable (last) as zero which keeps track of the position of the node to be visited.

the second part, temp!=null gives the condition that stops the loop, so, the loop stops only when there are no more nodes to consider.

The third part gives us what needs to change after each cycle of the loop. temp=temp.next moves our temp pointer to the next node in the list. last++ increments the counter.

Inside the loop, we simply check to see if we have hit the correct position or not, if we have, the element in the node referenced by temp is returned. The return is occurring inside the loop which acts as a loop exit, no other instructions in this method are performed once this return is executed.

Note that return -1; is NOT in the for loop, and only occurs if the loop ends due to temp being null. Returning -1 implies an unsuccessful call to valAt. This means that the list must not have a -1 as an element or we are in trouble, keep in mind, this is just an example.

Lets look at addAfter:

  public int addAfter(int pos, int val) {
    if (head==null) return -1;
    Node temp;
    int last;
    for (temp=head,last=0;temp != null;temp=temp.next,last++)
      if (last == pos) {
        temp.next = new Node (temp.next,val);
        return 0;
      }
    return -1;
  }

Notice how similar the approach is in addAfter to that of valAt. The loop keeps going until the node in a particular position is found. However, our intent is different in that we wish to add a new node to the list, thus, once the node in the correct position is located, temp.next = new Node (temp.next,val); is executed. This one statement does a lot of work, it creates a new node, puts the new element in it with its next pointer as temp's next pointer. The assignment statement causes the next pointer of temp to now point this new node. Lets show what happens with an example:

suppose we make the following call: addAfter(2,12), which means we wish to add a new node holding 12 after the node in the position 2. Lets assume the following list before the addAfter is performed:

head --> 9 --> 10 --> 11 --> 13 --> 14

The for statement loops until temp is pointing to the node containing 11. temp.next = new Node (temp.next,val); performs the following:

head --> 9 --> 10 --> 11--         -->13 --> 14
                          |       |
                           -->12--

An example on using intList

Consider the following Selection Sort. In this algorithm, data is an intList object containing our values.

   for (int i=0; i<n-1; i++) {
       int min = i;
       for (int j=i+1; j<n; j++)
           if (data.valAt(j) < data.valAt(min)) {
              min = j;
           }
       tmp = data.valAt(i);
       //Note that there is no "replace" method shown above in intList,
       //but, you we are assuming it exists for this example.
       data.replace(i,data.valAt(min));
       data.replace(min,tmp);
   }
}

We will be looking closely algorithm efficiency later, you will see me use terminology, such as, an algorithm is O(n2) or it is O(n). But, for this example, just think about how many iterations it will take to accomplish this sort. If you have 5 elements, you are looking at 15 iterations in the above nested loops. But, data.valAt(j) hides yet another loop! The code associated with valAt actually visits every cell leading to the jth node. Which means you really iterate 30 times. Without getting into the mathematical details of this one, we are looking at an O(n3) algorithm. With 1000 elements, instead of 1000,000 cell visits, we will have 1000,000,000 cell visits.