Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS210- Lecture 5 Jun 9, 2005 Agenda Queues

Similar presentations


Presentation on theme: "CS210- Lecture 5 Jun 9, 2005 Agenda Queues"— Presentation transcript:

1 CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Array Based Implementation of Queues Applications of Queues Linked Lists 2/24/2019 CS210-Summer 2005, Lecture 5

2 Queues Queue is a container of objects that are inserted and removed according to the first-in first-out (FIFO) principle. Objects can be inserted into a queue at any time, but only the objects that has been in the queue the longest can be removed. Objects enter the queue at the rear and are removed from the front. 2/24/2019 CS210-Summer 2005, Lecture 5

3 Queue ADT Queue ADT supports following methods:
enqueue(o): Insert object o at the rear of the queue. Input: Object o Output: None dequeue(): Remove and return from the queue the object at the front. Error occurs if the queue is empty. Input: None Output: Front object 2/24/2019 CS210-Summer 2005, Lecture 5

4 Queue ADT size(): Return the number of objects in the queue
Input: none Output: Integer (total number of objects) isEmpty(): Return a boolean indicating if the queue is empty. Input: None Output: boolean (true if queue is empty, false otherwise) 2/24/2019 CS210-Summer 2005, Lecture 5

5 Queue ADT front(): Return but do not remove, the front object in the queue. Error occurs if the queue is empty. Input: None Output: Front object Exceptions Attempting the execution of dequeue or front on an empty queue throws an EmptyQueueException. 2/24/2019 CS210-Summer 2005, Lecture 5

6 Example Operation Output Q enqueue(5) – (5) enqueue(3) – (5, 3)
dequeue() 5 (3) enqueue(7) – (3, 7) dequeue() 3 (7) front() 7 (7) dequeue() 7 () dequeue() “error” () isEmpty() true () enqueue(9) – (9) enqueue(7) – (9, 7) size() 2 (9, 7) enqueue(3) – (9, 7, 3) enqueue(5) – (9, 7, 3, 5) dequeue() 9 (7, 3, 5) 2/24/2019 CS210-Summer 2005, Lecture 5

7 Applications of Queues
Waiting lists: Stores, Theaters etc. Access to Shared resources (printer). Multiprogramming 2/24/2019 CS210-Summer 2005, Lecture 5

8 Queue Interface Java interface corresponding to our Queue ADT
public interface Queue { public int size(); public boolean isEmpty(); public Object front() throws EmptyQueueException; public void enqueue(Object o); public Object dequeue() throws EmptyQueueException; } Java interface corresponding to our Queue ADT Requires the definition of class EmptyQueueException No corresponding built-in Java class 2/24/2019 CS210-Summer 2005, Lecture 5

9 A Simple Array based implementation
We must decide how we are going to keep track of the rare and front of the queue. One possibility is to adapt the approach we used in stack. Q[0] be the front. Not efficient as need to move all elements forward one array cell each time we perform a dequeue operation. Dequeue operation will take O(n) time. 2/24/2019 CS210-Summer 2005, Lecture 5

10 A Simple Array based implementation
Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty. Initially f = r = 0 (initially empty) When we remove an element from front of the queue, we increment f to index the next cell. 2/24/2019 CS210-Summer 2005, Lecture 5

11 A Simple Array based implementation
When we add an element, we store it in cell Q[r] and increment r to index the next available cell in Q. This allows us to implement all methods in O(1) time. There is still a problem with this approach. 2/24/2019 CS210-Summer 2005, Lecture 5

12 A Simple Array based implementation
Q f, r Q 4 enqueue(4) f r Q dequeue(4) f, r Q 4 enqueue(4) f r If we do enqueue(4) and dequeue(4) 7 times. We would have f = r = 7. Next time we will get ArrayOutOfBoundsException. 2/24/2019 CS210-Summer 2005, Lecture 5

13 A Simple Array based implementation
Solution: Use an array of size N in a circular fashion We let the f and r indices wrap around the end of Q. normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

14 A Simple Array based implementation
We compute the increment of f and r as (f + 1) mod N (r + 1) mod N Another problem: If we enqueue N objects into Q without dequeuing any of them. We would have f = r, which is the same condition that occurs when the queue is empty. No difference between full queue and empty queue. 2/24/2019 CS210-Summer 2005, Lecture 5

