Prefix notation in action v SchemeLISP and other

  • Slides: 7
Download presentation
Prefix notation in action v Scheme/LISP and other functional languages tend to use a

Prefix notation in action v 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))))) Comp. Sci 100 E 11. 1

Postfix notation in action v v Practical example of use of stack abstraction Put

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

Postfix notation in action v For arithmetic expressions, is there more than one Postfix

Postfix notation in action v For arithmetic expressions, is there more than one Postfix representation? A + B – C / D * E A * B * C / D * E Comp. Sci 100 E 11. 3

Parentheses Matching Problem v How can we use a stack to check the syntactic

Parentheses Matching Problem v 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())) q v Is there a simple solution not using stacks? What about including braces, brackets, angle-brackets, etc. ? x = Comp. Sci 100 E m[k] + w[msg. index. Of(s[n]]); 11. 4

Queue: another linear ADT v FIFO: first in, first out, used in many applications

Queue: another linear ADT v FIFO: first in, first out, used in many applications q q q v Common operations q v Scheduling jobs/processes on a computer Tenting policy? Computer simulations Add to back, remove from front, peek at front o No standard java. util. Queue, instead java. util. Linked. List o add. Last(), get. First(), remove. First, size() o Can use add() rather than add. Last(); Downside of using Linked. List as queue q Can access middle elements, remove last, etc. why? Comp. Sci 100 E 11. 5

Stack and Queue implementations v Different implementations of queue (and stack) aren’t really interesting

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

Using linear data structures v We’ve studied arrays, stacks, queues, which to use? q

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