Monday, February 26, 2018 Announcements… For Today…

Slides:



Advertisements
Similar presentations
Data Structures Through C
Advertisements

Chapter 6 Queues and Deques.
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
Queues Chapter 3. Objectives Introduce the queue abstract data type. – Queue methods – FIFO structures Discuss inheritance in object oriented programming.
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.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
Chapter 1.2 Introduction to C++ Programming
Stack ADT (Abstract Data Type) N …
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
Chapter 1.2 Introduction to C++ Programming
CSCE 210 Data Structures and Algorithms
Data Structures Using C++ 2E
CS505 Data Structures and Algorithms
Queue ADT (Abstract Data Type) N …
Chapter 3 Control Statements
Chapter 4 The easy stuff.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
Stacks Stacks.
Revised based on textbook author’s notes.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 2 Assignment and Interactive Input
Homework 4 questions???.
Objectives In this lesson, you will learn to: Define stacks
Stacks and Queues.
Queues Queues Queues.
Queues Chapter 8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
Wednesday, February 28, 2018 Announcements… For Today…
Tuesday, February 20, 2018 Announcements… For Today… 4+ For Next Time…
CMSC 341 Lecture 5 Stacks, Queues
THURSDAY, OCTOBER 17 IN LAB
Lab 05 – Expressions.
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.
CS150 Introduction to Computer Science 1
Stacks Data structure Elements added, removed from one end only
Introduction to Data Structure
Chapter 4 Queues.
Jordi Cortadella and Jordi Petit Department of Computer Science
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Lab4 problems More about templates Some STL
Using a Queue Chapter 8 introduces the queue data type.
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
Queues.
DATA STRUCTURES IN PYTHON
Chapter 6 – Queues and Deques
Data Structures & Programming
Presentation transcript:

Monday, February 26, 2018 Announcements… For Today… Queues / Deques Announcements… Exam I – Today thru Tuesday (02/27) Chapters P thru 4 Closed book. No notes. No time limit. Lab 04 Linked List Peer Reviews ready. Lab 05 Expressions ready. Zipped file contains ONLY .cpp, .h files Direct emails to cs235ta@cs.byu.edu Please include netID when reporting a problem! For Today… 6.1 Queues

Attendance Quiz #20 Queues / Deques

Tip #21: Constness Queues / Deques What does it mean for a member function to be const? Bitwise constness (also known as physical constness). A member function is const if and only if it doesn't modify any of the object's data members (excluding static members.) Easy for compiler to detect violations: just look for assignments to data members. C++ definition of constness. Logical constness. A member function that modifies what a pointer points to is bitwise constness but not logical constness. The solution is simple: take advantage of C++'s const-related wiggle room know as mutable. Mutable frees non-static data members from the constraints of bitwise constness. Adding mutable to a variable allows a const pointer to change members.

Lab 05 – Expressions

Requirements Input is an expression string in infix notation. Queues / Deques Input is an expression string in infix notation. Expression: 43 + 2 * 19 Use an ExpressionManager class to hold your infix, postfix, (and prefix) expressions. Your class should be derived from the abstract interface class ExpressionManagerInterface. Implement member functions: virtual int value(void); virtual string infix(void); virtual string postfix(void); virtual string prefix(void); Output the resulting infix, postfix, (prefix), and integer evaluation value of the expression. Your calculations should perform integer division and produce integer results.

Example Input / Output Input Output Expression: 43 + 2 * 19 Queues / Deques Input Expression: 43 + 2 * 19 Expression: 2 + 3 + 5 . . . Output Expression: 43 + 2 * 19 Infix: 43 + 2 * 19 Postfix: 43 2 19 * + Prefix: + 43 * 2 19 Value: 81 Expression: 2 + 3 + 5 Infix: 2 + 3 + 5 Postfix: 2 3 + 5 + Prefix: + + 2 3 5 Value: 10 . . .

Inherits from ExpressionManagerInterface Queues / Deques class ExpressionManager : public ExpressionManagerInterface { private: string expression_; std::vector<string> inFix_; std::vector<string> postFix_; std::vector<string> preFix_; string operators = "([{ -+ */% "; public: ExpressionManager() { } ExpressionManager(string exp) : expression_(exp) { } ~ExpressionManager() { } virtual int value(void); /** Return the integer value of the infix expression */ virtual string infix(void); /** Return the infix expression / rejects invald */ virtual string postfix(void); /** Return a postfix representation */ virtual string prefix(void); /** (BONUS) Return a prefix representation */ virtual string toString() const; /** Return the infix vector'd expression items */ }; #endif // EXPRESSION_MANAGER_H Inherits from ExpressionManagerInterface

