Programming Abstractions

Similar documents
Programming Abstractions

Programming Abstractions

Programming Abstractions

STACKS AND QUEUES. Problem Solving with Computers-II

The Stack and Queue Types

CS 106B Lecture 5: Stacks and Queues

Programming Abstraction in C++

Programming Abstractions

CS 206 Introduction to Computer Science II

Programming Abstractions

CS W3134: Data Structures in Java

Stacks and Queues 2017 年 4 月 27 日星期四. Thanks for CS106B of Stanford University. reading: Programming Abstractions in C++, Chapter

Postfix (and prefix) notation

List, Stack, and Queues

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS 206 Introduction to Computer Science II

Stack Primer. Simple Example. CS106X Handout 06 Winter 2018 January 10 th, 2018 Stacks and Queues. Handout written by Jerry Cain.

Containers: Stack. Jordi Cortadella and Jordi Petit Department of Computer Science

12 Abstract Data Types

15. Stacks and Queues

Containers: Stack. The Stack ADT. The Stack ADT. The Stack ADT

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Abstract Data Types 1

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

Problem with Scanning an Infix Expression

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Review: Expressions, Variables, Loops, and more.

IV. Stacks. A. Introduction 1. Consider the 4 problems on pp (1) Model the discard pile in a card game. (2) Model a railroad switching yard

CS 171: Introduction to Computer Science II. Stacks. Li Xiong

Abstract Data Types 1

Stacks Fall 2018 Margaret Reid-Miller

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

ADTs: Stacks and Queues

Data Structures and Algorithms

Abstract Data Types. Stack. January 26, 2018 Cinda Heeren / Geoffrey Tien 1

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Lecture Data Structure Stack

Stack and Its Implementation

CSE 332: Data Structures. Spring 2016 Richard Anderson Lecture 1

CSC 222: Computer Programming II. Spring 2004

CSCI-1200 Computer Science II Fall 2008 Lecture 15 Associative Containers (Maps), Part 2

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

// The next 4 functions return true on success, false on failure

Loop Invariants. while!done do // what is true at every step // Update/iterate // maintain invariant od CPS

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Queues Fall 2018 Margaret Reid-Miller

Problem with Scanning an Infix Expression

Wentworth Institute of Technology COMP201 Computer Science II Spring 2015 Derbinsky. Stacks and Queues. Lecture 11.

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSE 326 Team. Today s Outline. Course Information. Instructor: Steve Seitz. Winter Lecture 1. Introductions. Web page:

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

CS 211 Programming Practicum Fall 2018

Stack Abstract Data Type

Stacks and their Applications

Stacks, Queues and Hierarchical Collections

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

March 13/2003 Jayakanth Srinivasan,

Stacks and Queues. David Greenstein Monta Vista

CSC148 Week 2. Larry Zhang

Stacks and Queues. Stack - Abstract Data Type. Stack Applications. Stack - Abstract Data Type - C Interface

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

! Set of operations (add, remove, test if empty) on generic data. ! Intent is clear when we insert. ! Which item do we remove? Stack.

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Stacks, Queues (cont d)

Arrays and Linked Lists

Name CPTR246 Spring '17 (100 total points) Exam 3

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Stacks and Queues

Advanced Java Concepts Unit 3: Stacks and Queues

CSC148H Week 3. Sadia Sharmin. May 24, /20

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

CS24 Week 4 Lecture 2

Project 1: Implementation of the Stack ADT and Its Application

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

[CS302-Data Structures] Homework 2: Stacks

DEEPIKA KAMBOJ UNIT 2. What is Stack?

4.3 Stacks and Queues

Lists, Stacks, and Queues. (Lists, Stacks, and Queues ) Data Structures and Programming Spring / 50

EEE2020 Data Structures and Algorithms Abstract Data Types: Stacks and Queues

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Abstract Data Types. Abstract Data Types

Lecture 12 ADTs and Stacks

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Lecture 4 Stack and Queue

CSCE 121 ENGR 112 List of Topics for Exam 1

ABSTRACT DATA TYPES (ADTS) COMP1927 Computing 2 16x1 Sedgewick Chapter 4

CSCI-1200 Data Structures Fall 2016 Lecture 17 Associative Containers (Maps), Part 2

Plan for Week. What is a linear structure? Stacks Queues and Deques, Oh My!

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Trees. Carlos Moreno uwaterloo.ca EIT

Queues and Unit Testing

Types of Data Structures

Transcription:

Programming Abstractions C S 1 0 6 X Cynthia Lee

