COS 111, Fall 1997

Implementing linked lists in Java-like pseudocode


These are the methods we wrote in class to implement linked lists.

An item in a linked list is composed of a value, and the location (address) of the next item in the linked list. If the name of an item is item_name, then we refer to these two components as item_name.value and item_name.next, respectively. insertFront is a procedure that takes one argument called newdata, which give the new value to be inserted. removeFront is a function that takes no arguments and returns the location of the item that has been removed -- the item is still in memory but no longer linked to the list.


defining insertFront(newdata)
       {  temp = first;
          first = address of a new place in memory for an item;
          first.value = newdata;
          first.next = temp;
          if (temp == nil )         //if the list started out empty
              {last = first;}       //make the new item last as well
       }


defining removeFront() returning the location of the removed item
       {  temp = first;
          if ( first != nil)       // if the list did not start out empty
              {
               first = first.next; // first moves down the list by one item
               if (first == nil)         // if the list is now empty
                   {last = first;}       // update last
              }
          return temp;
       }