Presentation is loading. Please wait.

Presentation is loading. Please wait.

Queues.

Similar presentations


Presentation on theme: "Queues."— Presentation transcript:

1 Queues

2 What is a queue? First-in first-out data structure (FIFO)
New objects are placed at rear Removal restricted to front Examples?

3 Queue ADT Operations enqueue(o): Insert o at rear of queue
Input: Object; Output: None dequeue(): Remove object at front; error if empty Input: None; Output: Object removed size(): Return number of objects in queue Input: None; Output: Integer isEmpty(): Return a boolean indicating queue empty Input: None; Output: Boolean first(): Return object at front without removing; error if empty Input: None; Output: Object

4 Example enqueue(5) enqueue(3) dequeue() enqueue(7) front() isEmpty()
size() enqueue(3) enqueue(5) dequeue()

5 Queue Interface int size() const; bool isEmpty() const;
Object& front() throw (QueueEmptyException); void enqueue(const Object& obj); Object dequeue() throw(QueueEmptyException);

6 Underlying Representation
Array versus Linked List Pros and cons? Running time? size isEmpty enqueue dequeue front

7 Array Implementation 1 2 3 4 5 6 … n-1 enqueue(5) enqueue(3) 5 3 1 2 3
1 2 3 4 5 6 n-1 enqueue(5) enqueue(3) 5 3 1 2 3 4 5 6 n-1 dequeue() ?

8 Array Implementation 1 2 3 4 5 6 … n-1 enqueue(5) enqueue(3) 5 3 1 2 3
1 2 3 4 5 6 n-1 enqueue(5) enqueue(3) 5 3 1 2 3 4 5 6 n-1 3 dequeue() ? 1 2 3 4 5 6 n-1

9 Circular Array f r 1 2 3 4 5 6 n-1 f – stores index of cell which stores first element of queue r – stores index of next available cell in queue Initially, f = r = 0 How do you add a new element? How do you remove an element?

10 Circular Array How do you add a new element?
f r 1 2 3 4 5 6 n-1 How do you add a new element? insert at array[r] increment r How do you remove an element? return array[f] increment f What happens when r >= n-1?

11 Circular Array Need to be able to wrap around Modulo – %
f 1 2 3 4 5 6 n-1 Need to be able to wrap around Modulo – % increment f using (f+1)%n increment r using (r+1)%n

12 Circular Array f r f dequeue 1 2 1 2 f r r f r =(2+1)%3= 0 enqueue
1 2 1 2 f r r f r =(2+1)%3= 0 enqueue enqueue 1 2 1 2 f r enqueue 1 2

13 Algorithms size isEmpty front dequeue enqueue return (N-f+r) mod N
return (f == r) front if isEmpty then throw QueueEmptyException return array[f] dequeue temp = array[f] f = (f+1)%N return temp enqueue if size == N-1 then throw QueueFullException SIZE MUST BE < N-1 array[r] = object r = (r+1)%N

14 Linked List Implementation
tail head Ø enqueue dequeue

15 Linked List Implementation
new_node head tail Object enqueue if(size == 0) head = new_node else tail->next_ptr = new_node tail = new_node increment size next_ptr

16 Linked List Implementation
new_node head tail Object1 Object2 enqueue if(size == 0) head = new_node else tail->next_ptr = new_node tail = new_node increment size next_ptr next_ptr

17 Linked List Implementation
dequeue if (isEmpty) throw QueueEmptyException head = head->next_ptr decrement size if new size is 0 – set tail to NULL

18 Exercises Implement a queue using two stacks
Implement a two-color stack


Download ppt "Queues."

Similar presentations


Ads by Google