1 / 81

C++ Programming: From Problem Analysis to Program Design, Second Edition

0. C++ Programming: From Problem Analysis to Program Design, Second Edition. Chapter 17: Linked Lists. 0. Objectives. In this chapter you will: Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists

Télécharger la présentation

C++ Programming: From Problem Analysis to Program Design, Second Edition

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. 0 C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists

  2. 0 Objectives In this chapter you will: • Learn about linked lists • Become aware of the basic properties of linked lists • Explore the insertion and deletion operations on linked lists • Discover how to build and manipulate a linked list • Learn how to construct a doubly linked list C++ Programming: From Problem Analysis to Program Design, Second Edition

  3. 0 Introduction • Data can be organized and processed sequentially using an array, called a sequential list • Problems with an array • Array size is fixed • Unsorted array: searching for an item is slow • Sorted array: insertion and deletion is slow C++ Programming: From Problem Analysis to Program Design, Second Edition

  4. 0 Linked Lists • Linked list: collection of components (nodes) • Every node (except last) has address of next node • Every node has two components • Address of the first node of the list is stored in a separate location, called head or first C++ Programming: From Problem Analysis to Program Design, Second Edition

  5. ‘X’ ‘C’ ‘L’ head 0 A Linked List • a linked list is a list in which the order of the components is determined by an explicit link member in each node • the nodes are structs--each node contains a component member and also a link member that gives the location of the next node in the list C++ Programming: From Problem Analysis to Program Design, Second Edition

  6. head “Ted” “Irv” “Lee” 0 Dynamic Linked List • in a dynamic linked list, nodes are linked together by pointers, and an external pointer (or head pointer) points to the first node in the list C++ Programming: From Problem Analysis to Program Design, Second Edition

  7. 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL 0 Nodes can be located anywhere in memory • the link member holds the memory address of the next node in the list head C++ Programming: From Problem Analysis to Program Design, Second Edition

  8. ‘A’6000 .info .link 0 Declarations for a Dynamic Linked List // Type DECLARATIONS struct NodeType { char info; NodeType* link; } // Variable DECLARATIONS NodeType* head; NodeType* ptr; 8

  9. ptr .info .link ptr ‘A’ 6000 .info .link *ptr ptr ‘A’ 6000 .info .link (*ptr).info ptr->info 0 Pointer Dereferencing and Member Selection ‘A’ 6000 ptr 9

  10. ptr .info .link 0 ptr is a pointer to a node ‘A’ 6000 ptr 10

  11. ptr 0 *ptr is the entire node pointed to by ptr ‘A’ 6000 .info .link *ptr 11

  12. ptr .info .link 0 ptr->infois a node member ‘A’ 6000 ptr->info (*ptr).info // equivalent 12

  13. ptr .info .link 0 ptr->linkis a node member ‘A’ 6000 ptr->link (*ptr).link // equivalent 13

  14. ptr 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  15. ptr 3000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  16. ptr 3000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  17. ptr 3000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  18. ptr 5000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  19. ptr 5000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  20. ptr 5000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  21. ptr 2000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  22. ptr 2000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  23. ptr 2000 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  24. ptr NULL 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  25. ptr NULL 3000 5000 2000 3000 “Ted” 5000 “Irv” 2000 “Lee” NULL head 0 Traversing a Dynamic Linked List //PRE: head points to a dynamic linked list ptr = head ; while (ptr != NULL) { cout << ptr->info ; // Or, do something else with node *ptr ptr = ptr->link ; } C++ Programming: From Problem Analysis to Program Design, Second Edition

  26. 0 Using Operator new If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated. The dynamically allocated object exists until the delete operator destroys it. 26

  27. ‘X’ ‘C’ ‘L’ head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location;

  28. ‘X’ ‘C’ ‘L’ head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; location

  29. ‘X’ ‘C’ ‘L’ head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; location

  30. ‘X’ ‘C’ ‘L’ head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; ‘B’ location

  31. ‘X’ ‘C’ ‘L’ head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; ‘B’ location

  32. head 0 Inserting a Node at the Front of a List ‘B’ item char item = ‘B’; NodeType* location; location = new NodeType; location->info = item; location->link = head; head = location; ‘X’ ‘C’ ‘L’ ‘B’ location

  33. 0 Using Operator delete The object currently pointed to by the pointer is deallocated, and the pointer is considered undefined. The object’s memory is returned to the free store. 33

  34. head 0 Deleting the First Node from the List item NodeType* tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

  35. head 0 Deleting the First Node from the List ‘B’ item NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

  36. head 0 Deleting the First Node from the List ‘B’ item NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

  37. head 0 Deleting the First Node from the List ‘B’ item NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; ‘B’ ‘X’ ‘C’ ‘L’ tempPtr

  38. head 0 Deleting the First Node from the List ‘B’ item NodeType * tempPtr; item = head->info; tempPtr = head; head = head->link; delete tempPtr; ‘X’ ‘C’ ‘L’ tempPtr

  39. 0 What is a List? • a list is a varying-length, linear collection of homogeneous elements • linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

  40. 0 struct NodeType // SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h ) struct NodeType { char item ; // Data (char to keep example simple) NodeType* link ; // link to next node in list } ; 40

  41. 0 // SPECIFICATION FILE DYNAMIC-LINKED SORTED LIST( slist2.h ) class SortedList2 { public : bool IsEmpty () const ; void Print ( ) const ; void InsertTop ( /* in */ char item ) ; void Insert ( /* in */ char item ) ; void DeleteTop ( /* out */ char& item ) ; void Delete ( /* in */ char item ); SortedList2 ( ) ; // Constructor ~SortedList2 ( ) ; // Destructor SortedList2 ( const SortedList2& otherList ) ; // Copy-constructor private : NodeType* head; } ; 41

  42. ‘C’ ‘L’ ‘X’ 0 class SortedList2 SortedList2 Private data: head ~SortedList2 IsEmpty Print InsertTop Insert DeleteTop Delete

  43. 0 Insert Algorithm • what will be the algorithm to Insert an item into its proper place in a sorted linked list? • that is, for a linked list whose elements are maintained in ascending order?

  44. 0 Insert algorithm for SortedList2 • find proper position for the new element in the sorted list using two pointers prevPtr and currPtr, where prevPtr trails behind currPtr • obtain a node for insertion and place item in it • insert the node by adjusting pointers

  45. 0 Implementing SortedList2Member Function Insert // DYNAMIC LINKED LIST IMPLEMENTATION (slist2.cpp) void SortedList2 :: Insert ( /* in */ char item ) // PRE: item is assigned && List components in ascending order // POST: item is in List && List components in ascending order { . . . }

  46. ‘C’ ‘L’ ‘X’ 0 Inserting ‘S’ into a Sorted List prevPtr currPtr Private data: head

  47. ‘C’ ‘L’ ‘X’ 0 Finding Proper Position for ‘S’ prevPtr currPtr NULL Private data: head

  48. ‘C’ ‘L’ ‘X’ 0 Finding Proper Position for ‘S’ prevPtr currPtr Private data: head

  49. ‘C’ ‘L’ ‘X’ 0 Finding Proper Position for ‘S’ prevPtr currPtr Private data: head

  50. ‘S’ 0 Inserting ‘S’ into Proper Position prevPtr currPtr Private data: head ‘C’ ‘L’ ‘X’

More Related