Download Docsity.com: A Comprehensive Collection of Academic Documents and more Slides Design and Analysis of Algorithms in PDF only on Docsity!
Figure 10.1 An array implementation of a stack §. Stack elements appear only in the lightly shaded
positions, (a) Stack S has 4 elements. The top element is 9. (b) Stack S' after the calls PUSH(S, 17)
and PUsH(S, 3). (c) Stack § after the call PoP(S) has returned the element 3, which is the one most
recently pushed. Although element 3 still appears in the array, it is no longer in the stack: the top is
element 17,
® docsity.com
STACK-EMPTY(S)
1 if top[S] =0
2 then return TRUE
3 else return FALSE
4 5 6 7 8 9 10 11
( ae Ts9T8 4
4 t
head(Q\=7 tail{Q] =
12 3 4 5 6 7 8 9 10 11 12
) O[3]5 BR is) 6 lols[4[i7
t 4
tail[(Q)=3 head{Q)=7
1 2 3 4 5 6 7 8 9 10 11 12
©) of[3|5 IR slo] sisi
{ t
tail[Q] =3 head{Q) =
Figure 10.2. A queue implemented using an array Q[1..12]. Queue elements appear only in the
lightly shaded positions. (a) The queue has 5 elements, in locations Q[7.. 11]. (b) The configuration
the queue after the calls ENQUEUE(Q, 17), ENQUEUE(Q, 3), and ENQUEUE(Q,5). (c) The
guration of the queue after the call DEQUEUE(Q) returns the key value 15 formerly at the head
fe queue. The new head has key 6. poet
ENQUEUE(Q, x)
1 Q[tail[Q]|] <x
2 if tail[Q] = length[Q|
3 then tail[Q| <— 1
4 else tail[O] < tail[Q]+ 1
DEQUEUE(Q)
1 x < Ofhead[Q]]
2 if head[Q]| = length[Q]
3 then head[Q] < 1
4 else head[Q| < head[Q]|+ 1
5S return x
LIST-INSERT(L, x)
1 next|x] < head[L]
2 if head[L| # NIL
3 then prev[head[L || <— x
4 head{|L| <x
5S prev[x] <— NIL
LIST-DELETE(L, x)
1 if prev[x] 4 NIL
2 then next[prev[x]] <— next[x]
3 else head[|L| <— next|x]
4 if next[x] 4 NIL
5 then prev[next[x]] < prev[x]
® docsity.com
LIST-DELETE'’ (L, x)
| next[prev[x]] <— next[x]
2 prev[next[x]] < prev[x]
LIST-INSERT’ (L, x)
1 next(x|] <— next[nil[L]]
2 prev[next[nil[L]]] <— x
3 next[nil[L]] <— x
4 prev[x| <— nil[L]
Figure 10.5 The linked list of Figure 10.3(a) represented by the arrays key, next, and prev. Each
vertical slice of the arrays represents a single object. Stored pointers correspond to the array indices
shown at the top; the arrows show how to interpret them. Lightly shaded object positions contain list
elements. The variable 1 keeps the index of the head.
® docsity.com
12 3 4 5 6 7 & 9 10 11 12 13 14 18 16 17 18 19 20 21 22 23 24
Ba]
key | prev
Hext
Figure 10.6 The linked list of Figures 10.3(a) and 10.5 represented in a single array A. Each list
element is an object that occupies a contiguous subarray of length 3 within the array. The three
fields key, next, and prev correspond to the offsets 0, 1, and 2, respectively. A pointer to an object is
an index of the first element of the object. Objects containing list elements are lightly shaded, and
arrows show the list ordering.
® docsity.com
FREE-OBJECT (x)
| next|x] < free
2 free<x
ree HD
next (4
Li :
key
1,[3] prev fi
Figure 10.8 Two linked lists, 1; (lightly shaded) and L (heavily shaded), and a free list (darkened)
intertwined.
® docsity.com
root{T]
aS EN
bps aa ye
ZG Z)
A 4)
A
Figure 10.9 The representation of a binary tree 7, Each node x has the fields plx] (top), fefrLr]
lower left), and right[x] (lower right). The key fields are not shown.
(® docsity.com