The Queue Data Structure Mugurel Ionu Andreica Spring 2012.

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

Stacks, Queues, and Linked Lists
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.
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.
The Stack Data Structure Mugurel Ionu Andreica Spring 2012.
1 Queues – Chapter 3 A queue is a data structure in which all additions are made at one end called the rear of the queue and all deletions are made from.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
1 Queues (5.2) CSE 2011 Winter May Announcements York Programming Contest Link also available from.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
CS Data Structures II Review COSC 2006 April 14, 2017
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures Mugurel Ionu Andreica Spring 2012.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Queue C and Data Structures Baojian Hua
Queues.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
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.
Queue C and Data Structures Baojian Hua
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
Data Structures - Queues
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks And Queues Chapter 18.
Foundations of Data Structures Practical Session #4 Recurrence ADT 08/04/2013Amihai Savir & Ilya Mirsky
Question of the Day  Three people check into a hotel for which they pay the manager $30. The manager finds out the rate is $25 and gives $5 to the bellboy.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
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.
The Graph Data Structure Mugurel Ionu Andreica Spring 2012.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
The Linked List Data Structure Mugurel Ionu Andreica Spring 2012.
Implementing Queues Eric Roberts CS 106B February 13, 2013.
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
QueueStack CS1020.
CSE 373: Data Structures and Algorithms
Queue data structure.
Queue, Deque, and Priority Queue Implementations
Queue, Deque, and Priority Queue Implementations
Lecture 21 Stacks and Queues Richard Gesick.
Queues.
Queue.
Stacks and Queues.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Initializing Objects.
The Heap Data Structure
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
ADT Queue (Array Implementation)
Queues Definition of a Queue Examples of Queues
Revised based on textbook author’s notes.
Stacks and Queues.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures & Programming
Presentation transcript:

The Queue Data Structure Mugurel Ionu Andreica Spring 2012

Operations enqueue(x) –Adds the element x at the tail of the queue dequeue() –Removes the element from the head of the queue and returns it –Returns an error if the stack is empty peek() –Returns (but does not remove) the element at the head of the queue isEmpty() –Returns 1 if the queue is empty and 0 otherwise The axioms are given implicitly – they determine the expected behavior of the queue for a given sequence of operations –See the examples on the upcoming slides

Queue - Example

Queue – Array-based Implementation (queue1.h) #define NMAX 100 template class Queue { private: T queueArray[NMAX]; int head, tail; public: void enqueue(T x) { if (tail >= NMAX) { fprintf(stderr, "Error The queue is full!\n"); return; } queueArray[tail] = x; tail++; } T dequeue() { if (isEmpty()) { fprintf(stderr, "Error The queue is empty!\n"); T x; return x; } T x = queueArray[head]; head++; return x; } T peek() { if (isEmpty()) { fprintf(stderr, "Error The queue is empty!\n"); T x; return x; } return queueArray[head]; } int isEmpty() { return (head == tail); } Queue() { head = tail = 0; // the queue is empty in the beginning } };

Using the Queue #include #include “queue1.h” int main() { Queue q; q.enqueue(7); q.enqueue(8); q.enqueue(6); printf("%d\n", q.dequeue()); printf("%d\n", q.peek()); q.enqueue(4); q.enqueue(2); printf("%d\n", q.dequeue()); printf("%d\n", q.isEmpty()); printf("%d\n", q.peek()); q.dequeue(); printf("%d\n", q.dequeue()); printf("%d\n", q.isEmpty()); return 0; }

Disadvantages of the Array-based Queue Implementation The head and tail variables are constantly increasing As elements are removed from the queue, the portion of the array which is effectively used shifts to the right We may reach the end of the array and be unable to enqueue any other elements, although a large fraction of the array (its left part) is empty (unused) Improved solution: circular array

Circular Array-based Queue - Example Circular array with NMAX=3 entries

Queue – Circular Array-based Implementation (queue2.h) #define NMAX 100 template class Queue { private: T queueArray[NMAX]; int head, tail, size; public: void enqueue(T x) { if (size == NMAX) { fprintf(stderr, "Error The queue is full!\n"); return; } queueArray[tail] = x; tail = (tail + 1) % NMAX; size++; } T dequeue() { if (isEmpty()) { fprintf(stderr, "Error The queue is empty!\n"); T x; return x; } T x = queueArray[head]; head = (head + 1) % NMAX; size--; return x; } T peek() { if (isEmpty()) { fprintf(stderr, "Error The queue is empty!\n"); T x; return x; } return queueArray[head]; } int isEmpty() { return (size == 0); } Queue() { head = tail = size = 0; } };