Lecture 15 linked lists part 2
Download
1 / 37

Lecture 15 Linked Lists part 2 - PowerPoint PPT Presentation


  • 179 Views
  • Uploaded on

Lecture 15 Linked Lists part 2. Richard Gesick. Topics. Sorted Linked Lists Doubly Linked Lists Linked Lists of Generic Types Recursively Defined Linked Lists. Sorted Linked List. For some applications, it may be desirable for a Linked List to be sorted.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Lecture 15 Linked Lists part 2' - chars


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Lecture 15 linked lists part 2

Lecture 15Linked Lists part 2

Richard Gesick


Topics
Topics

  • Sorted Linked Lists

  • Doubly Linked Lists

  • Linked Lists of Generic Types

  • Recursively Defined Linked Lists


Sorted linked list
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.



Inserting in a sorted linked list
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
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
Inserting in the Middle or End of a Sorted List List

Instantiate new node

and find appropriate

location

Attach new node to

current

Attach previous

to new node


Deleting from a sorted linked list
Deleting from a Sorted Linked List 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
Testing a Sorted Linked List Class List

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
    Doubly Linked List 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.


    Doubly linked node
    Doubly Linked Node List

    • A doubly linked node looks like this

      • The right arrow represents next.

      • The left arrow represents previous.


    Inserting in a doubly linked list
    Inserting in a Doubly Linked List 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
    Inserting in the Middle List

    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
    Inserting into a Doubly Linked List List

    1. Instantiate the

    new node

    2. Set next in new

    node to current.


    Inserting into a doubly linked list1
    Inserting into a Doubly Linked List 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 list2
    Inserting into a Doubly Linked List List

    5. Set previous in

    current to the new

    node.


    Inserting into a doubly linked list3
    Inserting into a Doubly Linked List List

    Here is the code to perform the insertion:

    PlayerNodepn = 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
    Deleting from a Doubly Linked List 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
    Deleting in the Middle of a ListDoubly 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 list1
    Deleting in the Middle of a ListDoubly Linked List

    Here is the code:

    ( current.getPrevious( ) ).setNext( current.getNext( ) ); // Step 1

    ( current.getNext( ) ).setPrevious( current.getPrevious( ) ); // Step 2

    numberOfItems--; // Step 3


    Linked lists of generic types
    Linked Lists of Generic Types List

    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
    Generic Class Syntax List

    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
    Generic Object References List

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


    Generic linked list code
    Generic Linked List Code List

    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.


    Generic linked list code1
    Generic Linked List Code 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 booleanvalue: 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.


    Recursively defined linked lists
    Recursively Defined Linked Lists 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)



    Inserting in a recursively defined linked list
    Inserting in a Recursively Defined Linked List 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 list1
    Inserting in a Recursively Defined Linked List List

    • The original list, before inserting Player p:

    • The list after inserting Player p:

    p1r1

    first

    rest

    p ned list)

    p1 r1

    first

    rest


    Deleting from a recursively defined linked list
    Deleting from a Recursively Defined Linked List 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
    Deleting the First Item in a Recursively Defined Linked List List

    The original list, before deleting Player p:

    p ned list)

    p1 r1

    first

    rest


    Deleting the first item in a recursively defined linked list1
    Deleting the First Item in a Recursively Defined Linked List List

    • first is assigned the first item of the rest of the list:first = rest.first;

    p1 ned list)

    p1 r1

    first

    rest


    Deleting the first item in a recursively defined linked list2
    Deleting the First Item in a Recursively Defined Linked List List

    • rest is assigned the rest of the rest of the list:

      rest = rest.rest;

    p1 r1

    first

    rest


    Processing a recursively defined linked list
    Processing a Recursively Defined Linked List 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
    Coding the ListtoString 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 List

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


    SOFTWARE List

    ENGINEERING TIP

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


    ad