dynamic array queue - oregon state...

33
CS 261: Data Structures Dynamic Array Queue

Upload: others

Post on 14-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

CS 261: Data Structures

Dynamic Array Queue

Page 2: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Dynamic Array -- Review

• Positives:

– Each element easily accessed

– Grows as needed

– The user unaware of memory management

2

Page 3: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Stack as Dynamic Array -- Review

• Remove and add elements from/to top

• Occasional capacity increase

• Remove operation has complexity O(1)

• Add operation has complexity O(1)

3

Page 4: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Bag as Dynamic Array -- Review

• Order is not important, so adding to the end

• Add is O(1), with occasional capacity increase

• Remove is O(n)

4

Page 5: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Dynamic Array -- Problems

• Data kept in a single large block of

memory

• Often more memory used than necessary

– especially when more frequently removing

elements than adding elements

• Inefficient for implementation of other ADT5

Page 6: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Queue

6

Page 7: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Queue

• Elements are inserted at one end, and

removed from another

• E.g. line of people

• First in, first out (FIFO)

7

Page 8: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Interface View of Queue

• addBack(newElement) -- inserts an element

• front() -- returns the first element

• removeFront() -- removes the first element

• isEmpty() -- checks if the queue is empty

8

Page 9: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Queue as Dynamic Array

• Which end is better for insertion?

• Which end is better for removal?

• What would be O(?) ?

9

Page 10: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Removing from Front, Adding to Backusing Dynamic Arrays

2 4 7 3 9 1

4 7 3 9 1

Remove requires moving elements => O(n)

Remove 2:

10

Page 11: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Removing from Front, Adding to Backusing Dynamic Arrays

2 4 7 3 9 1

4 7 3 9 1

Remove requires moving elements => O(n)

Remove 2:

Insertion to the end is O(1)11

Inefficient

Page 12: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Removing from Back, Adding to Frontusing Dynamic Arrays

Add requires moving elements => O(n)

Add 6:

12

Page 13: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Removing from Back, Adding to Frontusing Dynamic Arrays

Add requires moving elements => O(n)

Add 6:

Removal from the end is O(1)13

Inefficient

Page 14: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Deque

14

back indexfront index

remove addadd remove

allocated capacity

size

Page 15: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Deque

• Allows:

– Insertions at both front and back

– Removals at both front and back

• Stack, Queue à Special case of Deque

• Deque à Two end-to-end stacks

15

Page 16: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Interface View of Deque• addFront(newElem) -- inserts to the front

• addBack(newElem) -- inserts to the back

• front() -- returns the first front element

• back() -- returns the first back element

• removeFront() -- removes from the front

• removeBack() -- removes from the back

• isEmpty() -- checks if the queue is empty 16

Page 17: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Deque as Dynamic Array

• Key idea: – Do not tie "front" to index zero

• Instead,

– allow both "front" and "back" to float around the array

17

Page 18: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Example Deque

In this example, start index is larger than back index

backIndex startIndex

DataSize = 6DataStart = 7Data =

2 4 7 39 1

18

Page 19: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Adding/Removing for Deque

• Add front: decrease the start index by 1

• Add back: increase size by 1

19

backIndex startIndex

DataSize = 6DataStart = 7Data =

2 4 7 39 1

startIndex--backIndex++

Page 20: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Adding/Removing for Deque• Remove front: increase the start index by 1

• Remove back: decrease size by 1

20

backIndex start

DataSize = 6DataStart = 7Data =

2 4 7 39 1

startIndex++backIndex--

Page 21: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Adding/Removing for Deque

What if elements wrap around?

DataSize = 6DataStart = 7Data =

2 4 7 39 1

21

Page 22: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Wrapping: How to Compute New Index

• If Index < 0, then add capacity

• If Index > capacity, then subtract capacity

• If size == capacity, reallocate new buffer

DataSize = 6DataStart = 7Data =

2 4 7 39 122

Page 23: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Implementation

23

Page 24: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Deque Structure

struct deque { TYPE * data; int capacity; int size; int start;

};

24

Page 25: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Keeping size vs Keeping pointer to end

• We compute the back index from the start

index and size

• Why not keep the back index?

• OK, but need to compute size frequently

25

Page 26: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Wrapping: How to Compute Back Index

Use the mod operator:

backIndex = (start + size) % capacity;

26

backIndex startIndex

DataSize = 6DataStart = 7Data =

2 4 7 39 1

Page 27: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

initDequevoid initDeque (struct deque *d, int initCapacity) {

d->size = d->start = 0;/*initially, no data in Deque*/

assert(initCapacity > 0);

d->capacity = initCapacity;

d->data =

(TYPE *) malloc(initCapacity * sizeof(TYPE));

assert(d->data != 0);

} 27

Page 28: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

addBackDequevoid addBackDeque(struct deque *d, TYPE val) {

int back_idx;

if (d->size == d->capacity) _doubleCapDeque(d);

/* Increment the back index */

back_idx = (d->start + d->size) % d->capacity;

d->data[back_idx] = val;

d->size ++;

}

DataSize = 6DataStart = 7Data =

2 4 7 39 1

back_idx start 29

Page 29: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

addBackDequevoid addBackDeque(struct deque *d, TYPE val) {

int back_idx;

if (d->size == d->capacity) _doubleCapDeque(d);

/* Increment the back index */

back_idx = (d->start + d->size) % d->capacity;

d->data[back_idx] = val;

d->size ++;

} 30

Complexity?

Page 30: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

addFrontDequevoid addFrontDeque(struct deque *d, TYPE val) {

if (d->size == d->capacity) _doubleCapDeque(d);

/* Decrement the front index */

d->start--;

if (d->start < 0) d->start += d->capacity;

d->data[d->start] = val;

d->size ++;

}

DataSize = 6DataStart = 7Data =

2 4 7 39 1

start 31

Page 31: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

addFrontDequevoid addFrontDeque(struct deque *d, TYPE val) {

if (d->size == d->capacity) _doubleCapDeque(d);

/* Decrement the front index */

d->start--;

if (d->start < 0) d->start += d->capacity;

d->data[d->start] = val;

d->size ++;

}32

Complexity?

Page 32: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Worksheet 20

• Implement Dynamic Array Deque

• How do you

– Add to front or back?

– Return front? Return back?

– Remove front? Remove back?

33

Page 33: Dynamic Array Queue - Oregon State Universityweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/Deque.pdfQueue •Elements are inserted at one end, and removed from another

Queue as Deque• Special case of Deque• Add is O(1)• Remove is O(1)

34