Download
lists n.
Skip this Video
Loading SlideShow in 5 Seconds..
Lists PowerPoint Presentation

Lists

90 Vues Download Presentation
Télécharger la présentation

Lists

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Lists List Implementations

  2. Linked List Review • Recall from CMSC 201 • “A linked list is a linear collection of self-referential structures, called nodes, connected by pointer links. A linked list is accessed by keeping a pointer to the first node of the list. This pointer to the first node of a list is typically named head. Subsequent nodes are accessed via a link pointer member that is stored in each node.”

  3. OO Linked List • In the OO world, the Linked List (LL) is a class with the public interface described last time. • The pointer to the first node is a private data member of the LL class • Other private data members may be defined if needed (tail, number of elements, etc)

  4. Class Relationships • The List class contains a pointer to the first Node • The Node class contains a Data object as a data member – this is known as “composition” or “aggregation” • The List and Node cannot/should not/will not know anything about the Data. • The List and Node MUST use only the Data’s public interface

  5. LL diagram Other Private Data members Data Object Data Data Data Data head next next next List Object NodeObject A singly linked list

  6. Inserting into a Linked List • If the list is not sorted, why not always insert at the head? What’s the asymptotic performance of this kind of insert? • If the list is sorted, search to find the proper place in the list, then insert. What’s the asymptotic performance of this insert?

  7. Other Private Data members Insert 17 at the head 10 42 63 99 head next next next 17 Other Private Data members next 10 42 63 99 head next next next

  8. Other Private Data members Insert 17 in sorted order 10 42 63 99 head next next next Other Private Data members 10 17 42 63 99 head next next next next

  9. Finding an element • Starting with the head pointer in the List, follow the “next” links until you find the element in question or get to the end of the list • What is the worst case asymptotic performance of find( )? • How is the performance different in a sorted linked list?

  10. Pseudo-code for find( ) int find (Data d) { NodePtr p = list.head; while (p not NULL and it’s not the one you want) go to the next element If p not NULL, return success; If p is NULL, return failure }

  11. removal in Linked List • Find the node that contains the data element you want to remove. Make the node before the node that contains that element point to the node after the node that contains that element (NULL if the last element is being deleted), then free the memory for the node

  12. Diagram for remove( ) To remove( 17 ), make the node with 10 point to the node with 42, then free the memory for the node with 17. Other Private Data members 10 17 42 63 99 head next next next next

  13. Thinking about remove( ) • A problem – we can’t use find( ) because we need to modify the node before the one that has the element we want to delete. Besides, find() doesn’t return a pointer to a node. • Two solutions • The code in remove( ) keeps track of two pointers – one used to find the node to be removed and one to “trail behind” and point to the one before the one to be removed • Write a private method called “findPrevious( )” that is called from remove( ) that finds the node before the one with the specified element and returns a pointer to it

  14. remove (cont’d) • Problem – there may not be a node after the one being removed • Solution – not a problem. The next pointer of the node before the one being removed will become NULL without any special code

  15. remove( ) (cont’d) • Another problem -- there is no node before the node being removed. This can only occur when we are removing the first node. • Solution • Special code to handle this case

  16. remove( ) (cont’d) • What do we do if the element we are asked to delete is not in the list? • What do we do if duplicates are allowed in the list?

  17. remove( ) Pseudo-code remove (Data d) { if (removing first element) get a pointer to node being removed modify head pointer to point to 2nd node else get a pointer to the node being removed get a pointer to the node before the one to be removed (special code or findPrevious() ) modify next pointer of this “previous” nodefree memory of node being removed }

  18. Making remove() easier • The code that makes remove( ) somewhat complex is the code for finding the node that comes before the node you want to remove. You either have an extra pointer or write a special (private) function that we’ve called findPrevious() • An alternative is to build the necessary pointer into the linked list. This pointer (usually called “prev”) points to the node that comes before this node. Then we can write an alternative version of find( ) (let’s call it findIt() ) that returns a pointer to the node and use “prev” to access the previous node.

  19. Double Linked List Other Private Data members 10 17 42 63 99 head next next next next prev prev prev prev Now when we want to remove( 17 ), we can call findIt( 17 ) and use the “prev” pointer as the one that points to the node before the one we want to remove. Care must be taken to perform the pointer modifications in the correct order.

  20. Circular Linked List Some applications require a more sophisticated list. In this circular list, the “next” pointer of the last node points back to the first node instead of being NULL. This adds a little complication to find ( ). Other Private Data members 10 17 42 63 99 head next next next next next

  21. A Doubly Linked Circular List The most general kind of list is not only circular, but doubly linked. The “next” pointer of the last node points to the first node and the “prev” pointer of the first node points to the last node. Other Private Data members 10 17 42 63 99 head next next next next next prev prev prev prev prev