19. Data Structures: Lists, Stacks, and Queues

19.3. OBJECT-ORIENTED DESIGN: The List Abstract Data Type (ADT)

The PhoneList example from the previous section illustrates the basic concepts of the linked list. Keep in mind that there are other implemen- tations that could have been described. For example, some linked lists use a reference to both the first and last elements of the list. Some lists

 

use nodes that have two pointers, one to the next node and one to the previous node. This enables traversals in two directions—front to back and back to front—as well as making it easier to remove nodes from the list. The example we showed was intended mainly to illustrate the basic techniques involved in list processing.

Also, the PhoneList example is limited to a particular type of data— A generic list structure

namely, a PhoneListNode. Let’s develop a more general linked list class and a more general node class that can be used to store and process lists of any kind of data.

An Abstract Data Type (ADT) involves two components: the data that are being stored and manipulated and the methods and operations that can be performed on those data. For example, an int is an ADT. The data are the integers ranging from some MININT to some MAXINT. The operations are the various integer operations: addition, subtraction, mul- tiplication, and division. These operations prescribe the ways that ints can be used. There are no other ways to manipulate integers.

Moreover, in designing an ADT, it’s important to hide the implemen-

tation of the operations from the users of the operations. Thus, our pro- Information hiding

grams have used all of these integer operations on ints, but we have no real idea how they are implemented—that is, what exact algorithm they use.

Objects can be designed as ADTs, because we can easily distinguish an object’s use from its implementation. Thus, the private parts of an object—its instance variables and private methods—are hidden from the user while the object’s interface—its public methods—are available. As with the integer operators, the object’s public methods prescribe just how the object can be used.

So let’s design a list ADT. We want it to be able to store any kind of data, and we want to prescribe the operations that can be performed on those

data—the insert, delete, and so on. Also, we want to design the ADT so Design specifications

that it can be extended to create more specialized kinds of lists.

The Node Class

Our approach will be to generalize the classes we created in the Phone- List example. Thus, the PhoneListNode will become a generic Node that can store any kind of data (Fig. 16–13). Some of the changes are merely name changes. Thus, wherever we had PhoneListNode, we now have just Node. The link access methods have not changed significantly.

What has changed is that instead of instance variables for the name, phone

number, and so on, we now have just a single data reference to an Object.

This is as general as you can get, because, as we pointed out earlier, data

 

can refer to any object, even to primitive data.

The implementation of the Node class is shown in Figure 16.14. Note that the data access methods, getData() and setData(), use references


Figure 16.13: The Node class is a generalization of the PhoneListNode class.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 16.14: The Node class is a more abstract version of the Phone- ListNode class.

 

to Object for their parameter and return type. Note also how we’ve de- fined the toString() method. It just invokes data.toString(). Be- cause toString() is defined in Object, every type of data will have this method. And because toString() is frequently overridden in defining new objects, it is useful here.

The List Class

Let’s now generalize the PhoneList class (Fig. 16–15). The List class will still contain a reference to the head of the list, which will now be a list of Nodes. It will still define its constructor, its fig-listuml isEmpty()

method, and its print() method in the same way as in the PhoneList.

 

FIGURE 16.15 The List class contains a pointer to the head of the list and public methods to insert and remove objects from both the front and rear of the list.


However, in designing a generic List class, we want to design some new methods, particularly because we want to use this class as the basis for more specialized lists. The PhoneList.insert() method was used to insert nodes at the end of a list. In addition to this method, let’s design a method that inserts at the head of the list. Also, PhoneList had a method to remove nodes by name. However, now that we have generalized our data, we don’t know if the list’s Objects have a name field, so we’ll scrap this method in favor of two new methods that remove a node from the beginning or end of the list, respectively.

We already know the basic strategies for implementing these new methods, which are shown in the definition in Figure 16.16. We have re- named the insertAtRear() method, which otherwise is very similar

 

,,

public c l a s s L i s t

private Node head ;

public L i s t ( )head = null ;

public boolean isEmpty ( )return head == null ;

public void pr i n t ( )

i f ( isEmpty ( ) )

System . out . p r i n t l n ( L i s t i s empty” ) ; Node current = head ;

while ( current ! = null )

