Chapter 22 – Data Structures and Collections - PowerPoint PPT Presentation

kimi
chapter 22 data structures and collections n.
Skip this Video
Loading SlideShow in 5 Seconds..
Chapter 22 – Data Structures and Collections PowerPoint Presentation
Download Presentation
Chapter 22 – Data Structures and Collections

play fullscreen
1 / 118
Download Presentation
123 Views
Download Presentation

Chapter 22 – Data Structures and Collections

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

  1. Chapter 22 – Data Structures and Collections Outline 22.1 Introduction 22.2 Self-Referential Classes 22.3 Linked Lists 22.4 Stacks 22.5 Queues 22.6 Trees 22.6.1 Binary Search Tree of Integer Values 22.6.2 Binary Search Tree of IComparable Objects 22.7 Collection Classes 22.7.1 Class Array 22.7.2 Class ArrayList 22.7.3 Class Stack 22.7.4 Class Hashtable

  2. 22.1  Introduction • Dynamic data structures • Grow and shrink at execution time • Linked Lists: • Lined up in a row • Insertions and removals can occur anywhere in the list • Stacks: • Insertions and removals only at top • Queues: • Insertions made at back, removals from front • Binary Trees: • Facilitate high-speed searching and sorting of data • Efficient elimination of duplicate items

  3. 22.2  Self-Referential Classes • Contains a reference member to an object of the same class type • Reference can be used to link objects of the same type together • Dynamic data structures require dynamic memory allocation • Ability to obtain memory when needed and release memory when it is not needed • Uses new operator • Ex: Node nodeToAdd = new Node(10);

  4. NodeClass.cpp1 of 2 Outline

  5. Outline NodeClass.cpp2 of 2


  6. 22.2  Self-Referential Classes Fig. 22.2 Two self-referential class objects linked together.

  7. 22.3  Linked Lists • Linked List: • Linear collection of self-referential nodes connected by links • Nodes: class objects of linked-lists • Programs access linked lists through a reference to first node • Subsequent nodes accessed by link-reference members • Last node’s link set to null to indicate end of list • Nodes can hold data of any type • Nodes created dynamically • Similar to arrays, however: • Arrays are a fixed size • Linked lists have no limit to size • More nodes can be added as program executes

  8. 22.3  Linked Lists Fig. 22.3 Graphical representation of a linked list.

  9. Outline ListNode.h1 of 2

  10. Outline ListNode.h2 of 2

  11. Outline ListNode.cpp1 of 1

  12. Outline List.h1 of 2

  13. Outline List.h2 of 2

  14. Outline List.cpp1 of 6

  15. Outline List.cpp2 of 6

  16. Outline List.cpp3 of 6

  17. Outline List.cpp4 of 6

  18. Outline List.cpp5 of 6

  19. Outline List.cpp6 of 6

  20. Outline EmptyListException.h1 of 1

  21. Outline EmptyListException.cpp1 of 1

  22. Outline ListTest.cpp1 of 3

  23. Outline ListTest.cpp2 of 3

  24. Outline ListTest.cpp3 of 3

  25. 22.3  Linked Lists Fig. 22.11 Graphical representation of the InsertAtFront operation.

  26. 22.3  Linked Lists Fig. 22.12 Graphical representation of the InsertAtBack operation.

  27. 22.3  Linked Lists Fig. 22.13 Graphical representation of the RemoveFromFront operation.

  28. 22.3  Linked Lists Fig. 22.14 Graphical representation of the RemoveFromBack operation.

  29. 22.4  Stacks • Special version of linked list: • Last-in, first-out (LIFO) data structure: • Takes and releases new nodes only at top • Operations: • Push: adds new node to top of stack • Pop: removes top node from stack • Can be used for: • Storing return addresses • Storing local variables

  30. Outline StackInheritance.h1 of 1

  31. Outline StackInheritance.cpp1 of 1

  32. Outline StackInheritanceTest.cpp1 of 2

  33. Outline StackInheritanceTest.cpp2 of 2

  34. Outline StackComposition.h1 of 1

  35. Outline StackComposition.cpp1 of 2

  36. Outline StackComposition.cpp2 of 2

  37. 22.5  Queues • First-in, first-out (FIFO) data structure • Nodes removed from head, added to tail • Operations: • Enqueue: insert node • Dequeue: remove node • Many computer applications: • Printer spooling • Information packets on networks

  38. Outline QueueInheritance.h1 of 1

  39. Outline QueueInheritance.cpp1 of 1

  40. Outline QueueTest.cpp1 of 3

  41. Outline QueueTest.cpp2 of 3

  42. Outline QueueTest.cpp3 of 3

  43. 22.6  Trees • Tree: non-linear, two-dimensional data structure • Binary tree: • Contain two links • Root node: first node in a tree • Links refer to child nodes • Node with no children is a leaf node • Binary Search tree: • Values in left subtree are less than the value of the subtree’s parent • Values in right subtree are greater than the value of the subtree’s parent

  44. 22.6.1 Binary Search Tree of Integer Values • Post-order traversal • Get data from left child of node • Get data from right child of node • Get data from node • Level-order traversal • Visit nodes of tree row by row, from left to right

  45. B A D C 22.6.1 Binary Search Tree of Integer Values Fig. 22.23 Graphical representation of a binary tree.

  46. 47 25 77 11 43 65 93 7 17 31 44 68 22.6.1 Binary Search Tree of Integer Values Fig. 22.24 Binary search tree containing 12 values.

  47. Outline TreeNode.h1 of 3

  48. Outline TreeNode.h2 of 3

  49. Outline TreeNode.h3 of 3