15 A Simple Array based implementation
Solution: Insist that Q can never hold more than N – 1 objects. Size ? (N – f + r) mod N. When list is empty f = r = 0 Size = N mod N = 0 When list is full i.e N – 1 elements f = 0 r = N – 1 Size = (N – 0 + N – 1) mod N = N - 1 2/24/2019 CS210-Summer 2005, Lecture 5

16 Queue Operations We use the modulo operator (remainder of division)
Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

17 Queue Operations Operation enqueue throws an exception if the array is full This exception is implementation-dependent Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

18 Queue Operations Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Q 1 2 r f Q 1 2 f r 2/24/2019 CS210-Summer 2005, Lecture 5

19 Analyzing the Array Based Queue Implementation
Method Time size O(1) isEmpty O(1) front O(1) enqueue O(1) dequeue O(1) 2/24/2019 CS210-Summer 2005, Lecture 5

20 Drawbacks of Array based implementation
Array based implementation must assume a fixed upper bound N on the ultimate size of the stack. An application may actually need more or less queue capacity than this. Still in cases where we have a good estimate on the number of items to be stored in the queue, the array based implementation is hard to beat. 2/24/2019 CS210-Summer 2005, Lecture 5

21 Round Robin Schedulers
We can implement a round robin scheduler using a queue, Q, by repeatedly performing the following steps: e = Q.dequeue() Service element e Q.enqueue(e) The Queue Shared Service 1 . Deque the next element 3 Enqueue the serviced element 2 Service the 2/24/2019 CS210-Summer 2005, Lecture 5

22 The “Pass the Pillow” game
A group of n children sit in a circle passing a pillow around the circle. Children continue passing the pillow until a leader rings a bell, at which point the child holding the pillow must leave the game after handling the pillow to the next child in the circle. The process is then continued until there is only one child remaining, who is declared the winner. If leader always rings the bell after the pillow has been passed k times, then determining the winner for a given list of children in known as “Josephus Problem”. 2/24/2019 CS210-Summer 2005, Lecture 5

23 Singly Linked Lists A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node next node elem head A B C D 2/24/2019 CS210-Summer 2005, Lecture 5

24 Singly Linked Lists The next reference inside a node can be viewed as a link or pointer to another node. Moving from one node to another by following a next reference is known as link hopping or pointer hopping. The first and last node are usually called head and tail of the list resp. Tail has a null next reference which indicates the termination of the list. 2/24/2019 CS210-Summer 2005, Lecture 5

25 Singly linked list vs. Arrays
Like an array, a singly linked list keeps the elements in a certain linear order, which is determined by the chain of next links between the nodes. Unlike an array, a singly linked list does not have a predetermined fixed size, and uses space proportional to the number of its elements. 2/24/2019 CS210-Summer 2005, Lecture 5

26 Inserting at the head 2/24/2019 CS210-Summer 2005, Lecture 5

27 Inserting at the head Allocate a new node Insert new element
Have new node point to old head Update head to point to new node 2/24/2019 CS210-Summer 2005, Lecture 5

28 Removing at the head Update head to point to next node in the list
Allow garbage collector to reclaim the former first node 2/24/2019 CS210-Summer 2005, Lecture 5

29 Inserting at the tail 2/24/2019 CS210-Summer 2005, Lecture 5

30 Inserting at the tail Allocate a new node Insert new element
Have new node point to null Have old last node point to new node Update tail to point to new node 2/24/2019 CS210-Summer 2005, Lecture 5

31 Removing at tail Removing at the tail of a singly linked list is not efficient! There is no constant-time way to update the tail to point to the previous node. 2/24/2019 CS210-Summer 2005, Lecture 5

32 Analyzing the Singly Linked List
Method Time Inserting at the head O(1) Inserting at the tail O(1) Deleting at the head O(1) Deleting at the tail O(n) 2/24/2019 CS210-Summer 2005, Lecture 5


Download ppt "CS210- Lecture 5 Jun 9, 2005 Agenda Queues"

Similar presentations


Ads by Google