Download presentation

Presentation is loading. Please wait.

Published byWade Frake Modified over 2 years ago

1
Chapter 7 Queues

2
Data Structure 2 Chapter Outline Objectives Follow and explain queue-based algorithms using the front, rear, entering the queue, and exiting the queue Use a Queue class to implement stack-based algorithm such as scheduling first come, first serve Recognize situations that require a priority queue Contents Introduction to Queues Queue Applications Implementations of the Queue ADT Priority Queues

3
Data Structure 3 What is a Queue? Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). A queue is a FIFO “first in, first out” structure.

4
Data Structure 4 The Queue ADT Queue’s method Enqueue : Adding an item to the queue “Entering the queue” Dequeue : Removing an item from the queue “Deleting from the queue” Queue underflow If a program attempts to remove an item from an empty queue

5
Data Structure 5 Contrasting a Stack and a Queue A B C D Input : A B C D stack A B C D top front Rear Queue

6
Data Structure 6 Uses for Queues Copying a word 1. Declare a queue of characters 2. While(there are more characters of the word to read) { Read a character Insert the character into the queue } 3. While(the queue is not empty) { Get the front character from the queue Write the character to the screen } Simulation programs such as the traffic ate an intersection Input/Output buffering

7
Data Structure 7 Array Implementation of a Queue Add items at one end of the array and Remove items for other end Access the used portion of the array at both ends, increasing the size of the used portion at one end and decreasing the size of the used portion at the other end Need a two variables for tracking the array front rear

8
Data Structure 8 Array Implementation of a Queue Queue items will be in the array components data[front], data[front+1],... data[rear] To add an item, increment rear and store the new item To get the next item, retrieve data[front] rear is incremented but never decremented ? Reach the end of the array variable front would be incremented free up the array locations with index values less than front

9
Data Structure 9 Array Implementation of a Queue Way to reuse freed locations Maintain all the queue items so that front is equal to 0 It is very inefficient Think of the array as being bent into a circle When the rear index reaches the end of the array, start using the available locations at the front of the array The successor of the last array index is the first array index

10
Data Structure 10 Array Implementation of a Queue [0] [1] [2] [3] [4] A B C ? ? 0 0 2 2 frontrear [0] [1] [2] [3] [4] ? ? C D E 2 2 4 4 frontrear data

11
Data Structure 11 Array Implementation of a Queue [0] [1] [2] [3] [4] F ? C D E 2 2 0 0 frontrear data

12
Data Structure 12 Array Implementation of a Queue Circular array ? C D E F data 2 2 0 0 [0] [1] [2] [3] [4] front rear

13
Data Structure 13 Array Implementation of a Queue data : Queue’s item are held in data front, rear : private instance variable hold the indexes for the front and rear manyItems : records the number of items that are in the queue nextIndex : private method Helper method It is not part of the public specification It is just for our own use in implementing a specific kind of queue

14
Data Structure 14 Array Implementation of a Queue Helper Methods When a class requires some small computation that is likely to be used several times Improve the clarity of code Is private, Does not need to be included in the documentation

15
Data Structure 15 Array Implementation of a Queue Invariant of the Queue ADT The number of items in the queue is stored in the instance variable manyItems For a nonempty queue, the items are stored in a circular array beginning at data[front] and continuing through data[rear] For an empty queue, manyItems is zero and data is a reference to an array, but we are not using any of the array

16
Data Structure 16 Array Implementation of a Queue ensureCapacity method If the array is already big enough, we return with no work Otherwise, allocate a new larger array and copy the elements from original array to the new array Three cases If manyItems is zero If manyItems is nonzero and front <= rear If manyItems is nonzero and front > rear

17
Data Structure 17 Array Implementation of a Queue ensureCapacity method [0] [1] [2] [3] [4] C D ? A B v v [0] [1] [2] [3] [4] A B C D ? [5] [6] [7] [8] ? ? rear front rear

