CHAPTER 5 QUEUE v2 by www.asyrani.com. Queue in C++ [Non-English] It is an ordered group of homogeneous items of elements. [English] –You go to the shopping.

Slides:



Advertisements
Similar presentations
Stack & Queues COP 3502.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
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.
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.
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.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
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.
Chapter 7 Queues. Data Structure 2 Chapter Outline  Objectives  Follow and explain queue-based algorithms using the front, rear, entering the queue,
Fundamentals of Python: From First Programs Through 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.”
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.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
Queue Overview Queue ADT Basic operations of queue
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.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
1 Queues CPS212 Gordon College. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank, food.
CS 206 Introduction to Computer Science II 10 / 26 / 2009 Instructor: Michael Eckmann.
1 Queues and Priority Queues Chapter 8. 2 Introduction to Queues A queue is a waiting line – seen in daily life –Real world examples – toll booths, bank,
CS 106 Introduction to Computer Science I 12 / 13 / 2006 Instructor: Michael Eckmann.
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.
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a 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.”
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
Queue 1 Operations on Queues A Dequeue Operation An Enqueue Operation Array Implementation Link list Implementation Examples.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
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.
1 5. Queues Def. As a data structure, a queue is an ordered collection of data items with the property that items can be removed only at one end, called.
Data Structures Using C++
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
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.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Queues Chapter 5 Queue Definition A queue is an ordered collection of data items such that: –Items can be removed only at one end (the front of the queue)
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
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.
Queues Another Linear ADT Copyright © 2009 Curt Hill.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
 In general, Queue is line of person waiting for their turn at some service counter like ticket window at cinema hall, at bus stand or at railway station.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Chapter 4 Stacks
Review Array Array Elements Accessing array elements
CSCE 210 Data Structures and Algorithms
Data Structures Using C++ 2E
Data Structures Using C, 2e
Queues.
CC 215 Data Structures Queue ADT
Queues.
Lectures Queues Chapter 8 of textbook 1. Concepts of queue
Stacks and Queues.
Queues.
CSC 143 Queues [Chapter 7].
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Queues.
Queue.
CSCS-200 Data Structure and Algorithms
Data Structures Using C++ 2E
[Most of the details about queues are left for to read about and work out in Lab 6.] Def. As a data structure, a queue is an ordered collection of data.
Presentation transcript:

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 mall on Sunday (peak hour) –When it is time for you to pay, you need to queue up. –A lot of people (homogeneous items of elements) in front of you.

How does it works? Queues has two ends Elements are removed from the other end. Elements are added at one end.

How does it works? Queues has two ends After this guy pay bill at the cashier, He/she is the one That first got out. We called him/her as FIRST IN, FIRST OUT (FIFO)

So, what operations involved?

Before you queue up at the cashier to pay your groceries, you need to make sure the queue line exists. Make sure the cashier is available. Thus the queue line is available InitializeQueue: initialize the queue to an empty state. 1

So, what operations involved? But, sometimes you got no luck at all because when the time you arrives at the counter. The cashier says to you “Sorry sir, my shift is over and you must queue up in another queue line. I need to go back and feed my kittens” destroyQueue: removes all elements from the queue, leaving the Queue empty. 2

So, what operations involved? Also, maybe you arrives at the empty queue line but the cashier is there doing nothing. So, it is the checking the empty queue process isEmptyQueue: check wether the Queue empty, if empty it return the value true, otherwise it return value false. 3

So, what operations involved? And sometimes the queue is also full (well the security prevent you from enter a full parking space) isFullQueue: check whether the Queue is full, if full it return true, otherwise it return false. 4

So, what operations involved? So, when the queue line is not full but it is ok to queue up. You will queue up. addQueue/enQueue adds new element to the rear of Queue,input consists of Queue and new element. Queue must exist and must not be full. 5

So, what operations involved? After one of the guy in front of you have paid, then this guy will go away. removeQueue/deQueue: removes the front element from the Queue and store the front element to deqElement. Input consists of the Queue and location where the front element will be stored. Queue must exist and must not empty. 6

