Queues What is a queue Firstin firstout data
- Slides: 18
Queues
What is a queue? • First-in first-out data structure (FIFO) • New objects are placed at rear • Removal restricted to front • Examples?
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 • is. Empty(): Return a boolean indicating queue empty – Input: None; Output: Boolean • first(): Return object at front without removing; error if empty – Input: None; Output: Object
Example • • • enqueue(5) enqueue(3) dequeue() enqueue(7) dequeue() front() dequeue() is. Empty() • • • enqueue(9) enqueue(7) size() enqueue(3) enqueue(5) dequeue()
Queue Interface int size() const; bool is. Empty() const; Object& front() throw (Queue. Empty. Exception); void enqueue(const Object& obj); Object dequeue() throw(Queue. Empty. Exception);
Underlying Representation • Array versus Linked List – Pros and cons? • Running time? – size – is. Empty – enqueue – dequeue – front
Array Implementation enqueue(5) enqueue(3) dequeue() ? 0 1 5 3 0 1 2 3 4 5 6 … n-1
Array Implementation enqueue(5) enqueue(3) 0 1 5 3 0 1 2 3 4 5 6 … n-1 3 dequeue() ? 0 1
Circular Array f 0 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?
Circular Array f 0 r 1 2 3 4 5 6 … • 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? n-1
Circular Array r 0 1 2 3 f 4 5 6 … • Need to be able to wrap around • Modulo – % – increment f using (f+1)%n – increment r using (r+1)%n n-1
Circular Array f r 0 1 2 r f f dequeue 0 f 1 r 2 enqueue r =(2+1)%3= 0 enqueue 0 1 f 2 r enqueue 0 1 2
Algorithms • size – return (N-f+r) mod N • is. Empty – return (f == r) • front – if is. Empty then throw Queue. Empty. Exception – return array[f] • dequeue – – • if is. Empty then throw Queue. Empty. Exception temp = array[f] f = (f+1)%N return temp enqueue – if size == N-1 then throw Queue. Full. Exception SIZE MUST BE < N-1 – array[r] = object – r = (r+1)%N
Linked List Implementation tail head • enqueue • dequeue Ø
Linked List Implementation new_node head tail Object next_ptr • enqueue – if(size == 0) head = new_node – else tail->next_ptr = new_node – tail = new_node – increment size
Linked List Implementation new_node Object 2 next_ptr head tail Object 1 next_ptr • enqueue – if(size == 0) head = new_node – else tail->next_ptr = new_node – tail = new_node – increment size
Linked List Implementation • dequeue – if (is. Empty) throw Queue. Empty. Exception – head = head->next_ptr – decrement size – if new size is 0 – set tail to NULL
Exercises • Implement a queue using two stacks • Implement a two-color stack
- Queue adalah jenis antrian
- Priority queue vs queue java
- Stacks and queues in python
- Priority queues: quiz
- Java stacks and queues
- Representation of queues
- Java stack exercises
- Ipcs unix
- Adaptable priority queue java
- Rtos mailbox
- Applications of priority queues
- Define a queue
- Mgh
- What are stacks
- Contoh queue
- Queue abstract data type
- Priority queue abstract data type
- Queue abstract data type
- Static data structure and dynamic data structure