Download presentation
Presentation is loading. Please wait.
1
Data Structures from Cormen, Leiserson, Rivest & Stein
2
Sets Fundamental to CS Dynamis Sets: manipulated by algorithms, and can grow, shrink, change over time. Operations on sets: –SEARCH(S,k), returns a pointer to k location or NIL –INSERT(S,k) –DELETE(S,k)
3
Dynamic sets operations (contd) –MINIMUM(S) –MAXIMUM(S) –SUCCESSOR(S,x) –PREDECESSOR(S,x) Most operations return pointers to some location
4
Stacks and Queues Dynamic sets in which the element removed from the set in the DELETE operation is pre-specified. LIFO – last in first out – in stacks FIFO – first in first out – in queue Stacks - folklore names: PUSH – INSERT POP - DELETE
5
Stacks We can implement a stack of almost n elements with an array S[1..n]. top[S] – indexes the most recently inserted element. The stack consists of S[1],…S[top[S]] elements, S[1] is the element at the bottom of the stack, and S[top[S]] is the element at the top of the stack.
6
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
7
Stack operations When top[S]=0 the stack is empty Stack “errors”: Underflow – when an empty stack is popped Overflow - If top[S]>n
8
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
11
Queues ENQUEUE – INSERT to a queue DEQUEUE – Delete from a queue Queues have a head and a tail Inserting at the tail Deleting at the head head[Q] – points to the head tail[Q]-the location to insert a new element
12
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.