Today s Topics ADTs Stack Example: Reverse-Polish Notation calculator Queue Example: Mouse Events

Stacks

New ADT: Stack stack.h template <typename ValueType> class Stack { public: Stack(); virtual ~Stack(); int size() const; bool isempty() const; void clear(); void push(valuetype value); ValueType pop(); ValueType peek() const; std::string tostring(); private: -Redacted- Source: http://www.flickr.com/photos/35237093334@n01/409465578/ Author: http://www.flickr.com/people/35237093334@n01 Peter Kazanjy] };

Using a Stack to buffer file input void mystery(ifstream& infile) { Stack<string> lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isempty()) { cout << lines.pop() << endl; } } What does this code do? A. Prints a file s contents to cout B. Prints only the first line of a file to cout C. Prints out a file to cout in reverse D. All/ none/ more

Using a Stack to buffer file input void mystery(ifstream& infile) { What does this code do? Stack<string> lines; A. Prints a file s contents to cout string line; while (getline(infile,line)) { B. Prints only the first line of a file to cout lines.push(line); C. Prints out a file to cout in } Why do I need Stack? reverse I could have done that with a Vector! infile.close(); D. All/ none/ more while (!lines.isempty()) { } } cout << lines.pop() << endl;

Stack or Vector? void mystery(ifstream& infile) { } Stack<string> lines; string line; while (getline(infile,line)) { } lines.push(line); infile.close(); while (!lines.isempty()) { } cout << lines.pop() << endl; Vector<string> lines; lines.insert(lines.size(), line); cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1);

Vector version void mystery(ifstream& infile) { Vector<string> lines; string line; while (getline(infile,line)) { lines.insert(lines.size(),line); } infile.close(); while (!lines.isempty()) { cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1); } } This code isn t terrible, but it is harder to read quickly, and is probably more error prone. For example, it would be easy to forget the -1 when you remove lines.size()-1.

Applications of Stacks We ve seen one (buffering input and giving it back in reverse LIFO order). What else are Stacks good for?

Operator Precedence and Syntax Trees Ignoring operator precedence rules, how many distinct results are there to the following arithmetic expression? 3 * 3 + 3 * 3

Reverse Polish Notation Ambiguities don t exist in RPN Also called postfix because the operator goes after the operands Postfix (RPN): 4 3 * 4 3 * + Equivalent Infix: (4*3) + (4*3) http://commons.wikimedia.org/wiki/file:hewlett-packard_48gx_scientific_graphing_calculator.jpg

Reverse Polish Notation This postfix expression: 4 3 * 7 2 5 * + + Is equivalent to this infix expression: A. ((4*3) + (7*2)) + 5 B. (4*3) + ((7+2) + 5) C. (4*3) + (7 + (2*5)) D. Other/none/more than one http://commons.wikimedia.org/wiki/file:hewlett-packard_48gx_scientific_graphing_calculator.jpg

Stacks and RPN Evaluate this expression with the help of a stack Encounter a number: PUSH it Encounter an operator: POP two numbers and PUSH result 4 3 * 7 2 5 * + + 4 3 4 * 12 7 12 2 7 12 5 2 7 12 *?????? Contents of the stack, reading from top down: (A) 7, 12 (C) 10, 7,12 (D) 10, 5, 2, 7, 12 (E) Other

Stacks and RPN: What does that look like in code? Evaluate this expression with the help of a stack Encounter a number: PUSH it Encounter an operator: POP two numbers and PUSH result 43*725*++

Application of Stacks W E V E S E E N O N E ( B U F F E R I N G I N P U T A N D G I V I N G I T B A C K I N REVERSE L I F O O R D E R ). W H A T E L S E A R E S T A C K S G O O D F O R?

Operator Precedence Ignoring operator precedence rules, how many distinct results are there to the following arithmetic expression? 3 * 3 + 3 * 3 A pain to have to learn those rules Or use parentheses to disambiguate

Reverse Polish Notation Ambiguities don t exist in RPN! Also called postfix notation because the operator goes after the operands Postfix (RPN): 4 3 * 3 + Equivalent Infix: (4*3) + (3) http://commons.wikimedia.org/wiki/file:hewlett-packard_48gx_scientific_graphing_calculator.jpg

Reverse Polish Notation This postfix expression: 4 3 * 7 2 5 * + + Is equivalent to this infix expression: A. ((4*3) + (7*2)) + 5 B. (4*3) + ((7+2) + 5) C. (4*3) + (7 + (2*5)) D. Other/none/more than one http://commons.wikimedia.org/wiki/file:hewlett-packard_48gx_scientific_graphing_calculator.jpg

