Download presentation
Presentation is loading. Please wait.
1
Queues
2
Queues A queue is restricted list in which items are inserted from rear end and deleted from front end. Example
3
Queues A queue is a linear list structure where elements can be inserted at any time, but only the elements that have been in the queue the longest that can be remove. The queue’s storage policy or principle is First-In, First-Out (FIFO). This means that the first element inserted will be the first to be removed in a sequence.
4
Application Of Queues Just like stack, queue will frequently be used by the programmer to model real-world situations . Some of the uses of Queue are listed below: When a resource is shared among multiple consumers. Examples include CPU scheduling, Disk Scheduling. Print Server. When placed on hold for telephone operators. For example, when you phone the toll-free number for your bank, you may get a recording that says, "Thank you for calling A-1 Bank. Your call will be answered by the next available operator. Please wait." This is a queuing system. 11/17/2018
5
Queue Operations Queue operations are:
Enqueue(e) -> Insert element e at the rear of the queue. Dequeue -> Remove and return from the queue the object at the front length() -> Return the number of elements in the queue Isempty() -> Return a Boolean value that indicates whether queue is empty. Isfull() -> Return a Boolean value that indicates whether queue is full.
6
Implementation of Queues
Queue can be implemented in two ways Arrays Linked Lists
7
Using Arrays First the size has to be defined and fixed.
For example: take size of queue is 6. Size of the Queue is 6 Enqueue(20) Enqueue(5) Front Front 20 20 5 Rear Rear Front = 0 Rear = 0 Front = 0 Rear = 1
8
Using Arrays Enqueue(10) Enqueue(23) Front Front length()=3 Elements
isfull() = False isempty() = False front() = 20 Rear() = 10 20 5 10 20 5 10 23 Rear Rear Front = 0 Rear = 2 Front = 0 Rear = 3 Enqueue(11) Enqueue(14) Front Front length()=6 Elements isfull() = True isempty() = False front() = 20 Rear() = 14 20 5 10 23 11 20 5 10 23 11 14 Rear Front = 0 Rear = 5 Rear Front = 0 Rear = 4
9
Using Arrays Enqueue(45) length () = 6 Elements isfull() = True
isempty() = False front() = 20 Rear() = 14 This state is called queue is full Front 20 5 10 23 11 14 Front = 0 Rear = 5 Rear Dequeue() Dequeue() Dequeue() Front Front Front 5 10 23 11 14 10 23 11 14 23 11 14 Front = 1 Rear = 5 Rear Front = 2 Rear = 5 Rear Front = 3 Rear = 5 Rear
10
Using Arrays Dequeue() Front length () = 2 Elements isfull() = False
isempty() = False front() = 11 Rear() = 14 11 14 Front = 4 Rear = 5 Rear Enqueue(75) To Insert Element 75, move all elements to first position. Such queue is called shifting queue. Front Front 11 14 11 14 75 Rear Rear Front = 4 Rear = 5 Front = 4 Rear = 5 Front = 0 Rear = 2
11
Using Arrays Dequeue() Dequeue() Front Front 14 75 75 Rear Rear
12
ADT Queue (Array Implementation)
Array implementation of the queue…a fixed size array is used to store the data elements. As data elements are enqueued & served the queue crawls through the array from low to high index values. As the queue crawls forward, it also expands and contracts.
13
ADT Queue (Array Implementation)
Head Tail After one En-queue and one Serve Head Tail
14
ADT Queue (Array Implementation)
Head Tail Where to En-queue this?
15
ADT Queue (Array Implementaion)
public class ArrayQueue <T> { private int maxsize; private int size; private int head, tail; private T[] nodes; /** Creates a new instance of ArrayQueue */ public ArrayQueue(int n) { maxsize = n; size = 0; head = tail = 0; nodes = (T[]) new Object[n]; }
16
ADT Queue (Array Implementation)
public boolean full () { return size == maxsize ? true : false; } public int length () { return size; public void enqueue(T e) { nodes[tail] = e; tail = (tail + 1); size++;
17
ADT Queue (Array Implementation)
public T serve () { T e = nodes[head]; head = (head + 1); size--; return e; }
18
Queues(linked list implementation)
Front Tail
19
ADT Queue: Specification
Structure: the elements are linearly arranged, and ordered according to the order of arrival, most recently arrived element is called the tail and least recently arrived element the front or head. Domain: the number of elements in the queue is bounded therefore the domain is finite. Type of elements: Queue
20
ADT Queue: Specification
Operations: Method Enqueue (Type e) requires: Queue Q is not full. input: Type e. results: Element e is added to the queue at its tail. output: none. Method Serve (Type e) requires: Queue Q is not empty. input: results: the element at the head of Q is removed and its value assigned to e. output: Type e. Method Length (int length) input: results: The number of element in the Queue Q is returned. output: length.
21
ADT Queue: Specification
Operations: Method Full (boolean flag). requires: input: results: If Q is full then flag is set to true, otherwise flag is set to false. output: flag.
22
ADT Queue (Linked Implementation)
public class LinkQueue <Type> { private Node<Type> head, tail; private int size; /** Creates a new instance of LinkQueue */ public LinkQueue() { head = tail = null; size = 0; }
23
ADT Queue (Linked Implementation)
public boolean full() { return false; } public int length (){ return size;
24
ADT Queue (Linked Implementation)
public void enqueue (Type e) { if (tail == null){ head = tail = new Node(e); } else { tail.next = new Node(e); tail = tail.next; size++;
25
ADT Queue (Linked Implementation)
public Type serve() { Type x; x = head.data; head = head.next; size--; if (size == 0) tail = null; return x; }
26
Series of Queue Operations
Output Queue enqueue(5) ----- 5 enqueue(3) ----- 5, 3 enqueue(-7) ----- 5, 3, -7 dequeue() 5 3, -7 enqueue(8) ----- 3, -7, 8 enqueue(-10) ----- 3, -7, 8, -10 dequeue() 3 -7, 8, -10 Size() 3 -7, 8, 10 dequeue() -7 8, 10 front() 8 8, 10
27
Queues- Evaluation Efficiency Same as stack:
enqueue: only at the back ? dequeue: only at the front ? Access: only at the front ? So, items can be inserted and removed from a queue in O(1) time.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.