Download presentation
Presentation is loading. Please wait.
Published byJean Shelton Modified over 9 years ago
1
Chapter 10 Elementary data structures Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.
2
20071109 chap10Hsiu-Hui Lee2 10.1 Stacks and queues Stacks and queues are dynamic set in which element removed from the set by the DELETE operation is prespecified. In a stack the element deleted from the set is the one most recently inserted; the stack implements a last-in, first-out, or LIFO, policy. Similarly, in a queue, the element deleted is implements a first-in, first-out, or FIFO, policy.
3
20071109 chap10Hsiu-Hui Lee3 An array implementation of a stack S PUSH(S, 17) PUSH(S, 3) POP(S)
4
20071109 chap10Hsiu-Hui Lee4 STACK_EMPTY(S ) 1 if top[S] = 0 2then return TRUE 3else return FALSE O(1) empty, underflows, overflows
5
20071109 chap10Hsiu-Hui Lee5 PUSH(S,x) 1 top[S] top[S]+1 2 S[top[S]] x O(1) POP(S ) 1 if STACK-EMPTY(S ) 2then error “ underflow ” 3else top[S] top[s] -1 4return S[top[S] + 1] O(1)
6
20071109 chap10Hsiu-Hui Lee6 An array implementation of a queue Q ENQUEUE(Q, 17) ENQUEUE (Q, 3) ENQUEUE (Q, 5) DEQUEUE(Q)
7
20071109 chap10Hsiu-Hui Lee7 ENQUEUE(Q, x ) 1Q[tail[Q]] x 2 if tail[Q]= length[Q] 3 then tail[Q] 1 4 else tail[Q] tail[Q]+1 O(1)
8
20071109 chap10Hsiu-Hui Lee8 DEQUEUE(Q) 1 x Q[head[Q]] 2 if head[Q] = length[Q] 3 then head[Q] 1 4 else head[Q] head[Q] +1 5 return x O(1)
9
20071109 chap10Hsiu-Hui Lee9 10.2 Linked lists Doubly linked list L: key, next, prev If prev[x]=NIL, the element has no predecessor and is the first element, or head, of the list. If next[x]=NIL, the element has no successor and is the last element, or tail, of the list. head[L] points to the first element of the list. If head[L]=NIL, the list is empty.
10
20071109 chap10Hsiu-Hui Lee10 LIST-SEARCH(L, k) 1 x head[L] 2 while x ≠ NIL and key[x] ≠ k 3 do x next[x] 4return x Θ (n) in worst case LIST-SEARCH(L, 4) returns a pointer to the third element LIST-SEARCH(L, 7) returns NIL
11
20071109 chap10Hsiu-Hui Lee11 LIST-INSERT(L, x) 1 next[x] head[L] 2 if head[L] ≠ NIL 3then prev[head[L]] x 4 head[L] x 5 prev[x] NIL O(1) LIST-INSERT(L, 25)
12
20071109 chap10Hsiu-Hui Lee12 LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2then next[prev[x]] next[x] 3else head[L] next[x] 4 if next[x] ≠ NIL 5 then prev[next[x] prev[x] O(1) or O(n) Delete a specified element (Call LIST_SEARCH first O(n)) LIST-DELETE(L, 4)
13
20071109 chap10Hsiu-Hui Lee13 Circular, doubly linked list with a sentinel A Sentinel is a dummy object to simplify boundary conditions. The sentinel nil[L] is placed between the head and the tail. next[nil[L]] points to the head of the list and prev[nil[L]] points to the tail. Both the next field of the tail and the prev field of the head point to nil[L]. We can eliminate the attribute head[L], since next[nil[L]] points to the head.
14
20071109 chap10Hsiu-Hui Lee14 LIST_DELETE ’ (L, x) 1 next[prev[x]] next[x] 2 prev[next[x]] prev[x] LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2then next[prev[x]] next[x] 3else head[L] next[x] 4 if next[x] ≠ NIL 5 then prev[next[x] prev[x] LIST-DELETE(L, 1)
15
20071109 chap10Hsiu-Hui Lee15 LIST_SEARCH ’ (L, k) 1 x next[nil[L]] 2 while x ≠ nil[L] and key[x] ≠ k 3 do x next[x] 4 return x LIST-SEARCH(L, k) 1 x head[L] 2 while x ≠ NIL and key[x] ≠ k 3 do x next[x] 4return x
16
20071109 chap10Hsiu-Hui Lee16 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] LIST-INSERT(L, x) 1 next[x] head[L] 2 if head[L] ≠ NIL 3then prev[head[L]] x 4 head[L] x 5 prev[x] NIL
17
20071109 chap10Hsiu-Hui Lee17 11.3 Implementing pointers and objects How to implement pointers and objects in languages, such as Fortran, that do not provide them?
18
20071109 chap10Hsiu-Hui Lee18 Multiple-array representation of objects
19
20071109 chap10Hsiu-Hui Lee19 A single array representation of objects
20
20071109 chap10Hsiu-Hui Lee20 Allocating and freeing objects-- garbage collector
21
20071109 chap10Hsiu-Hui Lee21 Allocate_object( ), LIST_INSERT(L,4), Key(4)=25
22
20071109 chap10Hsiu-Hui Lee22 LIST_DELETE(L,5), FREE_OBJECT(5)
23
20071109 chap10Hsiu-Hui Lee23 ALLOCATE_OBJECT( ) 1if free = NIL 2then error “ out of space ” 3else x free 4free next[x] 5return x FREE_OBJECT(x) 1 next[x] free 2 free x
24
20071109 chap10Hsiu-Hui Lee24 Two link lists
25
20071109 chap10Hsiu-Hui Lee25 10.4 Representing rooted trees
26
20071109 chap10Hsiu-Hui Lee26 Binary trees p[x], left[x], right[x]
27
20071109 chap10Hsiu-Hui Lee27 Rooted tree with unbounded branching
28
20071109 chap10Hsiu-Hui Lee28 Rooted tree with unbounded branching p[x], left-child[x], right-sibling[x]
29
20071109 chap10Hsiu-Hui Lee29 Other tree representation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.