SlideServe
Menu
Préférences
© SlideServe · v2026

CompSci 100e Program Design and Analysis II

ganit avatar
8 diapositives · 267 Views · Jul 18, 2014

CompSci 100e Program Design and Analysis II. March 15, 2011 Prof. Rodger. Announcements. APTs extended til 3/17 (Typing Job, Rat route hints) New APT set out today due March 22 Today Stacks and Queues Linear structures used in problem domains and algorithms.

CompSci 100e Program Design and Analysis II - slide 1
01 / 8CompSci 100e Program Design and Analysis II
1 / 17

Télécharger la présentation

CompSci 100e Program Design and Analysis II
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. CompSci 100eProgram Design and Analysis II March 15, 2011 Prof. Rodger CompSci 100e, Spring2011

  2. Announcements • APTs extended til 3/17 (Typing Job, Rat route hints) • New APT set out today due March 22 • Today Stacks and Queues • Linear structures used in problem domains and algorithms CompSci 100e, Spring2011

  3. Why don't we just use arrays? • Stacks used in implementing recursion, postscript language, Java language, graph algorithms • Stacks implemented using array/ArrayList • Queues used in simulation, graph algorithms, scheduling • Queues implemented using array/LinkedList CompSci 100, Fall 2010

  4. Simple stack example • Stack is part of java.util.Collections hierarchy • As an ADT it's a LIFO data structure (last in, first out) • what does pop do? What does push do? Stack<String> s = new Stack<String>(); s.push("panda"); s.push("grizzly"); s.push("brown"); System.out.println("size = "+s.size()); System.out.println(s.peek()); String str = s.pop(); System.out.println(s.peek()); System.out.println(s.pop()); CompSci 100, Fall 2010

  5. Postfix, prefix, and infix notation • Postfix notation used in some HP calculators • No parentheses needed, precedence rules still respected 3 5 + 4 2 * 7 + 3 - 9 7 + * • Read expression • For number/operand: push • For operator: pop, pop, operate, push • See Postfix.java for example code, key ideas: • Use StringTokenizer, handy tool for parsing • Note: Exceptions thrown, what are these? • What about prefix and infix notations, advantages? CompSci 100, Fall 2010

  6. Interlude: Exceptions • Exceptions are raised or thrown in exceptional cases • Bad indexes, null pointers, illegal arguments, … • File not found, URL malformed, … • Runtime exceptions aren't handled or caught • Bad index in array, don't try to handle this in code • Null pointer stops your program, don't code that way! • Some exceptions are caught or rethrown • FileNotFoundException and IOException • RuntimeException extends Exception • catch not required CompSci 100, Fall 2010

  7. Simple queue example • Queue is part of java.util.Collections hierarchy • As an ADT it's a FIFO data structure (first in, first out) • what does add do? What does remove do? Queue<String> s = new Queue<String>(); s.add("panda"); s.add("grizzly"); s.add("brown"); System.out.println("size = "+s.size()); System.out.println(s.peek()); String str = s.remove(); System.out.println(s.peek()); System.out.println(s.remove()); CompSci 100, Fall 2010

  8. Analysis • Assume a Stack is implemented with an ArrayList. How are inserts and removes done? • Inserting N elements into a stack takes how long? • Removing N removes from a stack takes how long? • Assume a Queue is implemented with an ArrayList. How are inserts and removes done? • Inserting N elements into a queue takes how long? • Removing N elements from a queue takes how long? CompSci 100e, Spring2011