ADT Stacks and Queues - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

ADT Stacks and Queues

Description:

ADT Stacks and Queues Stack: Logical Level An ordered group of homogeneous items or elements in which items are added and removed from only one end. – PowerPoint PPT presentation

Number of Views:128
Avg rating:3.0/5.0
Slides: 28
Provided by: facultyst60
Category:
Tags: adt | queues | stacks

less

Transcript and Presenter's Notes

Title: ADT Stacks and Queues


1
ADT Stacks and Queues
2
Stack 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.

3
Stack Logical Level
  • Stack Operations
  • Boolean IsEmpty ()
  • Boolean IsFull ()
  • Push (ItemType newitem)
  • void Pop ()
  • ItemType Top ()

4
Stack 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

5
Consider this codeoutline
  • // ------------------------------
  • void B ( )
  • // ------------------------------
  • void A ( )
  • B ()
  • // ------------------------------

6
Consider 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
7
Stack 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

8
General 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 ! ! !
9
Stack Implementation Level
  • Using an array

MAX_ITEMS - 1
. . .
0
-1
items
top
10
Stack Implementation Level
  • Using an array

Push ( 70 )
MAX_ITEMS - 1
. . .
70
0
0
items
top
11
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28)
MAX_ITEMS - 1
. . .
28
70
0
1
items
top
12
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88)
MAX_ITEMS - 1
. . .
88
28
70
0
2
items
top
13
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88) Pop
MAX_ITEMS - 1
. . .
88
28
70
0
1
items
top
14
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88) Pop Push ( 95)
MAX_ITEMS - 1
. . .
95
28
70
0
2
items
top
15
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop
MAX_ITEMS - 1
. . .
95
28
70
0
1
items
top
16
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop Pop
MAX_ITEMS - 1
. . .
95
28
70
0
0
items
top
17
Stack Implementation Level
  • Using an array

Push ( 70 ) Push ( 28) Push ( 88) Pop Push (
95) Pop Pop Pop
MAX_ITEMS - 1
. . .
95
28
70
0
-1
items
top
18
Stack Implementation Level
  • Using a linked list

NULL
top
19
Stack Implementation Level
  • Using a linked list

Push ( 70 )
NULL
70
top
20
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 )
NULL
28
70
top
21
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 )
NULL
88
28
70
top
22
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop
NULL
28
70
top
23
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
)
NULL
95
28
70
top
24
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop
NULL
28
70
top
25
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop Pop
NULL
70
top
26
Stack Implementation Level
  • Using a linked list

Push ( 70 ) Push ( 28 ) Push ( 88 ) Pop Push ( 95
) Pop Pop Pop
NULL
top
27
To be continued . . .
Write a Comment
User Comments (0)
About PowerShow.com