Presentation is loading. Please wait.

Presentation is loading. Please wait.

Monday, February 26, 2018 Announcements… For Today…

Similar presentations


Presentation on theme: "Monday, February 26, 2018 Announcements… For Today…"— Presentation transcript:

1 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 s to Please include netID when reporting a problem! For Today… 6.1 Queues

2 Attendance Quiz #20 Queues / Deques

3 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.

4 Lab 05 – Expressions

5 Requirements Input is an expression string in infix notation.
Queues / Deques Input is an expression string in infix notation. Expression: * 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.

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

7 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

8 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

9 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

10 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.

11 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

12 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

13 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.

14 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.

15 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>.

16 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)

17 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

18 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

19 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?

20 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; }

21 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.

22


Download ppt "Monday, February 26, 2018 Announcements… For Today…"

Similar presentations


Ads by Google