Download presentation
Presentation is loading. Please wait.
Published byWinfred Palmer Modified over 8 years ago
1
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org
2
Today’s Topics 1. ADTs/Containers: what are they? 2. Stack 3. Compare Stack and Vector implementations of the same code 4. [Maybe Monday] Stack example: Reverse Polish Notation evaluator 2
3
ADTs
4
Programming language independent models of common containers They encompass not only the nature of the data, but ways of accessing it They form a rich vocabulary of nouns and verbs, often drawing on analogies to make their use intuitive, and to give code written in them a certain literary quality
5
"Hope" is the thing with feathers BY EMILY DICKENSON “Hope” is the thing with feathers - That perches in the soul - And sings the tune without the words - And never stops - at all - And sweetest - in the Gale - is heard - And sore must be the storm - That could abash the little Bird That kept so many warm - I’ve heard it in the chillest land - And on the strangest Sea - Yet - never - in Extremity, It asked a crumb - of me.
6
Some ADTs implemented in the Stanford Libraries Vector Grid Graph Map (and HashMap) Set (and HashSet) PriorityQueue Queue Stack
7
ADT: Grid static void clearBoard(Grid & board); int main(){ Grid board(8,8); clearBoard(board); //not strictly necessary // …more code to come… return 0; } static void clearBoard(Grid & board){ for (int i=0; i<board.numRows(); i++){ for (int j=0; j<board.numCols(); j++){ board[i][j] = false; } queensafety.cpp
8
Stacks
9
New ADT: Stack template 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: }; stack.h -Redacted-
10
New ADT: Stack template 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: }; stack.h -Redacted- This image was released by the National Cancer Institute, an agency part of the National Institutes of Health, with the ID 8368National Cancer InstituteNational Institutes of Health8368
11
Using a Stack to buffer file input void mystery(ifstream& infile) { Stack 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 all lines of a file to cout B. Prints only the first line of a file to cout C. Prints only the last line of a file to cout D. Prints all lines of a file to cout in reverse E. All/ none/ more than one of the above
12
void mystery(ifstream& infile) { Stack lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isEmpty()) { cout << lines.pop() << endl; } Using a Stack to buffer file input What does this code do? A. Prints all lines of a file to cout B. Prints only the first line of a file to cout C. Prints only the last line of a file to cout D. Prints all lines of a file to cout in reverse E. All/ none/ more than one of the above Why do I need Stack? I could have done that with a Vector!
13
Stack or Vector? Vector lines; lines.insert(lines.size(), line); cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1); void mystery(ifstream& infile) { Stack lines; string line; while (getline(infile,line)) { lines.push(line); } infile.close(); while (!lines.isEmpty()) { cout << lines.pop() << endl; }
14
Vector version void mystery2(ifstream& infile) { Vector 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); }
15
Applications of Stacks We’ve seen one (buffering input and giving it back in reverse—LIFO—order). What else are Stacks good for?
16
Operator Precedence and Syntax Trees Ignoring operator precedence rules, how many distinct results are there to the following arithmetic expression? 3 * 3 + 3 * 3 A. 1 B. 2 C. 3 D. 4 E. More than 4
17
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
18
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
19
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 2 7 * ? ? ? ? ? ? Contents of the stack, reading from top down: A. 7, 12 B. 2, 7, 12 C. 10, 7,12 D. 10, 5, 2, 7, 12 E. Other 5 2 7 12
20
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*++
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.