Queue.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
Data Structure (Part I) Stacks and Queues. Introduction to Stack An stack is a ordered list in which insertion and deletions are made at one end. –The.
Topic 9 The Queue ADT.
What is a Queue? n Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Queue Overview Queue ADT Basic operations of queue
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
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.
Data Structures - Queues
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
CS 204 Stacks and Queues (not in any book that we are using) From Starting out with C++, 7th edition, by Gaddis, Walters and Muganda (chapter 18, pp
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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,
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
The Queue ADT.
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Unit-3 Queues-operations, array and linked representations. Circular Queue operations, Dequeues, applications of queue.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queues.
Csc 2720 Data structure Instructor: Zhuojun Duan
Stacks and Queues.
Queues Queues Queues.
Stack and Queue APURBO DATTA.
Stacks Stack: restricted variant of list
CMSC 341 Lecture 5 Stacks, Queues
Chapter 19: Stacks and Queues.
Queues.
(not in any book that we are using)
CSC 143 Queues [Chapter 7].
Queues: Implemented using Arrays
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Pointers & Dynamic Data Structures
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Visit for more Learning Resources
Circular Queues: Implemented using Arrays
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Queues Definition of a Queue Examples of Queues
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Getting queues right … finally (?)
Queues Dr. Anwar Ghani Lecture 06: Queue Data Structures
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures & Programming
Presentation transcript:

Queue

What is a queue? Queues have two ends: It is an ordered group of homogeneous items, examples: The cars at a stop light, The check-out line at a grocery store Queues have two ends: Items are added at one end. Items are removed from the other end. FIFO property: First In, First Out The item added first is also removed first

Conceptual View of a Queue Adding an element Front of queue New element is added to the rear of the queue

Conceptual View of a Queue Removing an element New front element of queue Element is removed from the front of the queue

Uses of Queues in Computing For any kind of problem involving FIFO data Printer queue Keyboard input buffer GUI event queue (click on buttons, menu items)

Uses of Queues in Computing In simulation studies, where the goal is to reduce waiting times: Optimize the flow of traffic at a traffic light Determine number of cashiers to have on duty at a grocery store at different times of day Ticket counter simulation

Queue Operations enqueue : add an element to the tail of a queue dequeue : remove an element from the head of a queue front : examine the element at the head of the queue (“peek”) Other useful operations (e.g. is the queue empty) It is not legal to access the elements in the middle of the queue!

Array Implementation of a Queue First Approach: Use an array in which index 0 represents one end of the queue (the front) Integer value last represents the last occupied slot in the array (the number of elements currently in the queue will be last + 1) Discussion: What is the challenge with this approach?

Operations on a Queue Operation Description dequeue enqueue front Removes an element from the front of the queue enqueue Adds an element to the rear of the queue front Examines the element at the front of the queue isEmpty Determines whether the queue is empty size Determines the number of elements in the queue isFull Determines whether the queue is full

An Array Implementation of a Queue A queue aq containing four elements first 1 2 3 4 … queueArray aq 3 last

Queue After Adding an Element last is incremented, Element is added at the array location given by the new last first 1 2 3 4 … queueArray aq 4 last

Queue After Removing an Element Element is removed from array location 0, remaining elements are shifted forward one position in the array, and then last is decremented. 1 2 3 4 … queueArray aq 3 last

Operations on a Queue Operation Description dequeue enqueue front Removes an element from the front of the queue enqueue Adds an element to the rear of the queue front Examines the element at the front of the queue isEmpty Determines whether the queue is empty size Determines the number of elements in the queue isFull Determines whether the queue is full

Second Approach: Queue as a Circular Array If we don't fix one end of the queue at index 0, we won't have to shift elements Circular array is an array that conceptually loops around on itself The last index is thought to “precede” index 0 In an array whose last index is n, the location “before” index 0 is index n; the location “after” index n is index 0 Need to keep track of where the first as well as the last of the queue are at any given time

Conceptual Example of a Circular Queue first After 5 dequeues 1 1 After 7 enqueues first 12 12 11 last 11 last 10 10 last 1 first 12 11 After 8 more enqueues 10

Circular Array Implementation of a Queue 3 2 3 4 1 first queueArray 5 cq 7 5 6 n-1 last currentsize n-2 7 n-3 8 . 9 . 10 .

A Queue Straddling the End of a Circular Array 98 2 3 4 1 first queueArray 5 cq 1 4 6 99 last currentSize 98 7 97 8 . 9 . 10 .

Circular Queue Drawn Linearly Queue from previous slide 98 cq first queueArray 1 2 3 4 96 97 98 99 … 1 4 last currentSize

Circular Array Implementation When an element is enqueued, the value of last is incremented But it must take into account the need to loop back to index 0: last = (last+1) % queue’sLength; Yes, so it may need enlarging

#ifndef INT_QUEUE_H_INCLUDED #define INT_QUEUE_H_INCLUDED   class intQueue { private: int currentsize, arraysize, first, last; int *queueArray; public: intQueue(int s = 100); // Constructor ~intQueue(); // Destructor intQueue(const intQueue& other); // Copy Constructor intQueue& operator=(const intQueue& other); // Assignment operator bool enqueue(int); // Add to end bool dequeue(); // Remove from front int front() const; // Get value at front bool isEmpty() const { return currentsize <= 0; } bool isFull() const { return currentsize >= arraysize; } int Size() const { return currentsize; } }; #endif // INT_QUEUE_H_INCLUDED

#include "intQueue.h" #include <iostream> #include <cstdlib>   using namespace std; intQueue::intQueue(int s) // Constructor { queueArray = new int[s]; // Dynamically allocate array arraysize = s; currentsize = 0; first = 0; last = -1; } intQueue::~intQueue() // Destructor delete[] queueArray; // Deallocate array

intQueue::intQueue(const intQueue& other) // Copy Constructor { arraysize = other.arraysize; currentsize = other.currentsize; first = other.first; last = other.last; queueArray = new int[arraysize]; // Dynamically allocate array   for (int i=0; i<arraysize; i++) // Copy array contents queueArray[i] = other.queueArray[i]; } intQueue& intQueue::operator=(const intQueue& rhs) if (this == &rhs) return *this; // handle self assignment arraysize = rhs.arraysize; currentsize = rhs.currentsize; first = rhs.first; last = rhs.last; delete [] queueArray; // Deallocate old array queueArray = new int[arraysize]; // Dynamically allocate new one for (int i=0; i<arraysize; i++) // Copy array contents queueArray[i] = rhs.queueArray[i]; return *this;

bool intQueue::enqueue(int value) { if (currentsize >= arraysize) return false; last = (last + 1) % arraysize; queueArray[last] = value; currentsize++; return true; }   bool intQueue::dequeue() if (currentsize < 1) return false; first = (first + 1) % arraysize; currentsize--; int intQueue::front() const if (currentsize < 1) cout << "Cannot take front of empty queue!\n"; exit(0); return queueArray[first];