Programming Abstractions

Slides:



Advertisements
Similar presentations
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Advertisements

Queues CS 308 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: –Elements are added at.
E.G.M. Petrakislists, stacks, queues1 Stacks Stack: restricted variant of list –Elements may by inserted or deleted from only one end  LIFO lists –Top:
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
 Balancing Symbols 3. Applications
Doubly-Linked Lists Same basic functions operate on list Each node has a forward and backward link: What advantages does a doubly-linked list offer? 88.
Topic 15 Implementing and Using Stacks
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
TCSS 342, Winter 2005 Lecture Notes
Topic 15 Implementing and Using Stacks
Class 4: Queues. cis 335 Fall 2001 Barry Cohen What is a queue? n A stack is an ordered sequence of items. n As in lists and stacks, each node contains.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Stack and Queue.
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
Main Index Contents 11 Main Index Contents Stacks Further Stack Examples Further Stack Examples Pushing/Popping a Stack Pushing/Popping a Stack Class StackClass.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
ADT Stack & Queue - Case Studies TCP1201: 2013/2014.
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS 106X – Programming Abstractions in C++
Programming Abstractions
Stacks and Queues.
Revised based on textbook author’s notes.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stack as an ADT.
Homework 4 questions???.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Programming Abstractions
Cinda Heeren / Geoffrey Tien
Stacks Stack: restricted variant of list
Stacks and Queues.
Monday, February 26, 2018 Announcements… For Today…
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Programming Abstractions
Building Java Programs Chapter 14
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Lecture 21 Stacks and Queues Richard Gesick.
Programming Abstractions
Stacks and Queues.
Stacks, Queues, and Deques
Lab 05 – Expressions.
Building Java Programs
Programming Abstractions
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Stacks and Queues 1.
slides created by Marty Stepp
Container classes, ADTs
Topic 15 Implementing and Using Stacks
Jordi Cortadella and Jordi Petit Department of Computer Science
Programming Abstractions in C++, Chapter
Stacks and Queues.
Data Structures & Programming
Presentation transcript:

Programming Abstractions CS106B Cynthia Lee

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

Stacks

New ADT: Stack stack.h -Redacted- 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: }; Source: http://www.flickr.com/photos/35237093334@N01/409465578/ Author: http://www.flickr.com/people/35237093334@N01 Peter Kazanjy] -Redacted- -- Note destructor and recurring “ValueType” parameter

Using a Stack Stack<int> s; // {} bottom -> top s.push(42); // {42} s.push(-3); // {42, -3} s.push(17); // {42, -3, 17} cout << s.pop() << endl; // 17 (s is {42, -3}) cout << s.peek() << endl; // -3 (s is {42, -3}) cout << s.pop() << endl; // -3 (s is {42}) Remember this format—we’ll use it in section examples and on exams

Using a Stack to buffer (i.e., temporarily hold) 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; Reading from a file basics: ifstream is an input stream (like cin) but from a file getline takes the file and a string by reference, and reads one line into string Returns false when there are no more lines to read What does this code do?

Using a Stack to buffer (i.e., temporarily hold) 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; Reading from a file basics: ifstream is an input stream (like cin) but from a file getline takes the file and a string by reference, and reads one line into string Returns false when there are no more lines to read What does this code do? 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; Vector<string> lines; lines.insert(lines.size(), line); cout << lines[lines.size()-1] << endl; lines.remove(lines.size()-1);

Vector version 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”. 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);

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 9+9=18 3*(3+9)=36 3*6*3=3*18=54

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: ((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 (A) 7, 12 (B) 10, 7,12 (C) 10, 5, 2, 7, 12 (D) Other 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: (A) 7, 12 (B) 10, 7,12 (C) 10, 5, 2, 7, 12 (D) Other * * 4 3 4 12 7 12 2 7 12 5 2 7 12 ?

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 What are they? Example application

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

New ADT: Queue queue.h -Redacted- template <typename ValueType> class Queue { public: Queue(); virtual ~Queue(); int size() const; bool isEmpty() const; void clear(); void enqueue(ValueType value); ValueType dequeue(); ValueType& back(); ValueType& front(); ValueType peek() const; std::string toString(); private: -- Note destructor and recurring “ValueType” parameter -Redacted-

Using a Queue Queue<int> q; // {} front -> back q.enqueue(42); // {42} q.enqueue(-3); // {42, -3} q.enqueue(17); // {42, -3, 17} cout << q.dequeue() << endl; // 42 (q is {-3, 17}) cout << q.peek() << endl; // -3 (q is {-3, 17}) cout << q.dequeue() << endl; // -3 (q is {17}) Remember this format—we’ll use it in section examples and on exams

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 Queue: accessed at both ends Get in line (enqueue) Push Next to be served (dequeue) Pop

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 Queue: accessed at both ends

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