Chapter 17 - Data Structures
Chapter 17 - Data Structures
17.1 Introduction
• Self-referential class
– Has pointer to object of same class
– Link together to form useful data structures
• Lists, stacks, queues, trees
– Terminated with NULL pointer
15 10
Data member
and pointer NULL pointer (points to nothing)
• Sample code
class Node {
public:
Node( int );
void setData( int );
int getData() const;
void setNextPtr( Node * );
const Node *getNextPtr() const;
private:
int data;
Node *nextPtr;
};
• Linked list
– Collection of self-referential class objects (nodes) connected
by pointers (links)
– Accessed using pointer to first node of list
• Subsequent nodes accessed using the links in each node
– Link in last node is null (zero)
• Indicates end of list
– Data stored dynamically
• Nodes created as necessary
• Node can have data of any type
Insert at back
a) firstPtr lastPtr newPtr
12 7 11 5
12 7 11 5
lastPtr->nextPtr = newPtr
lastPtr = newPtr
If list empty, then
firstPtr = lastPtr = newPtr
12 7 11 5
b) firstPtr lastPtr
tempPtr = firstPtr
12 7 11 5
tempPtr
firstPtr = firstPtr->next
If there are no more nodes,
firstPtr = lastPtr = 0
delete tempPtr
12 7 11 5
12 7 11 5
tempPtr = lastPtr
lastPtr = currentPtr
tempPtr
delete tempPtr
2003 Prentice Hall, Inc. All rights reserved.
15
? 1
Enter integer: 2
The list is: 2 1
? 2
Enter integer: 3
The list is: 2 1 3
? 2
Enter integer: 4
The list is: 2 1 3 4
? 4
4 removed from list
The list is: 3
? 4
3 removed from list
The list is empty
? 5
End list test
17.5 Stacks
• Stack
– Nodes can be added/removed from top
• Constrained version of linked list
• Like a stack of plates
– Last-in, first-out (LIFO) data structure
– Bottom of stack has null link
• Stack operations
– Push: add node to top
– Pop: remove node from top
• Stores value in reference variable
17.5 Stacks
• Stack applications
– Function calls: know how to return to caller
• Return address pushed on stack
C
• Most recent function call on top B B B
• If function A calls B which calls C: A A A A A
17.5 Stacks
• Upcoming program
– Create stack from list
• insertAtFront, removeFromFront
– Software reusability
• Inheritance
– Stack inherits from List
• Composition
– Stack contains a private List object
– Performs operations on that object
– Makes stack implementation simple
17.6 Queues
• Queue
– Like waiting in line
– Nodes added to back (tail), removed from front (head)
– First-in, first-out (FIFO) data structure
– Insert/remove called enqueue/dequeue
• Applications
– Print spooling
• Documents wait in queue until printer available
– Packets on network
– File requests from server
17.6 Queues
• Upcoming program
– Queue implementation
– Reuse List as before
• insertAtBack (enqueue)
• removeFromFront (dequeue)
4.4 dequeued
The list is empty
All nodes destroyed
All nodes destroyed
17.7 Trees
17.7 Trees
• Terminology
– Root node: first node on tree
– Link refers to child of node
• Left child is root of left subtree
• Right child is root of right subtree
– Leaf node: node with no children
– Trees drawn from root downwards
A D
17.7 Trees
47
25 77
11 43 65 93
7 17 31 44 68
17.7 Trees
• Inserting nodes
– Use recursive function
– Begin at root
– If current node empty, insert new node here (base case)
– Otherwise,
• If value > node, insert into right subtree
• If value < node, insert into left subtree
• If neither > nor <, must be =
– Ignore duplicate
17.7 Trees
• Tree traversals
– In-order (print tree values from least to greatest)
• Traverse left subtree (call function again)
• Print node
• Traverse right subtree
– Preorder
• Print node
• Traverse left subtree
• Traverse right subtree
– Postorder
• Traverse left subtree
• Traverse rigth subtree
• Print node
17.7 Trees
• Upcoming program
– Create 2 template classes
– TreeNode
• data
• leftPtr
• rightPtr
– Tree
• rootPtr
• Functions
– InsertNode
– inOrderTraversal
– preOrderTraversal
– postOrderTraversal