Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 1302J Lecture 15 Linked Lists part 2 Richard Gesick.

Similar presentations


Presentation on theme: "CSE 1302J Lecture 15 Linked Lists part 2 Richard Gesick."— Presentation transcript:

1 CSE 1302J Lecture 15 Linked Lists part 2 Richard Gesick

2 CSE 1302 J 2 of 37 Topics Sorted Linked Lists Doubly Linked Lists Linked Lists of Generic Types Recursively Defined Linked Lists

3 CSE 1302 J 3 of 37 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 CSE 1302 J 4 of 37 PlayerSortedLinkedList Methods Return valueMethod name and argument list voidinsert( Player p ) inserts Player p in the location that keeps the list sorted Playerdelete( 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. Playerpeek( 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 CSE 1302 J 5 of 37 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 CSE 1302 J 6 of 37 Inserting in the Middle or at the End of a Sorted Linked List 1.Instantiate the new node. 2.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. 3.Attach the new node to current. 4.Attach previous to the new node. 5.Increment the number of items.

7 CSE 1302 J 7 of 37 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 CSE 1302 J 8 of 37 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 CSE 1302 J 9 of 37 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 CSE 1302 J 10 of 37 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 CSE 1302 J 11 of 37 Doubly Linked Node A doubly linked node looks like this –The right arrow represents next. –The left arrow represents previous.

12 CSE 1302 J 12 of 37 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 CSE 1302 J 13 of 37 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 CSE 1302 J 14 of 37 Inserting into a Doubly Linked List 1. Instantiate the new node 2. Set next in new node to current.

15 CSE 1302 J 15 of 37 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 CSE 1302 J 16 of 37 Inserting into a Doubly Linked List 5. Set previous in current to the new node.

17 CSE 1302 J 17 of 37 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 CSE 1302 J 18 of 37 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 CSE 1302 J 19 of 37 Deleting in the Middle of a Doubly Linked List We will delete the Player with id 7 1.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 CSE 1302 J 20 of 37 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 CSE 1302 J 21 of 37 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 CSE 1302 J 22 of 37 Generic Class Syntax The basic syntax for the header of a class that implements generics is: AccessModifier class className 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 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 CSE 1302 J 23 of 37 Generic Object References In order to use an object reference of a class implementing generics, we use the following syntax: ClassName 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

24 CSE 1302 J 24 of 37 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 CSE 1302 J 25 of 37 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 CSE 1302 J 26 of 37 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 valueMethod name and argument list voidinsert( T item ) inserts Player p at the beginning of the list booleandelete( 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 CSE 1302 J 28 of 37 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 CSE 1302 J 29 of 37 Inserting in a Recursively Defined Linked List The original list, before inserting Player p: The list after inserting Player p: p1 r1 p ned list) first rest firstrest p1 r1

30 CSE 1302 J 30 of 37 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 CSE 1302 J 31 of 37 Deleting the First Item in a Recursively Defined Linked List The original list, before deleting Player p: p ned list) firstrest p1 r1

32 CSE 1302 J 32 of 37 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; p1 ned list) firstrest p1 r1

33 CSE 1302 J 33 of 37 Deleting the First Item in a Recursively Defined Linked List rest is assigned the rest of the rest of the list: rest = rest.rest; p1 r1 firstrest

34 CSE 1302 J 34 of 37 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 CSE 1302 J 35 of 37 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; }

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


Download ppt "CSE 1302J Lecture 15 Linked Lists part 2 Richard Gesick."

Similar presentations


Ads by Google