Lecture 15 Linked Lists part 2

Presentation on theme: "Lecture 15 Linked Lists part 2"— Presentation transcript:

Lecture 15 Linked Lists part 2
Richard Gesick

Sorted Linked List For some applications, it may be desirable for a Linked List to be sorted. The items can be sorted based on the value of one of their instance variables, called a key. A Linked List that stores its items in ascending (or descending) order according to a key value is called a Sorted Linked List. For example, for a Sorted Linked List of Player objects, we could arrange the list by the key id. Without loss of generality, we will consider a list sorted in ascending order.

Return value Method name and argument list void insert( Player p ) inserts Player p in the location that keeps the list sorted Player delete( int searchID ) returns and removes the first Player of the list whose ID is equal to searchID. If there is no such Player on the list, the method throws a DataStructureException. peek( int searchID ) returns the first Player of the list whose ID is equal to searchID. If there is no such Player on the list, the method throws a DataStructureException.

Inserting in a Sorted Linked List
When inserting an item in a sorted linked list, we must keep the list sorted; that is, after insertion is performed, the list is still sorted. If the key value of the item to be inserted is smaller than all the key values in the list, then we insert the item at the beginning of the list. This operation is identical to our previous insertion in a linked list. Otherwise, we will insert the item somewhere in the middle or at the end of the list.

Inserting in the Middle or at the End of a Sorted Linked List
Instantiate the new node. Traverse the list to identify the location to insert the new node. Call the node before the insertion point previous, and the node after current. Attach the new node to current. Attach previous to the new node. Increment the number of items.

Inserting in the Middle or End of a Sorted List
Instantiate new node and find appropriate location Attach new node to current Attach previous to new node

Deleting from a Sorted Linked List
Our delete method is similar to the previous delete method of a linked list. If the key value we are trying to delete is not in the list, we can determine that fact as soon as we visit an item with a value greater than the search value. At that point, we can exit the method.

Testing a Sorted Linked List Class
When testing the insert method of a sorted linked list class, be sure to test all possibilities: inserting into an empty list. inserting at the beginning of the list. inserting in the middle or at the end of the list. Traverse the list after each insert to verify that the item was inserted at the correct location.

Doubly Linked List Each node has two links: one forward (as before) and one backward. The PlayerNode class now has an additional instance variable: previous, the previous PlayerNode. Thus, we can traverse a doubly linked list either forward or backwards. Every time we insert or delete, both links must be updated.

The right arrow represents next. The left arrow represents previous.

Inserting in a Doubly Linked List
There are three cases: insert at the beginning. insert in the middle. insert at the end. Updating the links will differ in the three cases above. Furthermore, when a node is inserted at the beginning, head needs to be updated.

Inserting in the Middle
In order to insert a node before a node named current, the following steps need to be performed: Instantiate a new node. Set two forward links: new node to current, node before current to new node. Set two backward links: current to new node, new node to node before current. Update the number of items. Updating the links will differ in the three cases above.

Inserting into a Doubly Linked List
1. Instantiate the new node 2. Set next in new node to current.

Inserting into a Doubly Linked List
3. Set next in node before current to the new node. 4. Set previous in the new node to the node before current.

Inserting into a Doubly Linked List
5. Set previous in current to the new node.

Inserting into a Doubly Linked List
Here is the code to perform the insertion: PlayerNode pn = new PlayerNode( p ); // 1 pn.setNext( current ); // 2 ( current.getPrevious( ) ).setNext( pn ); // 3 pn.setPrevious( current.getPrevious( ) ); // 4 current.setPrevious( pn ); // 5 numberOfItems++; // 6 Note that the order of execution is important to preserve the links.

Deleting from a Doubly Linked List
There are four cases: delete at the beginning. delete in the middle. delete at the end. cannot delete. Updating the links will differ in the first three cases above. Furthermore, when a node is deleted at the beginning, head needs to be updated.

Deleting in the Middle of a Doubly Linked List
We will delete the Player with id 7 Set next in the node before current to the node after current. 2. Set previous in the node after current to the node before current.

Deleting in the Middle of a Doubly Linked List
Here is the code: ( current.getPrevious( ) ).setNext( current.getNext( ) ); // Step 1 ( current.getNext( ) ).setPrevious( current.getPrevious( ) ); // Step 2 numberOfItems--; // Step 3

To write a linked list that accepts items of any class, we can use generic types. In a linked list, the data of the item is stored in the node; thus, the data in our Node class will be a generic object.

Generic Class Syntax The basic syntax for the header of a class that implements generics is: AccessModifier class className<IdentifierForGenericClass> For the identifier for our generic class, we will use the upper case letter T. Thus, for our Node class, the header will be: public class Node<T> Inside the class, we can then use that identifier, here T, as we would use an existing or user-defined class. For example, to declare an instance variable named data of class T, we write: private T data;

Generic Object References
In order to use an object reference of a class implementing generics, we use the following syntax: ClassName<IdentifierForGenericClass> Thus, in order to declare an object reference of the Node class as a return type or a parameter for a method, we use the notation Node<T>

We implemented our delete method differently. We cannot delete an item based on the value of one of its fields because we do not know what the fields of that item are, since that item is a generic object. Thus, the parameter of our delete method is a generic object of the same type as the items in the list.

There is no need to return an item if we find it and can delete it because we already have that item as the parameter of the method. So, the delete method returns a boolean value: true if we were able to delete the item, false otherwise. In order to compare item with the items in the list, we call the equals method, inherited by any class from the Object class, and which will need to be overwritten in the class the client specifies as the type for the linked list.

A recursively defined linked list is made up of two items: An item, the first item in the list. A linked list, the rest of the linked list. Thus, we have two instance variables: first, the first item in the list. rest, a linked list itself, the rest of the list. first (an item) rest (a linked list)

Return value Method name and argument list void insert( T item ) inserts Player p at the beginning of the list boolean delete( T item ) removes the first object of the list that matches item and returns true. If there is no such item on the list, the method returns false.

Inserting in a Recursively Defined Linked List
Unless the list is a sorted list, we will insert at the beginning of the list. After insertion: first will hold the item just inserted rest will hold the original list

Inserting in a Recursively Defined Linked List
The original list, before inserting Player p: The list after inserting Player p: p r1 first rest p ned list) p r1 first rest

Deleting from a Recursively Defined Linked List
Our recursive delete method deletes the first item on the list whose key value matches a given key value. There are three base cases: Empty list The item to delete is the first one on the list The item to delete is not the first one on the list and the rest of the list is empty In the general case, we make a recursive call to try to delete from the rest of the list.

Deleting the First Item in a Recursively Defined Linked List
The original list, before deleting Player p: p ned list) p r1 first rest

Deleting the First Item in a Recursively Defined Linked List
first is assigned the first item of the rest of the list: first = rest.first; p ned list) p r1 first rest

Deleting the First Item in a Recursively Defined Linked List
rest is assigned the rest of the rest of the list: rest = rest.rest; p r1 first rest

Processing a Recursively Defined Linked List
Generally, we want to do the following: If the list is empty, the method returns. If the list is not empty, process first, the first element of the list. The method may or may not return at this point. If rest is null, the method returns. If rest is not null, make a recursive call on rest.

Coding the toString Method of a Recursively Defined Linked List
public String toString( ) { String listString = "": if ( first != null ) listString = first.toString( ) + "\n"; if ( rest != null ) listString += rest.toString( ); } return listString;

Common Error Trap When processing a recursively defined list, not testing for all the base case conditions can result in a NullPointerException at run time.

SOFTWARE ENGINEERING TIP When implementing methods of a recursively defined class, think in terms of implementing recursive methods.