Queue Operations InitializeQueue: initialize the queue to an empty state. destroyQueue: removes all elements from the queue, leaving the Queue empty. isEmptyQueue: check wether the Queue empty, if empty it return the value true, otherwise it return value false. isFullQueue: check whether the Queue is full, if full it return true, otherwise it return false. addQueue:/enQueue adds new element to the rear of Queue,input consists of Queue and new element. Queue must exist and must not be full. removeQueue/deQueue: removes the front element from the Queue and store the front element to deqElement. Input consists of the Queue and location where the front element will be stored. Queue must exist and must not empty.

Enqueue/Dequeue Enqueue 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. 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 item is a copy of removed element.

Example : I/O buffers: queues, scrolls, deques 2. Queue here 1. Open file 3. Get input Input Buffer queues input for the CPU

Example : Scheduling queues in a multi-user computer system  Printer queue: When files are submitted to a printer, they are placed in the printer queue. The printer software executes an algorithm something like: for (...;...;...) { while (printerQueue.empty()) sleep 1; printFile = printerQueue.removeQ(); Print(printFile); }

Probably uses a priority queue: Items with lower priority are behind all those with higher priority. (Usually a new item is inserted behind those with the same priority.) Example : CPU Scheduling

1. Resident queue:On disk, waiting for memory 2. Ready queue:In memory — has everything it needs to run, except the CPU 3. Suspended queue:Waiting for I/O transfer or to be reassigned the CPU Other Tough Queues:

But how to implement queue? Implement the queue as a line or 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?

Let us look at example Declare Four (4) space for queue up process

Let us look at example Declare Four (4) space for queue up process..and name each space I’ve named it 0,1,2,3

Let us look at example Declare Four (4) space for queue up process..and name each space I’ve named it 0,1,2,3 For starting, Front and Rear Share the same place. Why? Because at the moment all four spaces are empty. Example : When you found that the queue line is empty. You are the first and the last customer queue up. Got it? Front/Rear

Let us look at example Now.. I want to use special command to put variable there. Let say q.Enqueue([number]) Example : q.Enqueue(2) So what q.Enqueue(2) do?

Let us look at example Now, we have used the command to put number “2” inside the empty space. q.Enqueue(2) Front Rear ->

Let us look at example Then, I need to put more numbers q.Enqueue(3) Front-> 2 0 Rear-> q.Enqueue(5) Front-> Rear-> 5 2 3

Let us look at example Watch that Rear has moved (position updated) after we added few new numbers. q.Enqueue(3) Front-> 2 0 Rear-> q.Enqueue(5) Front-> Rear-> 5 2 3

Let us look at example Now, we want to use another command q.Dequeue(item) What is that? “Because the guy in front of you used old credit card So before waiting for card Transaction is processed, the queue is moved its position into a guy behind him.” q.Enqueue(5) Front-> Rear-> 5 2 3

Let us look at example We used twice q.Dequeue command. q.Dequeue(2) 2 0 Front-> 3 1 Rear-> q.Dequeue(3) Front Rear -> 5 2 3

Let us look at example And then q.Enqueue command q.Enqueue(10) Front-> 5 2 Rear-> 10 3

Let us look at example Suddenly!!!!!! What to do??? It’s already full. q.Enqueue(20)?? Front-> 5 2 Rear-> 10 3

Let us look at example Stay calm. Let us “wrap the queue” by convert it into “ring queue” Front-> 5 2 Rear-> FRONT REAR

Let us look at example Since we only have four(4) space in ring queue. It will put the new data in circle.

Let us look at example We already have four people queue up (full). -Ahmad (First), Samad (Second), Ali (Third), and Abu (Fourth) -FRONT is Ahmad, REAR is Abu

Let us look at example FRONTREAR AHMAD SAMAD ALI ABU THEN SOMEONE COMING!!! HIS NAME IS OPTIMUS PRIME

Let us look at example AHMAD SAMAD ALI ABU FRONT REAR THEN SOMEONE COMING!!! HIS NAME IS OPTIMUS PRIME

