1 / 20

CS 106X – Programming Abstractions in C++

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 .

king
Download Presentation

CS 106X – Programming Abstractions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. ADTs

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

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

  8. Stacks

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

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

  11. 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; } }

  12. 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!

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

  14. 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); } }

  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 • 1 • 2 • 3 • 4 • 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: • ((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

  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 * + + Contents of the stack, reading from top down: • 7, 12 • 2, 7, 12 • 10, 7,12 • 10, 5, 2, 7, 12 • Other * *

  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*++

More Related