123 Views

Download Presentation
## Chapter 22 – Data Structures and Collections

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -

**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**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**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);**NodeClass.cpp1 of 2**Outline**Outline**NodeClass.cpp2 of 2**22.2 Self-Referential Classes**Fig. 22.2 Two self-referential class objects linked together.**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**22.3 Linked Lists**Fig. 22.3 Graphical representation of a linked list.**Outline**ListNode.h1 of 2**Outline**ListNode.h2 of 2**Outline**ListNode.cpp1 of 1**Outline**List.h1 of 2**Outline**List.h2 of 2**Outline**List.cpp1 of 6**Outline**List.cpp2 of 6**Outline**List.cpp3 of 6**Outline**List.cpp4 of 6**Outline**List.cpp5 of 6**Outline**List.cpp6 of 6**Outline**EmptyListException.h1 of 1**Outline**EmptyListException.cpp1 of 1**Outline**ListTest.cpp1 of 3**Outline**ListTest.cpp2 of 3**Outline**ListTest.cpp3 of 3**22.3 Linked Lists**Fig. 22.11 Graphical representation of the InsertAtFront operation.**22.3 Linked Lists**Fig. 22.12 Graphical representation of the InsertAtBack operation.**22.3 Linked Lists**Fig. 22.13 Graphical representation of the RemoveFromFront operation.**22.3 Linked Lists**Fig. 22.14 Graphical representation of the RemoveFromBack operation.**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**Outline**StackInheritance.h1 of 1**Outline**StackInheritance.cpp1 of 1**Outline**StackInheritanceTest.cpp1 of 2**Outline**StackInheritanceTest.cpp2 of 2**Outline**StackComposition.h1 of 1**Outline**StackComposition.cpp1 of 2**Outline**StackComposition.cpp2 of 2**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**Outline**QueueInheritance.h1 of 1**Outline**QueueInheritance.cpp1 of 1**Outline**QueueTest.cpp1 of 3**Outline**QueueTest.cpp2 of 3**Outline**QueueTest.cpp3 of 3**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**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**B**A D C 22.6.1 Binary Search Tree of Integer Values Fig. 22.23 Graphical representation of a binary tree.**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.**Outline**TreeNode.h1 of 3**Outline**TreeNode.h2 of 3**Outline**TreeNode.h3 of 3