0% found this document useful (0 votes)
11 views

Lesson 40 - C++ ArrayLists

Uploaded by

kdudeteam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lesson 40 - C++ ArrayLists

Uploaded by

kdudeteam
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

COMPUTER SCIENCE - C++

LESSON 40: STACKS

OBJECTIVES: The student will use stacks to solve a non-recursive inorder tree
traversal.

ACTIVITIES/TIME: Two Days:

Day one: Presentation - 35 minutes


Lab Exercise - 15 minutes
Days two: Lab Exercise

MATERIALS: Student Outline O.A.40.1


Transparency T.A.40.1, Printing a Binary Tree
Handout H.A.40.1, apstack
Lab Exercise L.A.40.1, Inorder
Answer Sheet L.A.40.1

REFERENCES: Adams, Joel, Sanford Leestma, and Larry Nyhoff. C++, An


Introduction to Computing. Chapter 16.

Savitch, Walter. Problem Solving with C++. Pages 757-763.

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.

APCS - C++, Lesson 40 ©1998, ICT


APCS - C++, Lesson 40 ©1998, ICT
STUDENT OUTLINE

Lesson 40: Stacks

INTRODUCTION: When studying recursion you were introduced to the concept of a


stack. A stack is a linear data structure with well defined insertion
and deletion routines. The stack abstraction has been solved for
you in the apstack class. After covering the member functions
available in the apstack class the lab exercise will use stacks to
solve a non-recursive inorder tree traversal problem.

The key topics for this lesson are:

A. The Stack Abstract Data Type


B. Implementation Strategies for a Stack Type

VOCABULARY: STACK POP


PUSH TOP

DISCUSSION: A. The Stack Abstract Data Type

1. The stack object is a linear data structure, with each node or


cell holding the same data type.

2. All additions to and deletions from a stack occur at the top of


the stack. The last item pushed onto the stack will be the first
item removed. A stack is sometimes referred to as a LIFO
structure, which stands for Last-In, First-Out.

3. Two of the more important stack operations involve pushing


data onto a stack and popping data off the stack.

4. The push operation will look like this:

Push Operation
Top After 28

Top Before 73 73

New value 19 19

55 55
28
42 42

Stack Before Push Stack After Push

5. The pop operation will look like this:

Pop Operation:

Top Before
43

89 89 Top After

16 16
43 Value
extracted
42 42

Stack Before Pop Stack After Pop

See Handout H.A.40.1, 6. Here is a summary of the stack operations supported by


apstack.
the apstack class. See Handout H.A.40.1, apstack for the
details.

Constructors

apstack(); constructs an empty stack


apstack (const apstack &s); copy constructor
~apstack (); destructor

Assignment

const apstack& operator= (const apstack& rhs);

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.

// Example program of using apstack class

#include <iostream.h>
#include <apstack.h>

main()
{
apstack<int> stack;
int value;

for (int k=1; k<=5; k++)


stack.push(k);
while (!(stack.isEmpty()))
{
stack.pop(value);
cout << value << " ";
}
return 0;
}

B. Implementation Strategies for a Stack Type

1. The data structure used in the apstack class is an apvector.


This allows for resizing of the stack as needed to make it
larger. However the author of the apstack class did not resize
the apvector smaller as the stack was popped. A vector is an
appropriate data structure to use to implement a stack.

2. Another approach would be to use a linked list which would


support true dynamic resizing. As you push data onto the stack
another node is added to the appropriate end of the linked list.
When data is popped from the stack, the linked list would be
reduced in size.

3. A future lesson will teach you how to write templated classes.


You will change the implementation of the apstack class while
leaving the interface stable.

SUMMARY/REVIEW: The stack ADT is what makes recursive algorithms possible. In


the lab exercise you will gain a better understanding of the
recursive inorder function used to traverse a binary tree.

ASSIGNMENT: Lab Exercise L.A.40.1, Inorder

You might also like