Lesson 40 - C++ ArrayLists
Lesson 40 - C++ ArrayLists
OBJECTIVES: The student will use stacks to solve a non-recursive inorder tree
traversal.
INSTRUCTOR
NOTES: In this lesson, stacks will be taught as an abstraction. The apstack
class will be used in programs which need a stack data type. The
lab will require a stack of tree pointers to support a non-recursive
inorder function. The function will be given in pseudocode form.
You are encouraged to give a general overview of the algorithm
without providing detail. A transparency of a binary tree
T.A.40.1, Printing a Binary Tree, is provided as a sample tree to
work through. Label each of these nodes with an address,
preferably a hexadecimal value (0..9 & A..F characters). As you
work through the pseudocode, illustrate how the hexadecimal
addresses are pushed and popped off the stack to solve the
problem.
Push Operation
Top After 28
Top Before 73 73
New value 19 19
55 55
28
42 42
Pop Operation:
Top Before
43
89 89 Top After
16 16
43 Value
extracted
42 42
Constructors
Assignment
Accessors
const itemType & top() const; returns top element with no pop
bool isEmpty() const; return true if stack is empty, else false
int length() const; returns length of stack
Modifiers
void push (const itemType & item); push item onto top of stack
void pop (); pop top element
void pop (itemType& item); combines pop and top
void makeEmpty(); empties stack
7. The distinction between top and pop follows: the top operation
simply returns the top value in the stack while the pop
operation removes the top value. Notice that the pop operation
has been overloaded to either remove the top element, or to
remove and return the top element.
8. Here is a short program illustrating usage of the apstack class.
#include <iostream.h>
#include <apstack.h>
main()
{
apstack<int> stack;
int value;