Prefix notation in action

Slides:



Advertisements
Similar presentations
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Chapter 6 Linked Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
CPS 100, Spring Spirits of Linear Structures l Past, Present, Future, … l Linked-lists and arrays and ArrayLists  ADT: insert, delete, iterate/traverse,
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.
Chapter 3 Stacks.
Stacks Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
CS102 – Data Structures Lists, Stacks, Queues, Trees, Hash Collections Framework David Davenport Spring 2002.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
The Stack and Queue Types Lecture 10 Hartmut Kaiser
CPS 100, Spring Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete,
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.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
ISOM MIS 215 Module 3 – Stacks and Queues. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
CPS Data Structures revisited l Linked lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete, traverse,
CPS Sequential search revisited l What does the code below do? How would it be called initially?  Another overloaded function search with 2 parameters?
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
CompSci 100E 9.1 Stack: What problems does it solve?  Stacks are used to avoid recursion, a stack can replace the implicit/actual stack of functions called.
CPS 100, Fall Catching Up, Moving ahead l Last week and this week  How did you study for the exam? Next time?  How can you/should you work with.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
CompSci 100e Program Design and Analysis II March 15, 2011 Prof. Rodger CompSci 100e, Spring20111.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
CSCI 62 Data Structures Dr. Joshua Stough October 7, 2008.
CPS Review of Data Structures l We’ve studied concrete data structures/type (CDT)  Vectors Homogeneous aggregates supporting random access  Linked.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CPS Recurrences l Summing Numbers int sum(int n) { if (0 == n) return 0; else return n + sum(n-1); } l What is complexity? justification? l T(n)
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
CPS 100, Fall Linear Structures Revisited l Linked-lists and arrays and ArrayLists and …  Linear structures, operations include insert, delete,
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.
G64ADS Advanced Data Structures
Double-Ended Queues Chapter 5.
Stacks Chapter 5.
Stack: What problems does it solve?
Marcus Biel, Software Craftsman
Chapter 15 Lists Objectives
MEMORY REPRESENTATION OF STACKS
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Objectives In this lesson, you will learn to: Define stacks
CSE 373: Data Structures and Algorithms
Introduction to Data Structures
Stacks and Queues.
structures and their relationships." - Linus Torvalds
structures and their relationships." - Linus Torvalds
Stacks and Queues.
Stacks, Queues, and Deques
Final Exam Review COP4530.
CSC 143 Queues [Chapter 7].
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.
Data Structures and Algorithms for Information Processing
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Topic 15 Implementing and Using Stacks
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
structures and their relationships." - Linus Torvalds
Chapter 20 Lists, Stacks, Queues, and Priority Queues
Stacks and Queues.
Lecture 9: Stack and Queue
Presentation transcript:

Prefix notation in action Scheme/LISP and other functional languages tend to use a prefix notation (define (square x) (* x x)) (define (expt b n) (if (= n 0) 1 (* b (expt b (- n 1)))))

Postfix notation in action Practical example of use of stack abstraction Put operator after operands in expression Use stack to evaluate operand: push onto stack operator: pop operands push result PostScript is a stack language mostly used for printing drawing an “X” with two equivalent sets of code %! 200 200 moveto 100 100 rlineto 200 300 moveto 100 –100 rlineto stroke showpage %! 100 –100 200 300 100 100 200 200 moveto rlineto moveto rlineto stroke showpage

Postfix notation in action For arithmetic expressions, is there more than one Postfix representation? A + B – C / D * E A * B * C / D * E

Parentheses Matching Problem How can we use a stack to check the syntactic correctness of expressions involving parentheses? if (msg.equals(txt.substring(3, n – 2 + txt.length())) Is there a simple solution not using stacks? What about including braces, brackets, angle-brackets, etc.? x = m[k] + w[msg.indexOf(s[n]]);

Queue: another linear ADT FIFO: first in, first out, used in many applications Scheduling jobs/processes on a computer Tenting policy? Computer simulations Common operations Add to back, remove from front, peek at front No standard java.util.Queue, instead java.util.LinkedList addLast(), getFirst(), removeFirst, size() Can use add() rather than addLast(); Downside of using LinkedList as queue Can access middle elements, remove last, etc. why?

Stack and Queue implementations Different implementations of queue (and stack) aren’t really interesting from an algorithmic standpoint Complexity is the same, performance may change (why?) Use ArrayList, growable array, Vector, linked list, … Any sequential structure As we'll see java.util.LinkedList is good basis for all In Java 5, LinkedList implements the new Queue interface ArrayList for queue is tricky, ring buffer implementation, add but wrap-around if possible before growing Tricky to get right (exercise left to reader)

Using linear data structures We’ve studied arrays, stacks, queues, which to use? It depends on the application ArrayList is multipurpose, why not always use it? Make it clear to programmer what’s being done Other reasons? Other linear ADTs exist List: add-to-front, add-to-back, insert anywhere, iterate Alternative: create, head, tail, Lisp or Linked-list nodes are concrete implementation Deque: add-to-front, add-to-back, random access Why is this “better” than an ArrayList? How to implement?