1 / 29

Fundamentals of Stacks, Queues, and Linked Lists in Data Structures

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.

Télécharger la présentation

Fundamentals of Stacks, Queues, and Linked Lists in Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. 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

  3. An array implementation of a stack S PUSH(S, 17) PUSH(S, 3) POP(S) Hsiu-Hui Lee

  4. empty, underflows, overflows STACK_EMPTY(S ) 1 if top[S] = 0 2 then return TRUE • else return FALSE O(1) Hsiu-Hui Lee

  5. 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

  6. An array implementation of a queue Q ENQUEUE(Q, 17) ENQUEUE(Q, 3) ENQUEUE(Q, 5) DEQUEUE(Q) Hsiu-Hui Lee

  7. 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

  8. 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

  9. 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

  10. LIST-SEARCH(L, k) 1 xhead[L] 2 while x ≠ NIL and key[x] ≠ k 3 do xnext[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

  11. 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

  12. 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

  13. 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

  14. 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

  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 LIST-SEARCH(L, k) 1 xhead[L] 2 while x ≠ NIL and key[x] ≠ k 3 do xnext[x] • return x Hsiu-Hui Lee

  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] 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

  17. 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

  18. Multiple-array representation of objects Hsiu-Hui Lee

  19. A single array representation of objects Hsiu-Hui Lee

  20. Allocating and freeing objects--garbage collector Hsiu-Hui Lee

  21. Allocate_object( ), LIST_INSERT(L,4), Key(4)=25 Hsiu-Hui Lee

  22. LIST_DELETE(L,5), FREE_OBJECT(5) Hsiu-Hui Lee

  23. 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

  24. Two link lists Hsiu-Hui Lee

  25. 10.4 Representing rooted trees Hsiu-Hui Lee

  26. Binary trees • p[x], left[x], right[x] Hsiu-Hui Lee

  27. Rooted tree with unbounded branching Hsiu-Hui Lee

  28. Rooted tree with unbounded branching • p[x], left-child[x], right-sibling[x] Hsiu-Hui Lee

  29. Other tree representation Hsiu-Hui Lee

More Related