Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,

Slides:



Advertisements
Similar presentations
1 Array-based Implementation An array Q of maximum size N Need to keep track the front and rear of the queue: f: index of the front object r: index immediately.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
queues1 Queues Data structures that wait their turn.
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.
CHAPTER 7 Queues.
5/4/20151 Queues Implementations. 5/4/20152 Outline Queues Basic operations Examples of useImplementations Array-based and linked list-based.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
CS Data Structures II Review COSC 2006 April 14, 2017
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Razdan with contribution from others 1 Chapter 7 Queues Anshuman Razdan Div of Computing Studies
Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation – FIFO As items are added they are chronologically ordered, items are removed.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 5: Stacks and Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
EC-211 DATA STRUCTURES LECTURE 9. Queue Data Structure An ordered group of homogeneous items or elements. Queues have two ends: – Elements are added at.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 18: Stacks and Queues.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
UNIT II Queue. Syllabus Contents Concept of queue as ADT Implementation using linked and sequential organization. – linear – circular queue Concept –
Chapter 7 Queues Introduction Queue applications Implementations.
M180: Data Structures & Algorithms in Java Queues Arab Open University 1.
Linear Data Structures
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
1 Lecture 9: Stack and Queue. What is a Stack Stack of Books 2.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Abstract Data Types Queues Dale Roberts, Lecturer
STACKS & QUEUES for CLASS XII ( C++).
Review Array Array Elements Accessing array elements
Data Structures Using C, 2e
Queues.
CS505 Data Structures and Algorithms
UNIT II Queue.
CC 215 Data Structures Queue ADT
Queues Implementations 6/17/2018.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Queues.
Queues.
CSC 143 Queues [Chapter 7].
Data Structures and Algorithms for Information Processing
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
CE 221 Data Structures and Algorithms
CSCS-200 Data Structure and Algorithms
Lecture 9: Stack and Queue
Presentation transcript:

Chapter 7 Queues

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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; }

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; }

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

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

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

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++; }

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

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; }

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

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

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

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

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

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