Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain.

Similar presentations


Presentation on theme: "Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain."— Presentation transcript:

1 Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain

2 introduction A queue is a first-in-first-out (FIFO) sequential data structure in which elements are added (enqueued) at one end (the back) and elements are removed (dequeued) at the other end (the front). A good example is a queue. We encounter queues all the time in every day life. What makes a queue a queue? What is the essence of queueness? BA FrontRear

3 Queue Concept

4 Queue applications Print server  maintains a queue of print jobs. Disk driver  maintains a queue of disk input/output requests. Scheduler (e.g., in an operating system)  maintains a queue of processes awaiting a slice of machine time.

5 Operation on Queue using array Insert Delete Empty full

6 Queue First-in, First-out (FIFO) structure Operations ◦ insertqueue: insert element at rear ◦ deletequeue: remove & return front element ◦ empty: check if the queue has no elements Sample use ◦ handling requests and reservations

7 Array Implementation of Queues An Object array and two integers ◦ front: index of first element in queue ◦ rear: index of first FREE element in queue... front rear 0 4

8 ArrayQueue public class ArrayQueue implements Queue { Object store[]; int front, rear; static final int MAX = 100; public ArrayQueue() { store = new Object[MAX]; front = rear = 0; } //... }

9 Check for Empty and Enqueue public class ArrayQueue implements Queue { //... public boolean empty() { // queue is empty if first element // is the same as the first free element return ( front == rear ); } public void enqueue( Object o ) { // put data in first free slot // move rear index to next slot if ( rear < MAX ) store[rear++] = o; } //... }... front rear 0 4

10 Dequeue Operation public class ArrayQueue implements Queue { //... public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { return store[front++]; }... front rear 0 4

11 Dequeue that allows GC public class ArrayQueue implements Queue { // … public Object dequeue() throws Exception { if ( empty() ) { throw new Exception(); } else { Object data = store[front]; // get front data // set pointer to null, so it can be garbage-collected // later (when caller code doesn’t let’s go of pointer store[front] = null; front++; return data; }... front rear 0 4

12 Circular Array Suppose many enqueue operations followed by many dequeue operations Result: rear approaches MAX but the queue is not really full Solution: Circular Array ◦ allow rear (and front) to “wrap around” the array (if rear = MAX-1, incrementing rear means resetting it to 0)

13 Alternative Alternative ways of visualizing a cyclic array (length 8)

14 Empty operation Return (queue_of_front == queue_of_rear)

15 Remove operation If (empty(queue) { Print(“underflow); exit; } If (queue_of_front == max-1) queue_of_front =0; Else (queue_of_front)++; Return (queue_items[queue_of_front]);

16 Insert operation If(queue_of_rear == max-1) Queue_of_rear=0; Else (queue_of_rear)++; If (queue_of_rear ==queue_of_front) { Print(“queue overflow”); (queue_of_rear)--; exit; } Queue_items[queue_of_rear]=x;

17 Circular Array, continued When is the array full? ◦ Simple answer: when (rear == front) ◦ Problem: this is the same condition as empty Solution: Reserve a slot ◦ full: when ( (rear+1) % MAX == front) (one free slot left) ◦ empty: when ( rear == front ) Note: “wastes” a slot ◦ alternative: have a boolean field called hasElements ◦ full: when ( hasElements && (rear == front)) ◦ But not really better  hasElements takes up extra space too  Also, need to take care of hasElements in enqueue and dequeue

18 Revised Enqueue public class ArrayQueue implements Queue { //... public void enqueue( Object o ) { if ( (rear + 1) % MAX != front ) { store[rear] = o; rear = (rear + 1) % MAX; } //... }

19 Linked List Implementation front null rear

20 The ObjectNode Class public class ObjectNode { Object data; ObjectNode next; public void setData( Object o ) { data = o; } public Object getData() { return data; } public void setNext( ObjectNode p ) { next = p; } public ObjectNode getNext() { return next; } }

21 Enqueue front null rear null

22 Dequeue front null rear return this object

23 The LinkedQueue Class public class LinkedQueue implements Queue { ObjectNode front; ObjectNode rear; public LinkedQueue() { front = rear = null; } // other Queue methods }

24 Applications Queue for bill submission Play list of audio songs. Printer job scheduling ◦ Round Robin scheduler Joseph problem discussed in book.

25 Performance the time needed to add or delete an item is constant and independent of the number of items in the queue. both addition and deletion as an O(1) operation. For any given real machine+operating system+language combination, addition may take c1 seconds and deletion c2 seconds, but we aren't interested in the value of the constant, it will vary from machine to machine, language to language, etc. The key point is that the time is not dependent on n - producing O(1) algorithms. O(1) methods are already very fast, and it's unlikely that effort expended in improving such a method will produce much real gain!

26 Priority Queues Often the items added to a queue have a priority associated with them: this priority determines the order in which they exit the queue - highest priority items are removed first. This situation arises often in process control systems. Imagine the operator's console in a large automated factory. occasionally something breaks or fails and alarm messages are sent. These have high priority because some action is required to fix the problem

27 Priority Queue The priority queue is a data structure in which th natural ordering of the elements does determine the results of its basic operations. Two types of priority queue ◦ Ascending ◦ Descending Stack can be view as descending priority queue, whose element are ordered by time of insertion. Queue is ascending priority queue.

28 Array implementation of priority Queue The delete operation in ascending priority queue. This raise two issues ◦ Locate smallest element, Every element of the array must examined. ◦ How element in the middle of array be deleted. Priority queue deletion requires both searching and deletion.

29 Solutions 1. An empty indicator can be placed into deleted position. can be value or separate field. Disadvantages are search process to locate max or min examine all deleted positions. 2. Each deletion campact the array by shiffting all elements. So the deletion become so inefficient. 3. Maintain an array as an ordered circuler array.this method moves the work of searching and shiffting from deletion operation to insertion.

30 Solution In the last solution array is sorted and searching is half expensive as compared to unordered array.

31 Link implementation of priority queue Home work/Lab

32 Disadvantage by link list Link list occupies more storage than a corresponding element in an array. Time spent in Management of available list


Download ppt "Data Structures and Algorithms Lecture (Queues) Instructor: Quratulain."

Similar presentations


Ads by Google