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.

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

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
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.
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.
CS 240Chapter 7 - QueuesPage 29 Chapter 7 Queues The queue abstract data type is essentially a list using the FIFO (first-in-first-out) policy for adding.
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.
CHAPTER 7 Queues.
What is a Queue? A queue is a FIFO “first in, first out” structure.
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.”
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.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Introduction to Stacks & Queues.
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 7: Queues QUEUE IMPLEMENTATIONS QUEUE APPLICATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
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.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
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.
Implementing a Queue as a Linked Structure CS 308 – Data Structures.
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.”
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.
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 -
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++
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
1 Queues. 2 Queue Which of the following cases use similar structures? Cars lined up at a tollgate Cars on a 4-lane highway Customers at supermarket check-out.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
Queue What is a queue?. Queues A queue is similar to waiting in line for a service, e.g., at the bank, at the bathroom –The first item put on the 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.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
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.
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.
CHAPTER 5 QUEUE v2 by Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.
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 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
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.
CSCE 210 Data Structures and Algorithms
CS505 Data Structures and Algorithms
CC 215 Data Structures Queue ADT
Queues.
C++ Plus Data Structures
Stack and Queue APURBO DATTA.
Chapter 5 ADTs Stack and Queue.
Queues.
C++ Plus Data Structures
Queues.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
COMPUTER 2430 Object Oriented Programming and Data Structures I
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Queues.
Queue.
CSCS-200 Data Structure and Algorithms
Presentation transcript:

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 at one end. – Elements are removed from the other end. The element added first is also removed first (FIFO: First In, First Out).

Queue Specification Definitions: (provided by the user) – MAX_ITEMS: Max number of items that might be on the queue – ItemType: Data type of the items on the queue Operations –MakeEmpty –Boolean IsEmpty –Boolean IsFull –Enqueue (const ItemType & newItem) –ItemType Dequeue()

void Enqueue (const ItemType& newItem) Function: Adds newItem to the rear of the queue. Preconditions: Queue has been initialized and is not full. Postconditions: newItem is at rear of queue.

ItemType Dequeue () Function: Removes front item from queue and returns it in item. Preconditions: Queue has been initialized and is not empty. Postconditions: Front element has been removed from queue and is returned.

Implementation issues Implement the queue as a circular structure. How do we know if a queue is full or empty? Initialization of front and rear. Testing for a full or empty queue.

Make front point to the element preceding the front element in the queue (one memory location will be wasted).

Initialize front and rear

Queue is empty now!! rear == front

Queue overflow The condition resulting from trying to add an element onto a full queue. if(!q.IsFull()) q.Enqueue(item);

Queue underflow The condition resulting from trying to remove an element from an empty queue. if(!q.IsEmpty()) q.Dequeue(item);

Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. Able was I ere I saw Elba We will read the line of text into both a stack and a queue. Compare the contents of the stack and the queue character-by-character to see if they would produce the same string of characters.

Example: recognizing palindromes

#include #include "stack.h" #include "queue.h“ int main() { StackType s; QueType q; char ch; char sItem, qItem; int mismatches = 0; cout << "Enter string: " << endl; while(cin.peek() != '\\n') { cin >> ch; if(isalpha(ch)) { if(!s.IsFull()) s.Push(toupper(ch)); if(!q.IsFull()) q.Enqueue(toupper(ch)); }

Example: recognizing palindromes while( (!q.IsEmpty()) && (!s.IsEmpty()) ) { s.Pop(sItem); q.Dequeue(qItem); if(sItem != qItem) ++mismatches; } if (mismatches == 0) cout << "That is a palindrome" << endl; else cout << That is not a palindrome" << endl; return 0; }

Case Study: Simulation Queuing System: consists of servers and queues of objects to be served. Simulation: a program that determines how long items must wait in line before being served.

Case Study: Simulation (cont.) Inputs to the simulation: (1) the length of the simulation (2) the average transaction time (3) the number of servers (4) the average time between job arrivals

Case Study: Simulation (cont.) Parameters the simulation must vary: (1) number of servers (2) time between arrivals of items Output of simulation: average wait time.

Queue Implementation template class QueueType { public: QueueType(int); \\int for maxSize QueueType(); ~QueueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(const ItemType &); ItemType Dequeue(); private: int front; int rear; ItemType* items; //dynamic array int maxQue; };

Queue Implementation (cont.) template QueueType ::QueueType(int max) { maxQue = max + 1; front = maxQue - 1; rear = maxQue - 1; items = new ItemType[maxQue]; }

Queue Implementation (cont.) template QueueType ::~QueueType() { delete [] items; }

Queue Implementation (cont.) template void QueueType :: MakeEmpty() { front = maxQue - 1; rear = maxQue - 1; }

Queue Implementation (cont.) template bool QueueType ::IsEmpty() const { return (rear == front); } template bool QueueType ::IsFull() const { return ( (rear + 1) % maxQue == front); }

Queue Implementation (cont.) template void QueueType ::Enqueue (const ItemType & newItem) { rear = (rear + 1) % maxQue; items[rear] = newItem; }

Queue Implementation (cont.) template ItemType QueueType ::Dequeue() { front = (front + 1) % maxQue; return items[front]; }