Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Array: Implementation of Queue & Deque

Similar presentations


Presentation on theme: "Dynamic Array: Implementation of Queue & Deque"— Presentation transcript:

1 Dynamic Array: Implementation of Queue & Deque
CS 261 – Data Structures Dynamic Array: Implementation of Queue & Deque

2 Dynamic Array: Adding & Removing at Front
Adding or removing in a dynamic array require “sliding” all elements with a higher index: Adding at front of array  slide elements up Removing from front of array  slide elements down

3 Deque ADT: Dynamic Array Solution
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 (cnt) Still need to reallocate when size equal to capacity

4 Deque: Dynamic Array Implementation
Create a dynamic array version of the ring buffer queue: First filled element no longer always at index 0 Filled elements may wrap around back to the front end of array Called ArrDeque first (beg) first (beg) count (cnt) count (cnt)

5 Dynamic Array Deque: Structure
struct ArrDeque { TYPE *data; /* Pointer to data array. */ int cnt; /* 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 = (TYPE *)malloc(cap * TYPE)); assert(d->data != 0); d->cnt = d->beg = 0; d->cap = cap; }

6 Inserting & Removing from Back
Adding to back is easy, just adjust count of the number of elements Still need to reorganize if adding and size = capacity Add Remove first (beg) first (beg) count (cnt) count (cnt) first (beg) first (beg) count (cnt) count (cnt)

7 Inserting & Removing from Front
Changes to front are easy, just adjust count and starting location Add Remove first (beg) first (beg) count (cnt) count (cnt) first (beg) first (beg) count (cnt) count (cnt)

8 Wrapping Around Problem: Elements can wrap around from start to end
first (beg) count (cnt)

9 Handling Wrap-Around: Computing Index
Calculate offset: add logical (element) index to start (beg) offset = beg + logicalIndex; /* logIndex = 3, offset = 9 */ If larger than capacity, subtract capacity if (offset > cap) absoluteIndex = offset – capacity; If smaller than zero, add cap if (offset < 0) absoluteIndex = offset + capacity; That way sizes are always in range Or..combine into single statement with mod: /* Convert logical index to absolute index. */ absIdx = (logicalIdx + beg) % cap; beg = 6 cap = 8 beg cnt beg = 0 cap = 8 beg cnt

10 Your Turn: Worksheet Time
Write the implementation of the dynamic array deque

11 Comparison for stack, queue, deque implementations
front() back() addFront() addBack() (push, enqueue) remove Front() (dequeue) Back() (pop) get(i) dynArrStack n/a O(1) O(1) ave. O(n) worst listStack O(1) always listQueue listDeque arrayDeque O(1) ave

12 Midterm Exam – Friday To Study Topics Exam Master all worksheets
Read lessons Practice exam Lecture Notes Topics Big O ADTs (stack, queue, deque and applications) Implementations: dynArr, listStack, listQueue, listDeque, arrayDeque Exam Closed book, closed notes, no calculator short answer, reading code, writing code


Download ppt "Dynamic Array: Implementation of Queue & Deque"

Similar presentations


Ads by Google