System . out . p r i n t l n ( current . t o S t r i n g ( ) ) ; current = current . get Next ( ) ;

}

// p r i n t ( )

public void ins e r t At Fr o nt ( Object obj ) Node newnode =new Node( obj ) ; newnode . set Next ( head ) ;

head = newnode ;

}p ublic void insert At Rear ( Object obj )

i f ( isEmpty ( ) )

head = new Node( obj ) ;

e ls e

Node current = head ;// S t a r t a t h e a d o f l i s t

while ( current . get Next ( ) ! = null ) // F i n d t h e e n d o f t h e l i s t

current = current . get Next ( ) ;

current . set Next ( new Node( obj ) ) ;// C r e a t e a n d i n s e r t n e w No de

}

// i n s e r t A t R e a r ( )

public Object remove First ( )

i f ( isEmpty ( ) )// E m p t y L i s t

return null ; Node f i r s t = head ;

head = head . get Next ( ) ;

return f i r s t . get Data ( ) ;

// r e m o v e F i r s t ( )

public Object removeLast ( )

i f ( isEmpty ( ) )// e m p t y l i s t

return null ;

Node current = head ;

i f ( current . get Next ( ) == null )// S i n g l e t o n l i s t

head = null ;

return current . get Data ( ) ;

N} ode previous = null ;// A l l o t h e r c a s e s

while ( current . get Next ( ) ! = null ) previous = current ;

current = current . get Next ( ) ;

}p revious . set Next ( null ) ;

return current . get Data ( ) ;

} // r e m o v e L a s t ( )

// L i s t

\J

Figure 16.16: The List ADT.

 

to the PhoneList.insert() method. The key change is that now its parameter must be an Object, because we want to be able to insert any kind of object into our list. At the same time, our list consists of Nodes, so we have to use the Object to create a Node in our insert methods:

,,

 

J

Recall that the Node constructor takes an Object argument and simply assigns it to the data reference. So when we insert an Object into the list, we make a new Node and set its data variable to point to that Object. Note that we check whether the list is empty before traversing to the last node.

The new insertAtFront() method (Fig. 16.16) is simple to imple- ment, since no traversal of the list is necessary. You just need to create a new Node with the Object as its data element and then link the new node into the head of the list:

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Heterogeneous lists


J

See Figure 16.8a for a graphical representation of this type of insertion.

The new removeFirst() method is also quite simple to implement. In this case, you want to return a reference to the Object that’s stored in the first node, but you need to adjust head so that it points to whatever the previous head.next was pointing to before the removal. This requires the use of a temporary variable, as shown in the method.

The new removeLast() method is a bit more complicated. It handles three cases: (1) The empty list case, (2) the single node list, and (3) all other lists. If the list is empty, it returns null. Obviously, it shouldn’t even be called in this case. In designing subclasses of List we will first invoke isEmpty() before attempting to remove a node.

If the list contains a single node, we treat it as a special case and set head to null, thus resulting in an empty list. In the typical case, case 3, we traverse the list to find the last node, again using the strategy of maintaining both a previous and a current pointer. When we find the last node, we must adjust previous.next so that it no longer points to it.

 

 

 

Testing the List ADT

 

 

Testing the list ADT follows the same strategy used in the PhoneList example. However, one of the things we want to test is that we can indeed create lists of heterogeneous types—lists that include Integers mixed with Floats, mixed with other types of objects. The main() method in Figure 16.17 illustrates this feature.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 16.17: A series of tests for the List ADT.

 

 

 

The list we create here involves various types of data. The Phone- Record class is a scaled-down version of the PhoneListNode we used in the previous example (Fig. 16.18). Its definition is shown in Figure 16.19. Note how we use an Object reference to remove objects from the list in main(). We use the Object.toString() method to display the object that was removed.


 

 

 

 

Figure 16.18: The PhoneRecord class stores data for a phone direc- tory.

 

,,

 

 

 

 

 

 

 

 

 

 

 

 

 

J

Figure 16.19: A PhoneRecord class.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Stack applications


SELF-STUDY EXERCISES

EXERCISE 16.7 Trace through the main() method line by line and predict its output.

EXERCISE 16.8 Design a test of the List program that shows that it is possible to insert new elements into a list after some or all of its previous nodes have been removed.