18
Data Structure 18 Array Implementation of a Queue public void ensureCapacity(int minimumCapacity) { Object biggerArray[]; int n1, n2; if(data.length >= minimumCapacity) return; else if(manyItems == 0) data = new Object[minimumCapacity]; else if(front <= rear) { biggerArray = new Object[minimumCapacity]; System.arraycopy(data, front, biggerArray, front, manyItems); data = biggerArray; }

19
Data Structure 19 Array Implementation of a Queue else { biggerArray = new Object[minimumCapacity]; n1 = data.length – front; n2 = rear + 1; System.arraycopy(data, front, biggerArray, 0 n1); System.arraycopy(data, 0, biggerArray, n1, n2); front = 0; rear = manyItems - 1; data = biggerArray; }

20
Data Structure 20 Linked List Implementation of a Queue Front Item Second Item Third Item null front rear

21
Data Structure 21 Linked List Implementation of a Queue Invariant of the Queue ADT The numbers of items in the queue is stored in the manyNodes The items in the queue are stored in a linked list with the front of the queue stored at the head node and the rear of the queue stored at the final node For a nonempty queue, front is the head reference For an empty queue, both front and rear are the null reference

22
Data Structure 22 Linked List Implementation of a Queue Insert method 20 30 null front 10 rear

23
Data Structure 23 Linked List Implementation of a Queue Insert method public void insert(Object item) { if(isEmpty()) { front = new Node(item, null); rear = front; } else { rear.addNodeAfter(item); rear = rear.getLink(); } manyNodes++; }

24
Data Structure 24 Linked List Implementation of a Queue getFront method 20 30 null front 10 rear 20 30 null front rear

25
Data Structure 25 Linked List Implementation of a Queue getFront method Public Object getFront() { Object answer; if(manyNodes == 0) throw new NoSuchElementException(“Queue underflow.”); answer = front.getData(); front = front.getLink(); ManyNodes--; if(manyNodes == 0) rear = null; return answer; }

26
Data Structure 26 Linked List Implementation of a Queue Forgetting Which End is Which 20 30 null front 10 rear null What’s the problem? Case 1) enqueue Case 2) dequeue

27
Data Structure 27 Priority Queues Is a data structure that stores items along with a priority for each item. Items are removed in order of priorities Ginger Snap, priority 0 Natalie Attired, priority 3 Emanual Transmission, priority 2 Gene Pool, priority 3 Kay Sera, priority 2

28
Data Structure 28 Priority Queues Constructor for the ObjectPriorityQueue Public ObjectPriorityQueue(int highest) Initialize an empty priority queue getFront public Object getFront() Get the highest priority item, removing it from this priority queue Precondition: this queue is not empty Postcondition : return value is highest priority item of this queue and the item has been removed

29
Data Structure 29 Priority Queues Insert public void insert(Object item, int priority) Add a new item to this priority queue Precondition : 0<= priority and priority is no more than the highest priority Postcondition : the item has been added to this priority queue

30
Data Structure 30 Priority Queues ADT What is the best way to take advantage of the existing ordinary queue? To provide the priority queue with an array of ordinary queues Suppose highest is 2 Implemented using three ordinary queues One to hold the items with priority 0 Another queue for the items of priority 1 Third queue for the items of priority 2

31
Data Structure 31 Priority Queues ADT Example1) Ginger Snap, priority 0 Natalie Attired, priority 3 Emanual Transmission, priority 2 Gene Pool, priority 3 Kay Sera, priority 2 Queues[3] : Natalie Attired, Gene Pool Queues[2] : Emanual Transmission, Kay Sera Queues[1] : empty Queues[0] : Ginger Snap

Similar presentations

OK

Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.

Queues CS-212 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed in their.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on statistics and probability formulas Seminar ppt on computer science topics Ppt on applied operations research va Ppt on electricity distribution system in india Ppt on itc group of hotels Ppt on schottky diode characteristics Ppt on vitamin c deficiency Ppt on kingdom monera organisms Ppt on self development academy Ppt on water conservation in hindi