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

cs261 data structures
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CS261 Data Structures

Dynamic Array Queue and Deque

slide-2
SLIDE 2

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.

Queues

slide-3
SLIDE 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
slide-4
SLIDE 4

Queue with Dynamic Array

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.

Removal from front is expensive!

slide-5
SLIDE 5

void addFront(TYPE val); void removeFront(); TYPE front(); void addBack(TYPE val); void removeBack () TYPE back();

Deque (Double Ended Queue) ADT

5 22 8 front back addBack removeBack addFront removeFront

Can simulate a stack or queue using as deque!

slide-6
SLIDE 6

Deque Application

  • Finite Length Undo
slide-7
SLIDE 7

Dynamic Array Deque

Adding to Front Removing from Front

front back

Big-Oh? Big-Oh?

slide-8
SLIDE 8
  • 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

Let the partially filled block “float”

slide-9
SLIDE 9

– First filled element no longer always at index 0 – Filled elements may wrap around back to the front end of array

beg size beg size

Dynamic Array Deque with Circular Buffer

slide-10
SLIDE 10

Problem: Elements can wrap around from beg to end

beg size

Wrapping Around

How can we compute the index of the last/back element? Index of front element of deque is just beg.

slide-11
SLIDE 11
  • Calculate offset:

index = beg + size – 1

  • If larger than or eq to capacity, subtract capacity

if (index >= cap) index = index - cap;

  • Or..combine into single statement with mod:

/* Convert logical index to absolute index. */ index = (beg + size – 1) % cap;

beg

beg = 6 cap = 8 size = 5

size

0 1 2 3 4 5 6 7

beg size

0 1 2 3 4 5 6 7 beg = 0 cap = 8

Index of Back

slide-12
SLIDE 12

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; }

ArrayDeque Structure

slide-13
SLIDE 13

Adding/removing to/from back is easy, just adjust size

– Still need to reorganize if adding and size = capacity

beg

size

beg size beg

size

Add Remove

beg

size

Adding/Removing from Back

slide-14
SLIDE 14

Changes to front are easy, just adjust size and starting location

beg size beg size beg size

Add Remove

beg size

Adding/Removing from Front

What about when beg = 0?

slide-15
SLIDE 15

Operations Analysis

Operation Best Worst Ave AddBack 1 n 1+ RemoveBack 1 1 1 AddFront 1 n 1+ RemoveFront 1 1 1

slide-16
SLIDE 16

Your Turn…

  • Complete Worksheet 20
slide-17
SLIDE 17

// 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]; }

Accessing End of Deque

struct ArrDeque { TYPE *data; int size; int beg; int cap; };

slide-18
SLIDE 18

void removeBack (struct deque *d) { assert(d->size > 0); d->size--; }

Accessing End of Deque

struct ArrDeque { TYPE *data; int size; int beg; int cap; };

slide-19
SLIDE 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; d->size++; }

struct ArrDeque { TYPE *data; int size; int beg; int cap; };

slide-20
SLIDE 20

Resize?

  • Can we simply copy the elements to a larger

array?

  • Do this on your own in WS #20
slide-21
SLIDE 21

Inserting & Removing from Front

Changes to front are easy, just adjust count and starting location

beg size beg size beg size

Add Remove

beg size

slide-22
SLIDE 22

Worksheet

  • dequeFront
  • dequeRemoveFront
  • dequeAddFront

Do these on your own.