Title: ADT Stacks and Queues
1ADT Stacks and Queues
2Stack Logical Level
- An ordered group of homogeneous items or
elements in which items are added and removed
from only one end. - A stack is also called a Last In First Out (LIFO)
data structure.
3Stack Logical Level
- Stack Operations
- Boolean IsEmpty ()
- Boolean IsFull ()
- Push (ItemType newitem)
- void Pop ()
- ItemType Top ()
4Stack Application Level
- A runtime stack of activation records (ar) is
maintained as a program executes to track
function calls and scopes. - Each activation record contains
- space for local variables and parameters
- ptr to dynamic parent
- ptr to static parent
- return address
5Consider this codeoutline
- // ------------------------------
- void B ( )
-
-
- // ------------------------------
- void A ( )
-
- B ()
-
- // ------------------------------
6Consider the following
B
A
main
- main begins executing
- main calls function A
- function A calls function B
- function B returns
- function A returns
- main returns
-
- Push (mains ar)
- Push (As ar)
- Push (Bs ar)
- Use info in Top ar to return control to A
- Pop
- Use info in Top ar to return control to main
- Pop
- Use info in Top ar to return control to OS
- Pop
Runtime Stack
7Stack Application Level
- Stacks can be used to analyze nested expressions
with grouping symbols to determine if they are
well-formed (all grouping symbols occur in
matching pairs and are nested properly.) - ( ( xxx ) x xx) is well-formed
- ( ( xxx x ) is ill-formed
8General Algorithm
( ( x x x ) x x x )
- get next symbol
- set balanced flag to true
- while (there are more input symbols and
expression still balanced) - if (next symbol is opening symbol)
- Push symbol onto stack
- else
- if (next symbol is closing symbol)
- if (stack is empty)
- set balanced to false
- else
- use Top to get copy of opening symbol on
top of stack - Pop the stack
- if (opening symbol does not match closing
symbol) - set balanced to false
- else
- ignore symbol
- get next symbol
- if (balanced and stack is empty)
- well-formed
(
(
Stack
Expression is Well-formed ! ! !
9Stack Implementation Level
MAX_ITEMS - 1
. . .
0
-1
items
top
10Stack Implementation Level
Push ( 70 )
MAX_ITEMS - 1
. . .
70
0
0
items
top
11Stack Implementation Level
Push ( 70 ) Push ( 28)
MAX_ITEMS - 1
. . .
28
70
0
1
items
top
12Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88)
MAX_ITEMS - 1
. . .
88
28
70
0
2
items
top
13Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88) Pop
MAX_ITEMS - 1
. . .
88
28
70
0
1
items
top
14Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95)
MAX_ITEMS - 1
. . .
95
28
70
0
2
items
top
15Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop
MAX_ITEMS - 1
. . .
95
28
70
0
1
items
top
16Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop Pop
MAX_ITEMS - 1
. . .
95
28
70
0
0
items
top
17Stack Implementation Level
Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop Pop Pop
MAX_ITEMS - 1
. . .
95
28
70
0
-1
items
top
18Stack Implementation Level
NULL
top
19Stack Implementation Level
Push ( 70 )
NULL
70
top
20Stack Implementation Level
Push ( 70 ) Push ( 28 )
NULL
28
70
top
21Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 )
NULL
88
28
70
top
22Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop
NULL
28
70
top
23Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
)
NULL
95
28
70
top
24Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop
NULL
28
70
top
25Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop Pop
NULL
70
top
26Stack Implementation Level
Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop Pop Pop
NULL
top
27To be continued . . .