10.Elementary data structures Hsu, Lih-Hsing
Computer Theory Lab. Chapter 11P 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.
Computer Theory Lab. Chapter 11P.3 An array implementation of a stack S
Computer Theory Lab. Chapter 11P.4 empty, underflows, overflows STACK_EMPTY(S ) 1 if top[S] = 0 2 then return TRUE 3 else return FALSE
Computer Theory Lab. Chapter 11P.5 PUSH(S,x) 1 top[S] top[S]+1 2 S[top[S]] x POP(S ) 1 if STACK-EMPTY(S ) 2 then error “underflow” 3 else top[S] top[s] -1 4 return S[top[S] + 1]
Computer Theory Lab. Chapter 11P.6 An array implementation of a queue Q
Computer Theory Lab. Chapter 11P.7 ENQUEUE(Q,S ) 1Q[tail[Q]] x 2 if tail[Q]= length[Q] 3then tail[Q] 1 4else tail[Q] tail[Q]+1
Computer Theory Lab. Chapter 11P.8 DEQUEUE(Q ) 1 x Q[head[Q]] 2 if head[Q] = length[Q] 3then head[Q] 1 4else head[Q] head[Q] +1 5 return x
Computer Theory Lab. Chapter 11P Linked lists
Computer Theory Lab. Chapter 11P.10 LIST_SEARCH(L,k) 1 x ≠ head[L] 2 while x ≠ NIL and key[x] ≠ k 3do x ≠ next[x] 4 return x O(n)
Computer Theory Lab. Chapter 11P.11 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)
Computer Theory Lab. Chapter 11P.12 LIST_DELETE(L,x) (Call LIST_SEARCH first O(n)) 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)
Computer Theory Lab. Chapter 11P.13 A Sentinel is a dummy object that allows us to simplify boundary conditions,
Computer Theory Lab. Chapter 11P.14 LIST_DELETE ’ (L,x) 1 next[prev[x]] next[x] 2 prev[next[x]] prev[x]
Computer Theory Lab. Chapter 11P.15 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
Computer Theory Lab. Chapter 11P.16 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]
Computer Theory Lab. Chapter 11P Implementing pointers and objects A multiple-array representation of objects
Computer Theory Lab. Chapter 11P.18 A single array representation of objects
Computer Theory Lab. Chapter 11P.19 Allocating and freeing objects--garbage collector
Computer Theory Lab. Chapter 11P.20 Allocate_object( ),LIST_INSERT(L,4),Key(4)=25
Computer Theory Lab. Chapter 11P.21 LIST_DELETE(L,5), FREE_OBJECT(5)
Computer Theory Lab. Chapter 11P.22 ALLOCATE_OBJECT( ) 1if free = NIL 2then error “out of space” 3else x free 4free next[x] 5return x
Computer Theory Lab. Chapter 11P.23 FREE_OBJECT(x ) 1 next[x] free 2 free x
Computer Theory Lab. Chapter 11P.24 Two link lists
Computer Theory Lab. Chapter 11P Representing rooted trees
Computer Theory Lab. Chapter 11P.26 Binary trees
Computer Theory Lab. Chapter 11P.27 Rooted tree with unbounded branching
Computer Theory Lab. Chapter 11P.28 Other tree representation