1 / 19

CSE 326 Linear ADTs: Lists, Stacks and Queues

CSE 326 Linear ADTs: Lists, Stacks and Queues. David Kaplan Dept of Computer Science & Engineering Autumn 2001. 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

duckettj
Download Presentation

CSE 326 Linear ADTs: Lists, Stacks and Queues

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. CSE 326Linear ADTs:Lists, Stacks and Queues David Kaplan Dept of Computer Science & Engineering Autumn 2001

  2. 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 CSE 326 Autumn 2001 2

  3. ClassifyingDynamic Sets “Linear” “Hierarchical” “Graphical” CSE 326 Autumn 2001 3

  4. ClassifyingLinear Dynamic Sets “List” “Stack” “Queue” CSE 326 Autumn 2001 4

  5. key data 0 1 2 3 … n object object_Q object_X object_A 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 CSE 326 Autumn 2001 5

  6. 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 CSE 326 Autumn 2001 6

  7. List ADT Set of n objects {A1, A2, … , An} • Ai precedes Ai+1 for 1  i < n • Ai succeeds Ai-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); } CSE 326 Autumn 2001 7

  8. 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); } CSE 326 Autumn 2001 8

  9. 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 next next next object object 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 CSE 326 Autumn 2001 9

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

  11. 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 next next next prev prev prev prev object object object header CSE 326 Autumn 2001 11

  12. 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 CSE 326 Autumn 2001 12

  13. 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 0 1 7 9 2 3 4 5 6 8 10 object F O A R N R T List A First = 2 next 1 7 9 0 3 8 6 4 0 10 5 CSE 326 Autumn 2001 13

  14. 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 CSE 326 Autumn 2001 14

  15. List Application:Polynomial ADT Polynomial ADT is a specialization of List ADT 5 1 3 1 4 27 0 3 1 3 5x27 + 4x3 + 1 3x3 + x • List-based Polynomial is sparse (unlike array-based) • Keep list sorted by exponent • Insert is O( )? • Add is O( )? • Multiply is O( )? CSE 326 Autumn 2001 15

  16. Motivation forStretchy 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 CSE 326 Autumn 2001 16

  17. 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( ) CSE 326 Autumn 2001 17

  18. 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 CSE 326 Autumn 2001 18

  19. 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 CSE 326 Autumn 2001 19

More Related