200 likes | 330 Views
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 .
E N D
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. CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee
Today’s Topics • ADTs/Containers: what are they? • Stack • Compare Stack and Vector implementations of the same code • [Maybe Monday] Stack example: Reverse Polish Notation evaluator
ADTs • 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 vocabularyof nouns and verbs, often drawing on analogies to make their use intuitive, and to give code written in them a certain literary quality
"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.
Some ADTs implemented in the Stanford Libraries • Vector • Grid • Graph • Map (and HashMap) • Set (and HashSet) • PriorityQueue • Queue • Stack
ADT: Grid queensafety.cpp static void clearBoard(Grid<bool>& board); int main(){ Grid<bool> board(8,8); clearBoard(board); //not strictly necessary // …more code to come… return 0; } static void clearBoard(Grid<bool>& board){ for (inti=0; i<board.numRows(); i++){ for (int j=0; j<board.numCols(); j++){ board[i][j] = false; } } }
New ADT: Stack stack.h template <typenameValueType> class Stack { public: Stack(); virtual ~Stack(); int size() const; boolisEmpty() const; void clear(); void push(ValueType value); ValueType pop(); ValueType peek() const; std::string toString(); private: }; -Redacted-
New ADT: Stack stack.h template <typenameValueType> class Stack { public: Stack(); virtual ~Stack(); int size() const; boolisEmpty() const; void clear(); void push(ValueType value); ValueType pop(); ValueType peek() const; std::string toString(); private: }; -Redacted- This image was released by the National Cancer Institute, an agency part of the National Institutes of Health, with the ID 8368
Using a Stack to buffer file input What does this code do? • Prints all lines of a file to cout • Prints only the first line of a file to cout • Prints only the last line of a file to cout • Prints all lines of a file to cout in reverse • All/ none/ more than one of the above 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; } }
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? • Prints all lines of a file to cout • Prints only the first line of a file to cout • Prints only the last line of a file to cout • Prints all lines of a file to cout in reverse • All/ none/ more than one of the above Why do I need Stack? I could have done that with a Vector!
Stack or Vector? Vector<string> lines; 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; } } lines.insert(lines.size(), line); cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1);
Vector version void mystery2(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); } }
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 • 1 • 2 • 3 • 4 • More than 4
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: • ((4*3) + (7*2)) + 5 • (4*3) + ((7+2) + 5) • (4*3) + (7 + (2*5)) • 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 * + + Contents of the stack, reading from top down: • 7, 12 • 2, 7, 12 • 10, 7,12 • 10, 5, 2, 7, 12 • 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*++