1 / 31

5. LinkedList

5. LinkedList. Yan Shi CS/SE 2630 Lecture Notes. Linked List. Unsorted List Sorted List Double linked list Circular linked list. Sorted and Unsorted Lists. UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST

crete
Download Presentation

5. LinkedList

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. 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes

  2. Linked List • Unsorted List • Sorted List • Double linked list • Circular linked list

  3. Sorted and Unsorted Lists UNSORTED LIST Elements are placed into the list in no particular order. SORTED LIST List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) .

  4. change state observe state process all ADT Unsorted List Operations Transformers • MakeEmpty • PutItem • DeleteItem Observers • IsFull • GetLength • GetItem Iterators • ResetList • GetNextItem

  5. ‘X’‘C’‘L’ class UnsortedType<char> UnsortedType Private data: length 3 listData currentPos ? MakeEmpty ~UnsortedType GetItem PutItem DeleteItem . . . GetNextItem

  6. ‘X’‘C’‘L’ Inserting ‘B’ into an Unsorted List Private data: length 3 listData currentPos ?

  7. ‘X’‘C’‘L’ location = new NodeType; item location ‘B’ Private data: length 3 listData currentPos ?

  8. ‘X’‘C’‘L’ location->info = item ; • item • location ‘B’ ‘B’ Private data: length 3 listData currentPos ?

  9. ‘X’‘C’‘L’ location->next = listData ; ‘B’ • item • location ‘B’ Private data: length 3 listData currentPos ?

  10. ‘X’‘C’‘L’ listData = location ; • item • location ‘B’ ‘B’ Private data: length 3 listData currentPos ?

  11. ‘X’‘C’‘L’ length++ ; • item • location ‘B’ Private data: length 4 listData currentPos ? ‘B’

  12. Sorted Type Class Interface Diagram SortedType class MakeEmpty Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos IsFull GetLength GetItem InsertItem DeleteItem ResetList GetNextItem

  13. InsertItem algorithm for Sorted Array Based List • Find proper location for the new element in the sorted list. • Create space for the new element by moving down all the list elements that will follow it. • Put the new element in the list. • Increment length.

  14. DeleteItem algorithm for Sorted Array Based List • Find the location of the element to be deleted from the sorted list. • Eliminate space occupied by the item by moving up all the list elements that follow it. • Decrement length.

  15. ‘C’‘L’‘X’ class SortedType<char> SortedType Private data: length 3 listData currentPos ? MakeEmpty ~SortedType RetrieveItem InsertItem DeleteItem . . . GetNextItem

  16. InsertItemalgorithm for Sorted Linked List • Find proper position for the new element in the sorted list using two pointers predLoc and location, where predLoc trails behind location. • Obtain a node for insertion and place item in it. • Insert the node by adjusting pointers. • Increment length.

  17. The Inchworm Effect

  18. ‘C’‘L’‘X’ moreToSearch Inserting ‘S’ into a Sorted List predLoc location Private data: length 3 listData currentPos ?

  19. ‘C’‘L’‘X’ moreToSearch true Finding proper position for ‘S’ predLoc location NULL Private data: length 3 listData currentPos ?

  20. ‘C’‘L’‘X’ moreToSearch true Finding proper position for ‘S’ predLoc location Private data: length 3 listData currentPos ?

  21. ‘C’‘L’‘X’ moreToSearch false Finding Proper Position for ‘S’ predLoc location Private data: length 3 listData currentPos ?

  22. ‘S’ moreToSearch false Inserting ‘S’ into Proper Position predLoc location Private data: length 4 listData currentPos ‘C’‘L’‘X’

  23. What is a Circular Linked List? • A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

  24. External Pointer to the Last Node

  25. Why Circular Linked list? • It doesn’t make any operation much shorter or simpler… • It is useful for applications that require access to both ends of the list.

  26. What is a Doubly Linked List? • A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

  27. Linking the New Node into the List

  28. Deleting from a Doubly Linked List

  29. What are Header and Trailer Nodes? • A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. • A Trailer Node is a node at the end of a list that contains a key larger than any possible key. • Both header and trailer are placeholding nodes used to simplify list processing: we never have to handle special cases when inserting/deleting the first/end node.

  30. A Sorted list Stored in an Array of Nodes

  31. An Array with Linked List of Values and Free Space

More Related