Stacks and RPN Evaluate this expression with the help of a stack Encounter a number: PUSH it Encounter an operator: POP two numbers and PUSH result 4 3 * 7 2 5 * + + 4 3 4 * 12 7 12 2 7 12 5 2 7 12 *?????? Contents of the stack, reading from top down: (A) 7, 12 (B) 10, 7,12 (C) 10, 5, 2, 7, 12 (D) Other

Stacks and RPN: What does that look like in code? Evaluate this expression with the help of a stack Encounter a number: PUSH it Encounter an operator: POP two numbers and PUSH result 43*725*++

Queues W H A T A R E T H E Y? E X A M P L E A P P L I C A T I O N

22 Queues They work the same way a waiting in line (or, if you re in the UK, queuing) works. FIFO = First in, first out First come, first serve Waiting for Apple Watch

Where Stack and Queue are accessed This may seem obvious, but it s an important point for the behind-the-scenes code implementation of these data structures: Stack: only accessed on one end Push Queue: accessed at both ends Get in line (enqueue) Pop Next to be served (dequeue)

Event queues (used in Fauxtoshop) While your code executes, a separate part of the program is constantly running, and its only job is listening for events and recording them Mouse moved, mouse clicked, mouse dragged, etc Every once in a while, your code can call getnextevent() to see what has happened getnextevent() returns the events one at a time, in the same order they happened In other words, returns them in FIFO order When it is recording events, it is enqueuing events in an event QUEUE Very common use of the Queue ADT

Map W H A T A R E T H E Y? E X A M P L E A P P L I C A T I O N

Linear containers, AKA Sequential containers Array Vector Stack Queue The question Which is the [1 st /2 nd /3 rd ] element? is salient Note: even though we can t directly access 3 rd element with Stack/Queue, there is a 3 rd element there is an ordering

Associative containers Map Set Lexicon "the" "if" "of" "to" "down" "from" "by" "she" "in" "you" "why" "him" Cynthia" Marty" Jess" "867-5309" 555-0371" 497-3070" set map Not as concerned with order but with matching Set: associates keys with membership (yes or no) Map: associates keys with values (could be any type)

Map programming exercise Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston). Report all words that appeared in the book at least 10 times, in alphabetical order Allow the user to type a word and report how many times that word appeared in the book What would be a good design for this problem? A. Map<int, string> wordcounts; B. Map<string, Vector<string>> wordcounts; C. Map<string, int> wordcounts; D. Map<string, Vector<int>> wordcounts; E. Other/none/more 28

Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston). How can we record the count? A. wordcounts[word]+=word; B. wordcounts[word]+=1; C. wordcounts[word]++; D. B and C are good, but you need to first detect new (never seen before) words so you can start at zero before you start adding +1 E. Other/none/more Map<string,int> wordcounts; string word; infile >> word; while (!infile.fail()){ //record count here infile >> word; } 29

Write a program to count the number of occurrences of each unique word in a text file (e.g. Poker by Zora Neale Hurston). Report all words that appeared in the book at least 10 times, in alphabetical order cout << Most common words:" << endl; for (string word : wordcounts){ if (wordcounts[word] >= 10){ cout << word << "\t ; cout << wordcounts[word] << endl; } Does this code do the job? } A. YES B. NO C. YES except for the detail of printing in alphabetical order D. Other/none/more 30

QT Creator A F E W W A R N I N G S & T I P S

If your code doesn t compile and gives an error about unknown function Main() (note capital M): There is more than one reason this could arise, but the most common by far is that QT lost its ability to find the path to our Stanford library includes (which define a fake Main()), because you put your code in a directory/folder that is deeply nested on your drive. For example, C:\Documents and Settings\Documents\classes\Stanf ord\2015\summer\cs106b\assign ments\c++\assignment1\... You need to move the assignment directory closer to C:\ (or on mac, the root directory)

If your code doesn t compile and gives you multiple definition errors for all the functions you have: This can arise if you add new files to your code inside QT creator using its handy Add New feature. NOT HANDY! Do not use this feature. It breaks the.pro file.

If it s too late and you already used the Add New feature 1. QUIT: Close QT Creator 2. CLEAN UP BROKEN FILES: Delete the.pro and.pro.user files in your code directory 3. GRAB REPLACEMENT: Download/unzip the assignment bundle again, and get a fresh copy of the project file (the one that ends in.pro) and put it in place of the one you deleted. 4. ALL BETTER! Re-open QT Creator (it will make a new.pro.user)