Data Structures from Cormen, Leiserson, Rivest & Stein
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)
Dynamic sets operations (contd) –MINIMUM(S) –MAXIMUM(S) –SUCCESSOR(S,x) –PREDECESSOR(S,x) Most operations return pointers to some location
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
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Stack operations When top[S]=0 the stack is empty Stack “errors”: Underflow – when an empty stack is popped Overflow - If top[S]>n
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.