file: 03.1.variationsOnStacks.txt author: Bob Muller date: January 27, 2014 CS102 Computer Science 102 Spring 2014 Lecture Notes for Meeting 1 of Week 3 Topics: 0. Generic Stacks 1. Banishing Stack Overflow A. Resizing Sequential Implementations B. Linked Representation ----------------------------------------------------------------- Notes: 0. Scope: In coding we use a lot of symbolic names: if, then, while, public, def, x, int, double, Stack, myStack, ... Each programming language will designate some symbols as reserved -keywords- which the programmer is not allowed to re-purpose. E.g., in Python, the symbols "if", "while", "def", ... are keywords while in Java the symbols "if", "then", "int", "public", ... are keywords. Programming language will also have rules and conventions for coder-defined symbols. E.g., in Java, variable names start with a lowercase letter while class and interface names starte with an upper case letter. Unlike Python, Java requires each coder-defined symbol to be announced or -declared- so the Java compiler can process it. This distinguished occurrence of that symbol, to be concrete let's use "x", is called a -binding occurrence- of the symbol. In Java, we would typically have something like: public int f(int x) { ... } or public int f() { int x; ... } Both of these occurrences of x are binding occurrences, the former is a formal parameter, accepting input to the function f, the latter is a local variable. Occurrences of coder-defined names that are not binding occurrences are called -uses- (or sometimes -applied occurrences-) of the name. In general, there can be many occurrences of the same name within a given program. The region of text in which uses of a name relate to a given binding occurrence is called the -scope- of the name. In Java, this region of text is usually delimited by curly braces. See SW page 87 for an example. 1. Review PostfixEval, StringBuilder See the posted code from last week. ----------------------------------------------------------------- 0. Generic Stacks --- AVOIDING USELESS WORK. Think for a minute about the following two functions: int addSix(int x) { return x + 6; } int addTen(int x) { return x + 10; } These will both compile and run without error but as coders we should be unhappy because we have two functions are essentially the same, apart from their names, they vary only in the presence of "6" and "10". It wouldn't matter for something so small, but for bigger things we have to worry about: 1. implementing two functions 2. debugging two functions, 3. documenting two functions, 4. extending two functions, etc. Procedural abstraction tells us to replace the varying parts of the two functions with a variable, then we'd have only one function to worry about: int add(int x, int y) { return x + y; } We can call this one function as in add(4, 6) or add(4, 10). Now consider stacks. Our ADT for a stack of Strings: public interface StringStack { public void push(String s); public String pop(); public boolean isEmpty(); public String toString(); } Now a different ADT for a stack of Doubles: public interface DoubleStack { public void push(Double s); public Double pop(); public boolean isEmpty(); public String toString(); } These vary only in the types of the push and pop operations. The -generic- (or polymorphic) case, USE A TYPE VARIABLE T IN THE PLACES WHERE THE TWO INTERFACES VARY: public interface Stack { public void push(T s); public T pop(); public boolean isEmpty(); public String toString(); } Then a -use- of the interface provides a type that will be plugged in for type variable T: Stack dStack; // T will be replaced by Double Stack sStack; // T will be replaced by String (There is another (older) type of polymorphism in Java that we will discuss in the coming weeks.) ----------------------------------------------------------------- 1. Banishing the possibility of Stack Overflow A. A Resizing Sequential Implementation See http://algs4.cs.princeton.edu/13stacks/ResizingArrayStack.java ----------------------------------------------------------------- B. A Linked Representation See the linked zip file.