Let us look at example OPTIMUS PRIME SAMAD ALI ABU FRONT REAR OPTIMUS PRIME WILL BE PUT AT “0”!! And REAR also change!!

Back to example… Then, FRONT REAR q.Enqueue(20) Rear-> Front->

Back to example… In coding style if (rear == maxQueue – 1) // Meaning at location “3” rear = 0; //put it back at location “0” else rear = rear + 1; // else put after the last one //we also can use rear = (rear + 1) % maxQueue

What if the space has been occupied (full)? Case 1 q.Enqueue(30) Rear-> Front->

Queue is empty now!! rear == front

QUEUE 0 0 A [0][1][2][3][99][98][97] front rear addQueue A

QUEUE 0 2 ABC [0][1][2][3][99][98][97] front rear addQueue B, C

QUEUE 1 2 ABC [0][1][2][3][99][98][97] front rear A deqElement deQueue

QUEUE Circular Queue [99][0]

QUEUE XY [0][1][2][3][99][98][97] front rear addQueue Zrear=(rear+1)%maxQueue

QUEUE 98 0 XY [0][1][2][3][99][98][97] front rear Z

How to program it in C++? First, start your Visual C++ or XCode C++

How to program it in C++? First, start your Visual C++ or XCode C++

Let’s Code As usual, we declare maximum content in queue

Let’s Code -Build a private inside class queue -Declare three variables -int t[MAX] for an array -int al for rear queue -int dl for front queue

Let’s Code As usual, a constructor to set up dl and al to -1.

Let’s Code -This function add item into queue 1.For starting, we check whether both dl and al is -1. If still empty, we set it to become 0 for both. 2.Then, we add data t[al] = item. 3.For example, if item = 60. Then t[0] = 60. because al = 0 and dl also equal to 0

Example add(50) For starting, the dl = al = -1 And we send add (60) 1.Thus command involves:- if(dl==-1 && al==-1) { dl++; al++; } 2. So, dl = al = 0 3. Next command:- t[al] = item 4. So, t[0] = 60 add(60) FRONT (dl) REAR (al) ->

Let’s Code -Otherwise if al and dl is not equal to -1 (meaning some data already inside the queue list, so 1.We update the al to al++. 2.If the queue list is full then we send message “Queue is Full” and set back al to al— 3.But, we still add t[al] = item and we replace the

Example add(50) For starting, the dl = 0, al = 2 And we send add (30) 1.Thus command involves:- al++; if(al==MAX) { cout<<"Queue is Full\n"; al--; return; } 2. So, al = 3. and check the MAX 3. Next command:- t[al] = item 4. So, t[3] = 30 Add(30) FRONT (dl) -> REAR (al) -> Add(30) FRONT (dl)-> REAR (al)->

Let’s Code -This function remove the queue

Let’s Code -This function remove the queue

Let’s Code -This function remove the queue

Let’s Code -This function remove the queue

Let’s Code

Queues vs. Stacks Stacks are a LIFO container Store data in the reverse of order received Queues are a FIFO container Store data in the order received Stacks then suggest applications where some sort of reversal or unwinding is desired. Queues suggest applications where service is to be rendered relative to order received. Stacks and Queues can be used in conjunction to compare different orderings of the same data set. From an ordering perspective, then, Queues are the “opposite” of stacks Easy solution to the palindrome problem

Example: recognizing palindromes A palindrome is a string that reads the same forward and backward. able was I ere I saw elba madam ere madam 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

Easy Palindrome Check #include “Stack.h”; //include class definition of Stack #include “Queue.h”; //include class definition of Queue #include using namespace std; int main(){ StackS; QueueQ; charch; cout << “Enter string to be tested as palindrome: “; while (cin >> ch){ S.push(ch); Q.addq(ch); } bool pal = true; while (!Q.empty() && pal) { pal = Q.front() == S.pop(); Q.removeq(); } if (pal) cout << “Palindrome!!” << endl; return0; }

The end…