Professional Documents
Culture Documents
Lecture #5
• Stacks
2
Recap
• Advanced Linked Lists
– Tail Pointers
– Doubly-linked Lists
Stacks
Why should you care?
Solving mazes
Undo in your word processor
Evaluating math expressions
Tracking where to return from C++
function calls
So pay attention!
4
Just like a stack of plates, the last item pushed onto the
top of a stack is the first item to be removed.
Stack operations:
• put something on top of the stack (PUSH)
• remove the top item (POP)
• look at the top item, without removing it
• check to see if the stack is empty
Note: You can only access the top item of the stack,
since the other items are covered.
6
Stacks
int main(void)
class Stack // stack of ints {
{ Stack is;
public:
Stack(); // c’tor is.push(10);
is.push(20);
void push(int i);
int pop(); ...
bool is_empty(void); }
int peek_top();
private:
... Answer:
}; How about an array
and a counter variable
to track where the
top of the stack is?
Question:
What type of data structure can we
use to implement our stack?
Implementing a Stack
7
int pop() {
if (m_top == 0) return -1; // underflow
m_top -= 1;
return m_stack[m_top];
...
private:
int m_stack[SIZE];
int m_top;
};
Stacks
8
int main(void)
const int SIZE = 100; {
class Stack Stack is;
{ Currently, our m_top points to the int a;
The
next second
open slot item
in the we
stack…
public: 1 10
>=>=100?
>= 100?
100?
push7 =will
10
5
m_stack[1]
m_stack[0]
m_stack[1]
Stack() { m_top be ==placed
0; } 10
7
5 in is.push(5);
But we
void push(int want
slot
val)#1to
{of return the top item
our stack. is.push(10);
already
if (m_top pushed
>= SIZE) on the//stack.
return; overflow a = is.pop();
m_stack[m_top] = val; cout << a;
m_top So
+= first
1; we must decrement our is.push(7);
} m_top
2 == variable…
0??
}
return
int pop() { m_stack[1] is
So we
if (m_top == return
0) return 10 -1; // underflow m_top 2
0
1
m_top -= 1;
return m_stack[m_top]; m_stack
} 0 5
...
1 7
10
2
private: ... …
int m_stack[SIZE];
10
7 99
int m_top; 5 a
}; 10
Stacks
9
Always Remember:
const int SIZE = 100; When we push, we:
class Stack
{ A. Store the new item in m_stack[m_top]
public: B. Post-increment our m_top variable
Stack() { m_top = 0; } (post means we do the increment after storing)
void push(int val) {
if (m_top >= SIZE) return; // overflow
m_stack[m_top] = val;
m_top += 1;
}
int pop() {
if (m_top == 0) return -1; // underflow
m_top -= 1;
return m_stack[m_top]; Always Remember:
}
When we pop, we:
... A. Pre-decrement our m_top variable
private: B. Return the item in m_stack[m_top]
int m_stack[SIZE]; (pre means we do the
int m_top; decrement before returning)
};
Stacks
10
Stack Challenge
Show the resulting stack after the following program runs:
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> istack; // stack of ints
istack.push(6);
for (int i=0;i<2;i++)
{
int n = istack.top();
istack.pop();
istack.push(i);
istack.push(n*2);
}
}
12
Stack Challenge
Show the resulting stack after the following program runs:
#include <iostream>
#include <stack>
using namespace std;
int main()
{ i 0
1
stack<int> istack; // stack of ints
n
istack.push(6);
for (int i=0;i<2;i++) 24
{ 12
1
int n = istack.top();
6
0
istack.pop();
istack.push(i);
istack.push(n*2);
}
}
13
void bar(int b)
{ When you pass a value to a function,
cout << b << endl; the CPU pushes that value onto a stack
} in the computers memory.
void foo(int a)
{ … when your function returns, the values
cout << a << endl; are popped off the stack and go away.
10
bar(a*2);
} Output: b
10
int main(void) Every a a local
5 time you declare 5 variable,
{ program pushesxit on the PC’s stack
your10
int x = 5; automatically!
5
foo( 5
x );
}
Undo!
15
As we’ll
Postfix notation is another waysee, postfix
to write expressions
algebraic have–
expressions
here the operator follows
no the
suchoperands: AB+
ambiguity!
Infix Postfix
Here are some infix 15 + 6 15 6 +
If you’ve ever
expressions andused an9 HP
– 4 Is that9 (5+10)
4 - *3
calculator, you’ve used
their postfix
(15 + 6) * 5 15or
6 + 5 *
equivalents:
postfix notation!
7 * 6 + 5 5 +7(10
6 ** 53)+
To3 understand
+ (4 * 5) infix
3 4 5expressions,
* + the
computer has to be equipped with
Postfix expressions are easier forprecedence
a computer torules!
compute than
infix expressions, because they’re unambiguous.
Class Challenge
Given the following postfix expression: 6 8 2 / 3 * -
Show the contents of the stack *after* the 3 has been
processed by our postfix evaluation algorithm.
Reminder:
1. Start with the left-most token.
2. If the token is a number:
a. Push it onto the stack
3. If the token is an operator:
a. Pop the top value into a variable called v2, and the
second-to-top value into v1.
b. Apply operator to the two #s (e.g., v1 / v2)
c. Push the result of the operation on the stack
4. If there are more tokens, advance to the next token and go
back to step #2
5. After all tokens have been processed, the top # on the stack
is the answer!
19
0 1 2 3 4 5 6 7
Start 0
(1,1) 1
2
3
4 Finish
5 (6,6)
6 X
7
22
sx,sy = 1,1
0 1 2 3 4 5 67
1. PUSH starting point onto the stack.
2. Mark the starting point as “discovered.”
0 ?
1 ? ##?
3. If the stack is empty, there is
NO SOLUTION and we’re done!
2 ?
#
3
4. POP the top point off of the stack. 4
5. If we’re at the endpoint, DONE! Otherwise… 5
6. If slot to the WEST is open & is undiscovered 6 x
Mark (curx-1,cury) as “discovered” 7
PUSH (curx-1,cury) on stack.
7. If slot to the EAST is open & is undiscovered 1,1 == 6,6?
Mark (curx+1,cury) as “discovered” Not yet!
PUSH (curx+1,cury) on stack.
8. If slot to the NORTH is open & is undiscovered
Mark (curx,cury-1) as “discovered”
PUSH (curx,cury-1) on stack.
9. If slot to the SOUTH is open & is undiscovered
Mark (curx,cury+1) as “discovered”
PUSH (curx,cury+1) on stack.
1,2
10. GOTO step #3 2
1,1
cur = 1,1
Solving a Maze with a Stack!
24
0 1 2 3 4 5 67
1. PUSH starting point onto the stack. 0
2. Mark the starting point as “discovered.” 1 ?#
#
3. If the stack is empty, there is 2 ? #?
NO SOLUTION and we’re done! 3 ?
#
4. POP the top point off of the stack. 4
5. If we’re at the endpoint, DONE! Otherwise… 5
6. If slot to the WEST is open & is undiscovered 6 x
Mark (curx-1,cury) as “discovered” 7
PUSH (curx-1,cury) on stack.
7. If slot to the EAST is open & is undiscovered 1,2 == 6,6?
Mark (curx+1,cury) as “discovered” Not yet!
PUSH (curx+1,cury) on stack.
8. If slot to the NORTH is open & is undiscovered
Mark (curx,cury-1) as “discovered”
PUSH (curx,cury-1) on stack.
9. If slot to the SOUTH is open & is undiscovered
Mark (curx,cury+1) as “discovered”
PUSH (curx,cury+1) on stack.
1,3
2
10. GOTO step #3 2,1
cur = 1,2
Solving a Maze with a Stack!
25
0 1 2 3 4 5 67
1. PUSH starting point onto the stack. 0
2. Mark the starting point as “discovered.” 1 ##
3. If the stack is empty, there is 2 ?
#
NO SOLUTION and we’re done! 3 ? #?
4. POP the top point off of the stack. 4 ?
5. If we’re at the endpoint, DONE! Otherwise… 5
6. If slot to the WEST is open & is undiscovered 6 x
Mark (curx-1,cury) as “discovered” 7
PUSH (curx-1,cury) on stack.
7. If slot to the EAST is open & is undiscovered 1,3 == 6,6?
Mark (curx+1,cury) as “discovered” Not yet!
PUSH (curx+1,cury) on stack.
8. If slot to the NORTH is open & is undiscovered
Mark (curx,cury-1) as “discovered”
PUSH (curx,cury-1) on stack.
9. If slot to the SOUTH is open & is undiscovered
Mark (curx,cury+1) as “discovered”
PUSH (curx,cury+1) on stack.
1,3
10. GOTO step #3 2,1
cur = 1,3
Solving a Maze with a Stack!
26
0 1 2 3 4 5 67
1. PUSH starting point onto the stack.
2. Mark the starting point as “discovered.”
0 ?
1 ?##
# ?
3. If the stack is empty, there is 2 #?
NO SOLUTION and we’re done! 3 #
4. POP the top point off of the stack. 4
5. If we’re at the endpoint, DONE! Otherwise… 5
6. If slot to the WEST is open & is undiscovered 6 x
Mark (curx-1,cury) as “discovered” 7
PUSH (curx-1,cury) on stack.
7. If slot to the EAST is open & is undiscovered 2,1 == 6,6?
Mark (curx+1,cury) as “discovered” Not yet!
PUSH (curx+1,cury) on stack.
8. If slot to the NORTH is open & is undiscovered
Mark (curx,cury-1) as “discovered”
PUSH (curx,cury-1) on stack.
9. If slot to the SOUTH is open & is undiscovered
Mark (curx,cury+1) as “discovered”
PUSH (curx,cury+1) on stack.
10. GOTO step #3 3
2,1
cur = 2,1
Solving a Maze with a Stack!
27
0 1 2 3 4 5 67
1. PUSH starting point onto the stack.
2. Mark the starting point as “discovered.”
0 ?
1 ##? ## ?
3. If the stack is empty, there is 2 # # ?
NO SOLUTION and we’re done! 3 #
or o ur
4. POP the top point off of the stack. 4 ze, !
5. If we’re at the endpoint, DONE! Otherwise… the 5ma olu ti on
n t o n o s
6. If slot to the WEST is open & is undiscovered
so l u tio he re6 is x
d
Mark (curx-1,cury) asn“discovered” the h at t .”
f i
’ll on stack. ti n g t 7
ea rc h
y ,
PUSH (curx-1,cury)w e d i ca
in& is undiscovered r st s
n tu a ll EAST is o u t, p t h -fi3,1 == 6,6?
e
7. If vslot
E Mark to the
m p ty open
a “ de
w i ll e
(curx+1,cury) as “discovered” alle d Not yet!
c k
staPUSH (curx+1,cury) onrstack. mi s c
ith
oopen & is undiscovered
g a l g
8. If slot to the NORTH
a r c h in is
is se(curx,cury-1) as “discovered”
Mark
ThPUSH (curx,cury-1) on stack.
9. If slot to the SOUTH is open & is undiscovered
Mark (curx,cury+1) as “discovered”
PUSH (curx,cury+1) on stack.
3,2
10. GOTO step #3 3
4,1
cur = 3,1
28
Next Lecture
• Queues