1 / 29

Lecture 14 Basic Linked List concepts

Lecture 14 Basic Linked List concepts. Richard Gesick. Topics. Linked Lists Concepts and Structure Linked Lists of Objects. Linked Lists. In Chapters 8 and 9, we introduced the concepts of arrays and the ArrayList class.

woody
Télécharger la présentation

Lecture 14 Basic Linked List concepts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 14Basic Linked List concepts Richard Gesick

  2. Topics • Linked Lists Concepts and Structure • Linked Lists of Objects

  3. Linked Lists • In Chapters 8 and 9, we introduced the concepts of arrays and the ArrayList class. • Arrays and ArrayLists are examples of data structures, which are methodologies a program uses to store data in memory. • In some situations, the number of data items may dynamically increase or decrease as the program executes. • A Linked List is a data structure that shrinks and grows one object at a time, keeping the size of the list to a minimum at all times.

  4. Linked Lists • A linked list can be thought of as a chain of linked nodes. • A node is an object with two attributes: • data • the location of the next node in the chain

  5. Linked Lists The data stored at each node can be primitive data types (int, char, double..) or objects (Book, Astronaut, Player…) Here is an example of a list of Player objects with three instance variables: ID, name, and game.

  6. A Linked List of Primitive Types • First we code a linked list where the data in the nodes are ints. • We define the node class, IntegerNode, which has two instance variables: • data, an int • an IntegerNode reference, next, which is a link to the next node in the list.

  7. A LinkedList Class Next we define an IntegerLinkedList class, which will implement the linked list of IntegerNode objects. It has two instance variables: • an IntegerNode reference, named head, representing the first node in the list • an int, named numberOfItems, representing the number of nodes in the list. We will provide an accessor for numberOfItems, but no mutator because clients should not change this value.

  8. SOFTWARE ENGINEERING TIP Do not provide an accessor or mutator method for the head node instance variable of the linked list. This will protect the head node from being accessed or changed outside the class. Similarly, do not include a mutator method for the number of items in the list. Only the insert and delete methods of the linked list class should alter the number of items.

  9. SOFTWARE ENGINEERING TIP Including an instance variable in the linked list class that stores the number of items in the list allows for quick access to the size of the list, as needed.

  10. Basic Functionality of a Linked List Clients should be able to insert and delete items, and display all the items in a list. We implement the following APIs:

  11. The toString Method ThetoStringmethod traverses the list(visits every node) and returns a String containing the data for all the objects in the list. public String toString( ) { String listString = ""; IntegerNode current = head; for( inti = 0; i < numberOfItems; i++ ) { listString += current.getData( ) + " "; current = current.getNext( ); } return listString; }

  12. SOFTWARE ENGINEERING TIP Provide a toString method that traverses the list. This is helpful at the debugging stage when testing other methods, in particular insert and delete.

  13. The insert Method The insert method performs the following steps: 1. Instantiate a new node containing the int to be inserted. 2. Attach that node at the beginning of the list, that is, make that node point to the previous head node. If the list originally was empty and the previous head node has the value null, then the next field of the new node is given the value null. 3. Indicate that the new node is now the head of the list, that is, make head point to the new node. 4. Increase the number of items in the list by 1.

  14. The insert Method The original list: The new node is instantiated: The new node is attached to the beginning of the list: head now points to the new node:

  15. The delete Method To find the node to delete, we traverse the list. Traversing a list means to loop through the list, visiting each node in order. Three outcomes are possible: • The item is found and is not the head of the list. • The item is found and is the head of the list. • The item is not found, and therefore, cannot be deleted. As we traverse the list, we maintain a reference to the previous node, because we will need to connect that node to the node following the deleted node.

  16. Deleting an Item That is Not the Head The list before deleting the item with the value 8: The previous node is connected to the node after current:

  17. Deleting an Item That is the Head The list before deleting the item whose value is 7: The link from the first node becomes the new head:

  18. Common Error Trap When traversing a list and calling a method with a node reference, be sure that the node reference is not null. Calling a method with a null node reference will generate a NullPointerException at run time. Because compound expressions are evaluated left to right, if a compound logical expression uses an object reference to call a method, check whether the object reference is null in the first expression.

  19. Testing a Linked List Class Be sure to test all possibilities. Insert: • inserting into an empty list • inserting into a nonempty list Delete: • attempting to delete from an empty list • deleting the first item in the list • deleting the last item in the list • deleting an item in the middle of the list. • attempting to delete an item not in the list

  20. Linked Lists of Objects To implement a linked list of Player objects, we will code three classes: • A Player class, encapsulating a player. • A PlayerNode, encapsulating a node. • A List class, encapsulating a linked list of Players.

  21. Linked Lists of Objects • Next we will define a linked list where the items in the lists are objects, specifically, Player objects. • The Player class encapsulates game players and has three instance variables: • id, an int • name, a String (the player’s name) • game, a String (the player’s favorite game)

  22. The PlayerNode Class For this list, the items of the nodes will contain Player objects. The PlayerNode class has two instance variables: • player, a Player object reference • next, a PlayerNodeobject reference (the next node in the list)

  23. A Linked List Shell Class • Because we anticipate having many linked list classes, we set up a linked-list superclass from which our more specialized linked-list classes (unsorted, sorted, stacks, queues, ...) will inherit. • This class will provide basic utility methods that test whether the list is empty and return the number of items in the list, and also a toString method that traverses the list. • This class will be abstract.

  24. The ShellLinkedList Class • The ShellLinkedList class has two instance variables: • head, a PlayerNodeobject reference (the first node in the list) • numberOfItems, an int (the number of items in the list, for convenience) • We declare the instance variables as protected so that our subclasses will inherit them.

  25. ShellLinkedList Methods

  26. Another Way to Code toString Instead of using a for loop with the number of nodes, we can use a while loop and check for the end of the list. public String toString( ) { String listString = ""; PlayerNode current = head; while( current != null ) //check for last node { listString += current.getPlayer( ).toString( ) + “\n"; current = current.getNext( ); } return listString; }

  27. Linked List Functionality • Our ShellLinkedList provides this functionality: • insert an item at the beginning of the list • delete an item based on the value of an instance variable. For the Player class, we chose ID, but we could also write delete methods based on other instance variables. We will return the Player object deleted. • retrieve, or peek at, the contents of a node. This method returns the object identified by ID, but does not delete it.

  28. The DataStructureException Class We want our delete method and peek method to return a reference to the item. • But there will be instances where we cannot find an item (empty list, item not found), and we do not want to return null. In these cases, we will throw an exception. Thus, we create our own exception class. • The constructor takes an error message identifying the cause of the error.

  29. PlayerLinkedList Methods

More Related