Chapter 6 Objectives Queues / Deques To learn how to represent a waiting line (queue) with a Queue ADT and functions for insertion (push), removal (pop), and for accessing the element at the front (front). To understand how to implement the Queue ADT using a single-linked list, a circular array, and a double-linked list. To understand how to simulate the operation of a physical system that has one or more waiting lines using queues and random number generators. To introduce the standard library Deque class. The queue, like the stack, is a widely used data structure, but differs from a stack in one important way a stack is LIFO list – Last-In, First-Out while a queue is FIFO list – First-In, First-Out

6.1, pgs. 358-362 6.1 The Queue Abstract Data Type A Queue of Customers A Print Queue The Unsuitability of a ``Print Stack'' Specification of the Queue ADT 6.1, pgs. 358-362

Queue Abstract Data Type Queues / Deques The queue, like the stack, is a widely used data structure, but differs from a stack in one important way a stack is LIFO list – Last-In, First-Out while a queue is FIFO list – First-In, First-Out A queue can be visualized as a line of customers waiting for service The next person to be served is the one who has waited the longest. New elements are placed at the end of the line.

A Queue of Customers Thome Abreu Jones Queues / Deques To the left is a queue of three customers waiting to buy concert tickets Ticket agent Thome has been waiting the longest Thome Abreu Jones Jones is the most recent arrival Thome will be the first customer removed from the queue (and able to buy tickets) when a ticket agent becomes available

A Queue of Customers Abreu Jones Ticket agent Queues / Deques Ticket agent Abreu will then become the first one in the queue Abreu Jones Any new customers will be inserted in the queue after Jones

Print Queue Operating systems use queues to Queues / Deques Operating systems use queues to keep track of tasks waiting for a scarce resource ensure that the tasks are carried out in the order they were generated. Print queue: printing typically is much slower than the process of selecting pages to print, so a queue is used.

Why Not a “Print Stack”? Stacks are Last-In, First-Out (LIFO). Queues / Deques Stacks are Last-In, First-Out (LIFO). The most recently selected document would be the next to print. Unless the print stack is empty, your print job may never be executed if the printer is connected to a computer network and others are issuing print jobs.

Specification for a Queue Interface Queues / Deques Because only the front element of a queue is visible, the operations performed by a queue are few in number. We need to be able to retrieve the front element, remove the front element, push a new element onto the queue, and test for an empty queue. The functions above are all defined in the header file for the STL container queue, <queue>.

Specification for a Queue Interface Queues / Deques For queue names in (a), the value of names.empty() is false. string first = names.front(); stores "Jonathan" in first without changing names names.pop(); removes "Jonathan" from names. The queue names now contains four elements and is shown in (b) names.push("Eliana"); adds "Eliana" to the end of the queue; the queue names now contains five elements and is shown in (c)

6.2, pgs. 362-365 6.2 Maintaining a Queue of Customers Case Study: Maintaining a Queue Exercises for Section 6.2 6.2, pgs. 362-365

Maintaining a Queue of Customers Queues / Deques Problem Write a menu-driven program Maintain_Queue that maintains a list of customers The user should be able to: insert a new customer in line display the customer who is next in line remove the customer who is next in line display the length of the line Analysis Inputs Operation selection New customer entry Outputs The effect of each operation

Maintaining a Queue of Customers Queues / Deques Design Maintain_Queue uses a queue<string> for customers. Algorithm for main While the user is not finished Display the menu and get the selected operation. Perform the selected operation. Use customers.push(name) to enter new customer into queue. Use customers.front() to retrieve/display who is next in line Use customers.pop() to remove next in line Use customers.size() to output how many are in line Implementation Use switch to select operation. Use enum? const?

Implementation Queues / Deques #include <iostream> #include <queue> #include <string> using namespace std; const string choices[] = { "push", "front", "pop", "size", "quit" }; const int num_choices = 5; int main() { queue<string> customers; string name; int choice_num = 0; do cout << endl << "Options:" << endl; for (int i = 0; i < num_choices; i++) cout << " " << i << ": "; cout << choices[i] << endl; cout << "Select option: "; cin >> choice_num; ---------------------------------------- } while (choice_num != 4); return 0; } switch (choice_num) { case 0: cout << "Enter new customer name: "; cin >> name; customers.push(name); break; case 1: cout << "Customer " << customers.front(); cout << " is next in line" << endl; case 2: cout << " removed from the line" << endl; customers.pop(); case 3: cout << "Size of line is "; cout << customers.size() << endl; case 4: cout << "Leaving customer queue" << endl; default: cout << "Invalid selection" << endl; }

Maintaining a Queue of Customers Queues / Deques Testing Normal operation: Verify that all customers are stored and retrieved in FIFO order. Thoroughly test the queue by selecting different sequences of queue operations. Error conditions: Select "0xxx". Select "quit". Customer "John Doe". Flush cin buffer.