290 likes | 296 Vues
This presentation covers the concepts of stacks, queues, and linked lists with array and pointer implementations. Learn about LIFO and FIFO policies and efficient data manipulation techniques. Explore the array and doubly linked list structures in detail. Implement pointers and objects in various programming languages. Understand how to efficiently manage objects in memory using arrays. Dive into the representation of rooted trees and binary trees.
E N D
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.
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. Hsiu-Hui Lee
An array implementation of a stack S PUSH(S, 17) PUSH(S, 3) POP(S) Hsiu-Hui Lee
empty, underflows, overflows STACK_EMPTY(S ) 1 if top[S] = 0 2 then return TRUE • else return FALSE O(1) Hsiu-Hui Lee
PUSH(S,x) 1 top[S]top[S]+1 2 S[top[S]] x O(1) POP(S ) 1 if STACK-EMPTY(S ) 2 then error “underflow” 3 else top[S] top[s] -1 4 return S[top[S] + 1] O(1) Hsiu-Hui Lee
An array implementation of a queue Q ENQUEUE(Q, 17) ENQUEUE(Q, 3) ENQUEUE(Q, 5) DEQUEUE(Q) Hsiu-Hui Lee
ENQUEUE(Q, x ) 1 Q[tail[Q]] x 2 if tail[Q]= length[Q] 3 then tail[Q] 1 • else tail[Q] tail[Q]+1 O(1) Hsiu-Hui Lee
DEQUEUE(Q) 1 x Q[head[Q]] 2 if head[Q] = length[Q] 3 thenhead[Q] 1 • else head[Q] head[Q] +1 • return x O(1) Hsiu-Hui Lee
10.2 Linked lists • Doubly linked listL: 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. Hsiu-Hui Lee
LIST-SEARCH(L, k) 1 xhead[L] 2 while x ≠ NIL and key[x] ≠ k 3 do xnext[x] • return x Θ(n) in worst case LIST-SEARCH(L, 4) returns a pointer to the third element LIST-SEARCH(L, 7) returns NIL Hsiu-Hui Lee
LIST-INSERT(L, x) 1 next[x] head[L] 2 ifhead[L] ≠ NIL 3 thenprev[head[L]] x 4 head[L] x 5 prev[x] NILO(1) LIST-INSERT(L, 25) Hsiu-Hui Lee
LIST-DELETE(L, 4) LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2 then next[prev[x]] next[x] 3 else 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)) Hsiu-Hui Lee
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. Hsiu-Hui Lee
LIST_DELETE’(L, x) 1 next[prev[x]] next[x] 2 prev[next[x]] prev[x] LIST-DELETE(L, 1) LIST_DELETE(L, x) 1 if prev[x] ≠ NIL 2 then next[prev[x]] next[x] 3 else head[L] next[x] 4 if next[x] ≠ NIL 5 then prev[next[x] prev[x] Hsiu-Hui Lee
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 xhead[L] 2 while x ≠ NIL and key[x] ≠ k 3 do xnext[x] • return x Hsiu-Hui Lee
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 ifhead[L] ≠ NIL 3 thenprev[head[L]] x 4 head[L] x 5 prev[x] NIL Hsiu-Hui Lee
11.3 Implementing pointers and objects • How to implement pointers and objects in languages, such as Fortran, that do not provide them? Hsiu-Hui Lee
Multiple-array representation of objects Hsiu-Hui Lee
A single array representation of objects Hsiu-Hui Lee
Allocating and freeing objects--garbage collector Hsiu-Hui Lee
Allocate_object( ), LIST_INSERT(L,4), Key(4)=25 Hsiu-Hui Lee
LIST_DELETE(L,5), FREE_OBJECT(5) Hsiu-Hui Lee
ALLOCATE_OBJECT( ) 1 if free = NIL 2 then error “out of space” 3 else x free 4 free next[x] 5 return x FREE_OBJECT(x) 1 next[x] free 2 free x Hsiu-Hui Lee
Two link lists Hsiu-Hui Lee
10.4 Representing rooted trees Hsiu-Hui Lee
Binary trees • p[x], left[x], right[x] Hsiu-Hui Lee
Rooted tree with unbounded branching Hsiu-Hui Lee
Rooted tree with unbounded branching • p[x], left-child[x], right-sibling[x] Hsiu-Hui Lee
Other tree representation Hsiu-Hui Lee