Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001.

Similar presentations


Presentation on theme: "CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001."— Presentation transcript:

1 CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001

2 Linear ADTsCSE 326 Autumn 20012 Dynamic Sets  Dynamic set  Models a mathematical set, for use by an algorithm  Examples: list, stack, queue, tree, graph, …  Essentially, every ADT is a dynamic set  Linear dynamic set  Models a simple collection of objects  Examples: list, stack, queue, array  Hierarchical dynamic set  Models a collection of objects organized by hierarchy  Examples: trees, in all their flavors  Graphical dynamic set  Models a collection of objects with complex toplogy  Examples: graphs, in all their flavors

3 Linear ADTsCSE 326 Autumn 20013 Classifying Dynamic Sets Concepts Simple collectionCollection with hierarchy Collection with complex topology Plans (ADTs) List, Stack, Queue Binary Search Tree, B-Tree, AVL Tree, Heaps, … Directed Graph, DAG, Flow Network, … MechanismsNumerous “Linear”“Hierarchical”“Graphical”

4 Linear ADTsCSE 326 Autumn 20014 Classifying Linear Dynamic Sets Concept LDS with … User-determined ordering LDS with … LIFO ordering LDS with … FIFO ordering PlanList ADTStack ADTQueue ADT Mechanisms Linked List (array not recommended) Stretchy array Fixed array “List”“Stack”“Queue”

5 Linear ADTsCSE 326 Autumn 20015 ADT Analysis: General Framework We distinguish between ADT and outside agent  ADT is what we are studying  Outside agent uses ADT to store and retrieve “objects” object is composed of key and data  key is visible to ADT and to outside agent  data is visible only to outside agent Index or position  ADT internal information  Typically visible only to ADT key data object object_X 0213…n object_A object_Q

6 Linear ADTsCSE 326 Autumn 20016 ADT Analysis: Simplified Framework In most of 326, we use a simplified analysis framework  In our simple framework, key is an int, data is empty  Index or position is typically an int  Reduces code complexity, while illuminating the key points  Must distinguish clearly between key and index when we analyze ADTs

7 Linear ADTsCSE 326 Autumn 20017 List ADT Set of n objects {A 1, A 2, …, A n }  A i precedes A i+1 for 1  i < n  A i succeeds A i-1 for 1 < i  n Empty list == {} == List of size 0 class List { public: void Insert(object o, int position); object Remove(key k); object Find(key k); object Find_Kth(int position); object Next(key k); }

8 Linear ADTsCSE 326 Autumn 20018 Array Implementation of List Suppose we implement a List using an array …  Which operations are fast?  Which are slow?  Why? class List { public: void Insert(object o, int position); object Remove(key k); object Find(key k); object Find_Kth(int position); object Next(key k); }

9 Linear ADTsCSE 326 Autumn 20019 Linked List Linked list is a collection of nodes  Each node contains a pointer to the next node in the list and the object contained by the node (or a pointer to the object) next object next object next object header Header (or sentinel) node is a common programming convenience  Enables insertion and deletion at front of list  Contains no data  Can act as surrogate for List object itself

10 Linear ADTsCSE 326 Autumn 200110 next prev Doubly-Linked List Nodes in a doubly-linked list contain both forward and back pointers (next, prev) next object prev next object prev next object prev header

11 Linear ADTsCSE 326 Autumn 200111 next prev Circular Linked List Circular linked list (singly- or doubly-linked) connects first and last nodes  In effect, list becomes a continuous cycle, although header node may still be used to identify front of list next object prev next object prev next object prev header

12 Linear ADTsCSE 326 Autumn 200112 Linked List Implementation: Node Allocation Method Node allocation method allocates and destroys individual list nodes as needed  Separate Node object  Create, destroy Node objects with new, delete  Many allocate/deallocate calls can be expensive

13 Linear ADTsCSE 326 Autumn 200113 Linked List Implementation: Cursor Array Method Cursor array method  Typically used by List-class-specific memory manager  Avoid allocate/deallocate cost by over-riding new and delete  Store multiple Lists per cursor array  List is a path through array of nodes, following next “pointer”  Free list holds unused cells  Useful even in non-pointer languages  Good use for Stretchy Array FOARNRT 38640105 object next 179 23456810 First = 2 List A 0 1907

14 Linear ADTsCSE 326 Autumn 200114 Implementing Linked List in C++ Weiss uses separate List, Node, List Iterator classes  Clean, intuitive syntax  Somewhat verbose Many other implementations exist  Node object with exposed next pointer is sufficient to iterate  If no List object, caller needs to hold onto distinct header node

15 Linear ADTsCSE 326 Autumn 200115 List Application: Polynomial ADT Polynomial ADT is a specialization of List ADT 5 27 4 3 1 0 5x 27 + 4x 3 + 1 3 3 1 1 3x 3 + x  List-based Polynomial is sparse (unlike array-based)  Keep list sorted by exponent  Insert is O( )?  Add is O( )?  Multiply is O( )?

16 Linear ADTsCSE 326 Autumn 200116 Motivation for Stretchy Array  Stacks and queues may be implemented either with linked lists or with arrays – why?  When implemented with a fixed-size array  Insert method (push, enqueue) needs to check full  Outside agent needs to handle full-up error  Alternative is to implement stack or queue with a stretchy array, which grows based upon demand  Note: stretchy array typically does not shrink

17 Linear ADTsCSE 326 Autumn 200117 Stretchy Array Append(object o) if (array is full) allocate new_array(2 * array.size) copy array into new_array deallocate array array  new_array array[top]  o Questions: Best case Append = O( ) Worst case Append = O( )

18 Linear ADTsCSE 326 Autumn 200118 Review: Amortized Analysis  Consider any sequence of operations applied to a data structure  your worst enemy could choose the sequence!  Some operations may be fast, others slow  Goal: design the data structure so that the average time per operation is still good

19 Linear ADTsCSE 326 Autumn 200119 Amortized Analysis of Stretchy Array What is the max number of stretches? log n Consider sequence of n Append operations  What is the total time? Regular Append takes time a, stretching array of k elements takes time kb, for some constants a and b  Amortized time = (an+b(2n-1))/n = O(1) Note: this amortization scenario is pessimistic: n consecutive Appends with no deletes


Download ppt "CSE 326 Linear ADTs: Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001."

Similar presentations


Ads by Google