Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 15 Linked Lists part 2

Similar presentations


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

1 Lecture 15 Linked Lists part 2
Richard Gesick

2 Topics Sorted Linked Lists Doubly Linked Lists
Linked Lists of Generic Types Recursively Defined Linked Lists

3 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.

4 PlayerSortedLinkedList Methods
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.

5 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.

6 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.

7 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

8 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.

9 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.

10 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.

11 Doubly Linked Node A doubly linked node looks like this
The right arrow represents next. The left arrow represents previous.

12 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.

13 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.

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

15 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.

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

17 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.

18 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.

19 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.

20 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

21 Linked Lists of Generic Types
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.

22 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;

23 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>

24 Generic Linked List Code
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.

25 Generic Linked List Code
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.

26 Recursively Defined Linked Lists
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)

27 PlayerRecursiveLinkedList Methods
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.

28 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

29 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

30 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.

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

32 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

33 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

34 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.

35 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;

36 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.

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


Download ppt "Lecture 15 Linked Lists part 2"

Similar presentations


Ads by Google