Title: TCP 1241 Tutorial 4 Week II
1TCP 1241Tutorial 4Week II
TCS 1011 Tutorial 7 Week VII
2Question 1
- Create a new project.
- Copy-paste the header file and save it.
- Copy-paste the main program and save it.
- Read and understand the codes.
- Write a driver program of your own which
exercises all the functions implemented.
3Warm Up
- What is the actual type of StackElement?
- e.g
- void push(const StackElement value)
-
-
-
2. What is the stacks size? 3. How do you
delete a value from a stack? 4. How do you
insert a value into a stack?
4Push
Variable myArray
void push(const StackElement value) if
(myTop lt STACK_CAPACITY - 1) myTop
myArraymyTop value
0
0x1001
-1
5Pop
Variable myArray
StackElement pop() if ( !empty() )
StackElement temp myArraymyTop
myTop-- return temp
else coutltltEmptyltltendl
0
0x1001
-1
6Code
Code
// Instantiate a stack object Stack
stack // Check whether the stack is empty
cout ltlt "Stack created. Empty? " ltlt boolalpha ltlt
stack.empty() ltlt endl // Pushing / insert
data into the stack object cout ltlt "\nPushing
data\n\n\n" cout ltlt
"How many elements to add to the stack? " int
numItems cin gtgt numItems for (int i 1
i lt numItems i) stack.push(i) // Pause
the program coutltltendl system("pause")
Use noboolalpha to reset.
7Code
Code
// Display the stacks content cout ltlt
"\n\nStack contents\n
\n\n" stack.display(cout) cout ltlt "Stack
empty? " ltlt stack.empty() ltlt endl cout ltlt
"Top value " ltlt stack.top() ltlt endl ltlt endl
system("pause") // Pop the content from
stack object one by one until it empty while (
!stack.empty() ) cout ltlt "Popping " ltlt
stack.top() ltlt endl stack.pop()
8Code
Code
cout ltlt "\n\nStack empty? " ltlt stack.empty()
ltlt endl cout ltlt "Top value " ltlt stack.top()
ltlt endl cout ltlt "Trying to pop " ltlt endl
stack.pop() cout ltlt endl
system("pause")
9Refreshment
- What is a const function?
- e.g StackElement top() const
- void display(ostream out)
const
10Refreshment
2. What is typedef? e.g typedef int
StackElement
11Refreshment
3. What is initialization list? e.g
StackStack() myTop(-1)
12Question 4
For the linked list implemented in Question 3,
write a member function bottom() that returns the
bottom element of the stack
- Hints
- How do you validate myTop is either NULL or not?
- What is the head of a stack?
- How do you traverse from the head to the botton
(tail) ? - How do you that the traversal has reached the
bottom?
13Code Bottom( )
Code
StackElement Stackbottom() if ( myTop ! 0
) StackNodePointer node myTop
while ( node-gtnext ! 0 ) node
node-gtnext return node-gtdata
cerr ltlt "Stack empty no bottom element --
returning garbage value\n" StackElement
garbage return garbage
14Question 4
For the linked list implemented in Question 3,
Write a function nthElement() to retrieve the
nth stack element ( counting from the top ).
15Code nthElement(int n)
Code
n
StackElement StacknthElement(int n)
StackNodePointer node myTop int counter
1 while (counter lt n node ! 0)
counter node node-gtnext
if (counter n) return node-gtdata
cerr ltlt "Stack has no " ltlt n ltlt "-th element\n"
cerr ltlt " - returning a garbage value\n"
StackElement garbage return garbage
counter
Q1
Q2
Q3
Q4
Q5
16Question 5
Why do we need DoubleStack ?
Analogy of DoubleStack
17Question 5
Question Suppose that some application requires
using two stacks whose elements are of the same
type. A natural storage structure of such a
two-stack data type would consist of two arrays
and two top pointers. Explain why this may not be
a space wise efficient implementation?
Answer Proposed storage method assumes that the
two stacks will always be of the same relative
fullness. This need not be the case one stack
could become full while the other remains empty.
18DoubleStack
Stack limit
-1
Q1
Q2
Q3
Q4
5
19DoubleStackfull
Stack limit
-1
Code
bool DoubleStackfull () return ( myTop0
1 myTop1 )
Q1
5
20DoubleStackpop
Code
Stack limit
-1
ElementType DoubleStackpop (int stackNum)
ElementType value -1 if (stackNum 1
myTop0 ! -1) value
datamyTop0 myTop0-- else
if ( myTop1 ! STACK_LIMIT ) value
datamyTop1 myTop1
cerr ltlt " Error " ltlt endl return value
Q1
Q2
Q3
Q4
5
21DoubleStackpush
1. What is the role of stacknum ?
2. What is the actual data type of value ?
Code
void DoubleStackpush (int stackNum, ElementType
value) if ( full() ) cerr ltlt "No
room left for stack elements\n" exit(1)
if (stackNum 1) myTop0
datamyTop0 value else
myTop1-- datamyTop1 value
Q1
Q2
Q3
Q4
Q5