What is a Queue? A queue is a FIFO “first in, first out” structure.

Slides:



Advertisements
Similar presentations
Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Advertisements

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.
Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
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.
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 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
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.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
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.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
1 Chapter 6 Object-Oriented Software Development.
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.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Data Structures: Stacks Queues 1. 2 Stack ADT: What is a Stack? a stack is a varying-length, collection of homogeneous elements Insertion and Deletion.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
Chapter 9 Heaps and Priority Queues Lecture 18. Full Binary Tree Every non-leaf node has two children Leaves are on the same level Full Binary Tree.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
18 Chapter Stacks and Queues
CS505 Data Structures and Algorithms
Chapter 18: Stacks and Queues.
CC 215 Data Structures Queue ADT
Queues.
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Chapter 16-2 Linked Structures
Queues.
Chapter 19: Stacks and Queues.
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

What is a Queue? A queue is a FIFO “first in, first out” structure. 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.

Queues 2 2

Queues: Application Level For what type of problems would queues be useful? 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. 3 3

Application of Queue and Stack Palindromes: words or phrases that read the same in both directions, e.g. EYE, RACECAR, MADAM I'M ADAM, and : Testing if a given word or phrase is a palindrome: Scan the string character by character, and store each character into a stack(push) and queue(enqueue) After all characters have been processed, repeatedly pop a character from the stack, and deque a character from the queue, and compare them. Not match=>not a palindrome If finished comparing with no mismatches => a palindrome 4 4

Queues: implementation level class QueueType { public: QueueType(int max); QueueType(); ~QueueType(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); //add newItem to the rear of the queue. void Dequeue(ItemType& item); //remove front item from queue 5 5

Array-Based Implementation private: ItemType* items; // Dynamically allocated array int maxQue; // Whatever else we need }; Implementation level 6 6

Array-Based Implementation An array with the front of the queue fixed in the first position. Enqueue A, B, C, D Dequeue Shift elements up by 1 What’s the problem with this design? 7 7

Array-Based Implementation Another data structure: An array where the front floats. What happens if we add X, Y, and Z? 8 8

Array-Based Implementation Let the queue wrap around in the array; i.e. treat the array as a circular structure. 9 9

Array-Based Implementation (a) Initial conditions front = 2 rear = 2 (b) queue.Dequeue(item) front = 3 A [0] [1] [2] [3] [4] Empty Queue Full Queue How can we tell the difference? 10

Array-Based Implementation Reserve the slot (not used) before front item. front is the index of the slot before front item. Empty Queue Full Queue 11 11

DYNAMIC ARRAY IMPLEMENTATION class QueType QueType Private Data: front 1 rear 4 maxQue 5 items ~QueType Enqueue ‘C’ ‘X’ ‘J’ RESERVED Dequeue . items [0] [1] [2] [3] [4] 12 12

Array-Based Implementation private: int front; int rear; int maxQue; ItemType* items; }; Complete implementation level 13 13

Array-Based Implementation //returns true if the queue is empty bool QueueType::IsEmpty( ) { return ( rear == front ); } //returns true if the queue is full bool QueueType::IsFull( ) return ( (rear + 1) % maxQue == front ) 14 14

Array-Based Implementation //Pre: the queue is not full //Post: newItem is at the rear of the queue void QueueType::Enqueue(ItemType newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; } 15 15

Array-Based Implementation //pre: the queue is not empty //post: the front of the queue has been removed // and a copy returned in item void QueueType::Dequeue(ItemType& item) { front = (front + 1) % maxQue; item = items[front]; } 16 16

