cs261 data structures
play

CS261 Data Structures Dynamic Array Queue and Deque Queues int - PowerPoint PPT Presentation

CS261 Data Structures Dynamic Array Queue and Deque Queues int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front. Queue Applications


  1. CS261 Data Structures Dynamic Array Queue and Deque

  2. Queues int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front.

  3. Queue Applications • Also good for ‘remembering’, just in a different order. We’ll revisit this when we study graphs and search! • Operating systems – process scheduling • Print Queue

  4. Queue with Dynamic Array Removal from front is expensive! front back int isEmpty(); void addBack(TYPE val); // Add value at end of queue. TYPE front(); // Get value at front of queue. void removeFront(); // Remove value at front.

  5. Deque (Double Ended Queue) ADT void addFront(TYPE val); void removeFront(); TYPE front(); void addBack(TYPE val); void removeBack () TYPE back(); removeBack addFront front back 5 22 8 removeFront addBack Can simulate a stack or queue using as deque!

  6. Deque Application • Finite Length Undo

  7. Dynamic Array Deque front back Big-Oh? Adding to Front Big-Oh? Removing from Front

  8. Let the partially filled block “float” • One solution: Rather than always use index zero as our starting point, allow the starting index to “ float ” • Maintain two integer values: – Starting or beginning index ( beg ) – Count of elements in the collection ( size ) • Still need to reallocate when size equal to capacity

  9. Dynamic Array Deque with Circular Buffer – First filled element no longer always at index 0 – Filled elements may wrap around back to the front end of array beg beg size size

  10. Wrapping Around Problem: Elements can wrap around from beg to end beg size Index of front element of deque is just beg . How can we compute the index of the last/back element?

  11. Index of Back • Calculate offset: beg = 6 index = beg + size – 1 cap = 8 size = 5 • If larger than or eq to capacity, subtract capacity beg size if (index >= cap) index = index - cap; 0 1 2 3 4 5 6 7 beg = 0 beg cap = 8 size 0 1 2 3 4 5 6 7 • Or..combine into single statement with mod: /* Convert logical index to absolute index. */ index = (beg + size – 1) % cap;

  12. ArrayDeque Structure struct ArrDeque { TYPE *data; /* Pointer to data array. */ int size; /* Number of elements in collection. */ int beg; /* Index of first element. */ int cap; /* Capacity of array. */ }; void initArrDeque(struct ArrDeque *d, int cap) { d->data = malloc(cap * sizeof(TYPE))); assert(d->data != 0); d->size = d->beg = 0; d->cap = cap; }

  13. Adding/Removing from Back Adding/removing to/from back is easy, just adjust size – Still need to reorganize if adding and size = capacity Add Remove beg beg size size beg beg s ize size

  14. Adding/Removing from Front Changes to front are easy, just adjust size and starting location Add Remove beg beg size size beg beg size size What about when beg = 0?

  15. Operations Analysis Operation Best Worst Ave AddBack 1 n 1+ RemoveBack 1 1 1 AddFront 1 n 1+ RemoveFront 1 1 1

  16. Your Turn… • Complete Worksheet 20

  17. Accessing End of Deque // return back element of deque TYPE back (struct deque * d) { int index = d->start + d->size - 1; if (index >= d->capacity) index -= d->capacity return d->data[index]; struct ArrDeque { } TYPE *data; int size; int beg; int cap; };

  18. Accessing End of Deque void removeBack (struct deque *d) { assert(d->size > 0); d->size--; } struct ArrDeque { TYPE *data; int size; int beg; int cap; };

  19. Adding to end void addBack (struct deque * d, TYPE newValue) { int index; if (d->size >= d->capacity) _dequeSetCapacity(d, 2*d->capacity); // next slide index = d->start + d->size; if (index >= d->capacity) index -= d->capacity; d->data[index] = newValue; struct ArrDeque { d->size++; TYPE *data; } int size; int beg; int cap; };

  20. Resize? • Can we simply copy the elements to a larger array? • Do this on your own in WS #20

  21. Inserting & Removing from Front Changes to front are easy, just adjust count and starting location Remove Add beg beg size size beg beg size size

  22. Worksheet • dequeFront • dequeRemoveFront • dequeAddFront Do these on your own.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend