Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue.

Similar presentations


Presentation on theme: "1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue."— Presentation transcript:

1 1 Joe Meehean

2 2

3  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue  peek return (do not remove) item at front of queue 3

4  Similar to List ADT with restricted operations implemented using arrays or linked nodes 4

5  Fields Node *phead_; int size_; 0 0 size_: phead_: 1 1 //Empty //After q.enqueue(A) A A 5 size_: phead_:

6 2 2 //After q.enqueue(B) A A B B 2 2 //After q.enqueue(B) B B A A OR  Enqueue to end or insert at the front? 6 size_: phead_: size_: phead_:

7 EnQ at Front Single TailPtr Double TailPtr enqueue???? dequeue???? peek???? EnQ at Back Single TailPtr Double TailPtr enqueue???? dequeue???? peek???? 7

8 EnQ at Front Single TailPtr Double TailPtr enqueueO(1) dequeueO(N) O(1) peekO(N)O(1)O(N)O(1) EnQ at Back SingleDouble TailPtr enqueueO(N) O(1) dequeueO(1) peekO(1) 8

9  Fields T *items_; int size_; int capacity_; 0 0 1 1 X X //Empty //After q.enqueue(X) 9 4 4 capacity_: size_: items_: size_: items_:

10 2 2 X X Y Y //After q.enqueue(Y) 2 2 Y Y X X //After q.enqueue(Y) OR  Enqueue to end or insert at the front? 10 4 4 capacity_: size_: items_: 4 4 capacity_: size_: items_:

11 Method enQ at front dQ at end enQ at end dQ at front enqueue ?? dequeue ?? peek ?? 11

12 Method enQ at front dQ at end enQ at end dQ at front enqueue avg: O(1) dequeue O(1) peek O(1) 12

13 13

14  Array implementation of queue 14 Method enQ at front dQ at end enQ at end dQ at front enqueue avg: O(1) dequeue O(1) peek O(1)

15  O(1) for enqueue and dequeue  Don’t force 1 st item to index 0  New fields int first_ int last_ 3 3 X X Y Y Z Z A A 01234 q.enqueue(X) “”(Y) “”(Z) q.dequeue() q.enqueue(A) 15 size_: items_: first_ last_

16 void enqueue(const T& newItem){ if(size_ == capacity_) {...} last_++; items_[last_] = newItem; size++; } T dequeue(){ size--; first_++; return items_[first_ - 1]; }  O(1) if array not full (avg case) 16

17  What if we call q.enqueue(C)? could expand array? wastes space 17 3 3 X X Y Y Z Z A A B B 01234 size_: items_: first_ last_

18 01 2 3 4 Z A B 18 3 3 X X Y Y Z Z A A B B 01234 size_: items_: first_ last_ XY first_ last_

19 01 2 3 4 Z A B 19 3 3 C C Y Y Z Z A A B B 01234 size_: items_: first_ last_ CY first_ last_ q.enqueue(C) //enqueued at //pos 0

20 void enqueue(const T& newItem){ if( size_ == capacity_ ){ doubleCapacity(); } // increment the last index last_++; // wrapping if necessary if(last_ == capacity_){ last_ = 0; } items_[last] = newItem; size_++; } 20

21  Expanding the array C C D D A A B B 4 4 8 8 01234 567 items_: C C D D A A B B 4 4 size_: 4 4 capacity_: 0123 21 items_: size_: capacity_: first_ last_ first_ last_

22  Expanding the array C C D D A A B B 4 4 8 8 01234 567 items_: C C D D A A B B 4 4 size_: 4 4 capacity_: 0123 Big hole in the middle 22 items_: size_: capacity_: first_ last_ first_ last_

23 A A B B C C 0 1 23 4 D D 5 67 C C D D A A B B 0123 23 first_ last_ first_ last_

24  Allocate new array of twice the size: tmp  Copy items_[first] to items_[capacity_- 1] into tmp starting at position first  Copy items_[0] to items_[first - 1] into tmp starting at position capacity_  Fix member data set items_ to point at tmp delete old items last_ = first_ + capacity_ – 1 capacity_ *= 2 24

25  Space linked list stores pointer and data array stores only data array can be mostly empty toss up  Time linked all O(1) circular array all O(1) in avg case  Ease of implementation linked by a mile 25

26 26


Download ppt "1 Joe Meehean. 2  empty is the queue empty  size  enqueue (add) add item to end of queue  dequeue (remove) remove and return item at front of queue."

Similar presentations


Ads by Google