Array-Based Implementation QueueType::QueueType( int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; } QueueType::QueueType( ) { maxQue = 500 + 1; Why is the array declared max + 1? 17 17

Counted Queue* Counted Queue A queue that has an additional function GetLength that returns the number of items in the queue. The CountedQueType class will inherit (extend) the QueType class. What functions must be defined in class CountedQueType? 18 18

Counted Queue* class CountedQueueType : public QueueType { public: void Enqueue(ItemType newItem); void Dequeue(ItemType& item); int GetLength( ) const; private: int length; }; 19 19

Counted Queue* 20 20

Counted Queue* What is this? Why must the call to Enqueue be void CountedQueueType::Enqueue(ItemType newItem) { try QueueType::Enqueue(newItem); length++; } catch(FullQueue) { throw FullQueue();} What is this? Why must the call to Enqueue be embedded within a try/catch statement? 21 21

Counted Queue* Does the derived class have access to the base class’s private data members in C++? 22 22

Linked Implementation Data structure for linked queue What data members are needed? 23 23

Linked Implementation Enqueue(newItem) Set newNode to the address of newly allocated node Set Info(newNode) to newItem Set Next(newNode) to NULL Set Next(rear) to newNode If queue is empty Set front to newNode else 24 24

Linked Implementation Dequeue(item) Set tempPtr to front Set item to Info(front) Set front to Next(front) if queue is now empty Set rear to NULL Deallocate Node(tempPtr) 25 25

Circular Linked Queue Make the linked queue circular: Make the next member of rear node point to the front node of the queue Maintain a single pointer: rear Accessing front node is a constant time operation: rear->next 26

What does this table tell you? Storage Requirements What does this table tell you? 27 27

Big-O Comparison (Queue) Time Array-Based Implementation Linked Implementation Class constructor Class destructor O(1) O(n) IsFull() IsEmpty() Enqueue() Dequeue() 28 28

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE #include "ItemType.h" // for ItemType template<class ItemType> class QueType { public: QueType( ); QueType( int max ); // PARAMETERIZED CONSTRUCTOR ~QueType( ); // DESTRUCTOR . . . bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); private: int front; int rear; int maxQue; ItemType* items; // DYNAMIC ARRAY IMPLEMENTATION }; 29

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d template<class ItemType> QueType<ItemType>::QueType( int max ) // PARAMETERIZED { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; // dynamically allocates } bool QueType<ItemType>::IsEmpty( ) return ( rear == front ) 30

//-------------------------------------------------------- // CLASS TEMPLATE DEFINITION FOR CIRCULAR QUEUE cont’d template<class ItemType> QueType<ItemType>::~QueType( ) { delete [ ] items; // deallocates array } . bool QueType<ItemType>::IsFull( ) { // WRAP AROUND return ( (rear + 1) % maxQue == front ) 31

// DERIVED CLASS CountedQueType FROM BASE CLASS QueType ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQueType // DERIVED CLASS CountedQueType FROM BASE CLASS QueType template<class ItemType> class CountedQueType : public QueType<ItemType> { public: CountedQueType( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int LengthIs( ) const; // Returns number of items on the counted queue. private: int length; };

// Member function definitions for class CountedQue template<class ItemType> CountedQueType<ItemType>::CountedQueType( ) : QueType<ItemType>( ) { length = 0 ; } int CountedQueType<ItemType>::LengthIs( ) const return length ; 33

template<class ItemType> void CountedQueType<ItemType>::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Increments length. { length++; QueType<ItemType>::Enqueue( newItem ); } void CountedQueType<ItemType>::Dequeue(ItemType& item ) // Removes item from the rear of the queue. // Decrements length. length--; QueType<ItemType>::Dequeue( item );

class QueType<char> Private Data: qFront qRear ‘C’ ‘Z’ ‘T’ ~QueType Enqueue Dequeue .

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template<class ItemType> class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ; // DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType<ItemType>* qFront; NodeType<ItemType>* qRear; }; 36

// DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE continued // member function definitions for class QueType template<class ItemType> QueType<ItemType>::QueType( ) // CONSTRUCTOR { qFront = NULL; qRear = NULL; } bool QueType<ItemType>::IsEmpty( ) const return ( qFront == NULL ) 37

template<class ItemType> void QueType<ItemType>::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Pre: Queue has been initialized. // Queue is not full. // Post: newItem is at rear of queue. { NodeType<ItemType>* ptr; ptr = new NodeType<ItemType>; ptr->info = newItem; ptr->next = NULL; if ( qRear == NULL ) qFront = ptr; else qRear->next = ptr; qRear = ptr; } 38

template<class ItemType> void QueType<ItemType>::Dequeue( ItemType& item ) // Removes element from from front of queue // and returns it in item. // Pre: Queue has been initialized. // Queue is not empty. // Post: Front element has been removed from queue. // item is a copy of removed element. { NodeType<ItemType>* tempPtr; tempPtr = qFront; item = qFront->info; qFront = qFront->next; if ( qFront == NULL ) qRear = NULL; delete tempPtr; } 39