Académique Documents
Professionnel Documents
Culture Documents
Consider void B ( )
{
this code }
// ------------------------------
outline: void A ( )
B ();
// ------------------------------
int main ( )
{
A ();
return 0;
}
B
Consider the following:
A
main
main begins executing Push (mains ar)
main calls function A Push (As ar) Runtime Stack
function A calls function B Push (Bs ar)
function B returns Use info in Top ar to return control to A
Pop
Use info in Top ar to return control to main
function A returns
Pop
Use info in Top ar to return control to OS
main returns Pop
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.)
.
.
.
[0] -1
items top
Stack: Implementation Level
Using an array: Push ( 70 )
[MAX_ITEMS - 1]
.
.
.
70 [0] 0
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1]
.
.
.
28
70 [0] 1
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
.
.
.
88
28
70 [0] 2
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
.
.
.
88
28
70 [0] 1
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
.
.
95
28
70 [0] 2
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
.
95
28
70 [0] 1
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
Pop
.
95
28
70 [0] 0
items top
Stack: Implementation Level
Using an array: Push ( 70 )
Push ( 28)
[MAX_ITEMS - 1] Push ( 88)
Pop
. Push ( 95)
. Pop
Pop
.
Pop
95
28
70 [0] -1
items top
Stack: Implementation Level
Using a linked list:
NULL
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
70 NULL
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
28 70 NULL
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
88 28 70 NULL
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
28 70 NULL Pop
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
95 28 70 NULL Pop
Push ( 95 )
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
28 70 NULL Pop
Push ( 95 )
Pop
top
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
70 NULL Pop
Push ( 95 )
Pop
top Pop
Stack: Implementation Level
Using a linked list: Push ( 70 )
Push ( 28 )
Push ( 88 )
NULL Pop
Push ( 95 )
Pop
top Pop
Pop
Queue: Logical Level
An ordered group of homogeneous items or
elements in which items are added at one end
(the rear) and removed from the other end
(the front.)
items . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
rear -1
Queue: Implementation Level
Using an array: Option 1
items A . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
rear 0
Queue: Implementation Level
Using an array: Option 1
items A B . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
rear 1 Enqueue (B)
Queue: Implementation Level
Using an array: Option 1
items A B C . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
rear 2 Enqueue (B)
Enqueue (C)
Queue: Implementation Level
Using an array: Option 1
items A B C . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
rear 2 Enqueue (B)
Enqueue (C)
Dequeue(ch)
But now front is at position[1] , not [0]
items B C . . .
[0] [MAXQUEUE - 1]
front - fixed at [0] (similar to bottom of stack)
Enqueue (A)
rear 1 Enqueue (B)
Enqueue (C)
Dequeue(ch)
After the shifting
items
[0] [4]
front 0
rear -1
items A
[0] [4]
front 0
Enqueue (A)
rear 0
items A B
[0] [4]
front 0
Enqueue (A)
Enqueue (B)
rear 1
items A B C
[0] [4]
front 0
Enqueue (A)
Enqueue (B)
rear 2
Enqueue (C)
items A B C D
[0] [4]
front 0
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Keep track of both front and rear
items A B C D
[0] [4]
front 1
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear
items A B C D
[0] [4]
front 2
Enqueue (A)
Enqueue (B)
rear 3
Enqueue (C)
Enqueue (D)
Dequeue (ch)
Keep track of both front and rear
Dequeue (ch)
items A B C D E
[0] [4]
items
[0] [4]
items A
[0] [4]
rear 0
items A B
[0] [4]
items A B C
[0] [4]
items A B C D
[0] [4]
items A B C D
[0] [4]
items A B C D
[0] [4]
items A B C D
[0] [4]
items A B C D
[0] [4]
NULL
front
NULL
rear
Queue: Implementation Level
Using a linked list:
A NULL
front
Enqueue (A)
rear
Queue: Implementation Level
Using a linked list:
A B NULL
front
A B C NULL
front
B C NULL
front
C NULL
front
Enqueue (A)
rear Enqueue (B)
Enqueue (C)
Dequeue (ch)
Dequeue (ch)
Queue: Implementation Level
Using a linked list:
NULL
front
NULL
Enqueue (A)
rear Enqueue (B)
Enqueue (C)
Dequeue (ch)
Dequeue (ch)
Dequeue (ch)