04 A: Lists, Stacks, and Queues III CS1102S: Data Structures and - - PowerPoint PPT Presentation

04 a lists stacks and queues iii
SMART_READER_LITE
LIVE PREVIEW

04 A: Lists, Stacks, and Queues III CS1102S: Data Structures and - - PowerPoint PPT Presentation

Generic Types in Java Higher-order Programming In Java The Stack ADT 04 A: Lists, Stacks, and Queues III CS1102S: Data Structures and Algorithms Martin Henz February 3, 2010 Generated on Monday 1 st February, 2010, 16:31 CS1102S: Data


slide-1
SLIDE 1

Generic Types in Java Higher-order Programming In Java The Stack ADT

04 A: Lists, Stacks, and Queues III

CS1102S: Data Structures and Algorithms

Martin Henz

February 3, 2010

Generated on Monday 1st February, 2010, 16:31 CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 1

slide-2
SLIDE 2

Generic Types in Java Higher-order Programming In Java The Stack ADT

1

Generic Types in Java

2

Higher-order Programming In Java

3

The Stack ADT

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 2

slide-3
SLIDE 3

Generic Types in Java Higher-order Programming In Java The Stack ADT

1

Generic Types in Java

2

Higher-order Programming In Java

3

The Stack ADT

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 3

slide-4
SLIDE 4

Generic Types in Java Higher-order Programming In Java The Stack ADT

A Simple Box Class

public class IntegerBox { private Integer integer ; public void add ( Integer i ) { integer = i ; } public Integer get ( ) { return integer ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 4

slide-5
SLIDE 5

Generic Types in Java Higher-order Programming In Java The Stack ADT

Is this practical?

Situation Each time we want to have a box for some data type, we need to define a MyTypeBox class. First Idea Use Object as the type of the elements. After all, any Java

  • bject is an Object!

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 5

slide-6
SLIDE 6

Generic Types in Java Higher-order Programming In Java The Stack ADT

An Object Box Class

public class ObjectBox { private Object

  • bject ;

public void add ( Object

  • bj ) {
  • bject = obj ;

} public Object get ( ) { return

  • bject ;

} }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 6

slide-7
SLIDE 7

Generic Types in Java Higher-order Programming In Java The Stack ADT

Using the Object Box

public class ObjectBoxTest { public static void main ( String [ ] args ) { / / ONLY place Integer

  • bjects

i n t o box ! ObjectBox integerBox = new ObjectBox ( ) ; integerBox . add (new Integer ( 1 0 ) ) ; Integer someInteger = ( Integer ) integerBox . get ( ) ; System . out . p r i n t l n ( someInteger ) ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 7

slide-8
SLIDE 8

Generic Types in Java Higher-order Programming In Java The Stack ADT

Using the Object Box (2)

/ / ONLY place Integer

  • bjects

i n t o t h i s box ! ObjectBox integerBox = new ObjectBox ( ) ; / / Imagine t h i s i s one part

  • f

large a p p l i c a t i o n / / modified by one programmer . integerBox . add ( ” 10 ” ) ; / / note type now String / / . . . and t h i s i s another , perhaps w r i t t e n / / by a d i f f e r e n t programmer Integer someInteger = ( Integer ) integerBox . get ( ) ; System . out . p r i n t l n ( someInteger ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 8

slide-9
SLIDE 9

Generic Types in Java Higher-order Programming In Java The Stack ADT

Is this practical?

Situation In order to take items out of the box, we need to use a cast

  • peration. This operation circumvents Java’s type system, and

is not safe, especially in large programs! Idea Write a “generic” class that can be re-used for any kind of content type

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 9

slide-10
SLIDE 10

Generic Types in Java Higher-order Programming In Java The Stack ADT

A Generic Box

public class Box<T> { private T t ; / / T stands f o r ” Type ” public void add (T t ) { this . t = t ; } public T get ( ) { return t ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 10

slide-11
SLIDE 11

Generic Types in Java Higher-order Programming In Java The Stack ADT

Using A Generic Box

Box<Integer > integerBox = new Box<Integer >(); integerBox . add (new Integer ( 1 0 ) ) ; Integer someInteger = integerBox . get ( ) ; / / no cast ! System . out . p r i n t l n ( someInteger ) ; / / t r y adding a String : / / integerBox . add ( ” some s t r i n g ” ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 11

slide-12
SLIDE 12

Generic Types in Java Higher-order Programming In Java The Stack ADT

How to write functions on boxes?

Issue The Java operator new needs a proper class, not a generic one! Example public class BoxUtil { public static void f i l l B o x e s (U u , List <Box<U > > boxes ) { for (Box<U > box : boxes ) box . add ( u ) ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 12

slide-13
SLIDE 13

Generic Types in Java Higher-order Programming In Java The Stack ADT

How to write functions on boxes?

Example public class BoxUtil { public static void f i l l B o x e s (U u , List <Box<U > > boxes ) { for (Box<U > box : boxes ) box . add ( u ) ; } } Problem Where does the type U come from?

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 13

slide-14
SLIDE 14

Generic Types in Java Higher-order Programming In Java The Stack ADT

Generic Method

Example public class BoxUtil { public static <U > void f i l l B o x e s (U u , List <Box<U > > boxes ) { for (Box<U > box : boxes ) box . add ( u ) ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 14

slide-15
SLIDE 15

Generic Types in Java Higher-order Programming In Java The Stack ADT

Generic Method: Example

class Crayon {}; Crayon red = new Crayon ( ) ; List <Box<Crayon> > crayonBoxes = new ArrayList <Box<Crayon >>(); . . . BoxUtil .<Crayon>f i l l B o x e s ( red , crayonBoxes ) ; BoxUtil . f i l l B o x e s ( red , crayonBoxes ) ; / / compiler i n f e r s that U i s Crayon

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 15

slide-16
SLIDE 16

Generic Types in Java Higher-order Programming In Java The Stack ADT

An Issue: Subtyping

public class Subtyping { public static void someMethod (Number n){ System . out . p r i n t l n ( n ) ; } public static void main ( String [ ] args ) { Object someObject = new Object ( ) ; Integer someInteger = new Integer ( 1 0 ) ; someObject = someInteger ; / / OK someMethod (new Integer ( 1 0 ) ) ; / / OK someMethod (new Double ( 1 0 . 1 ) ) ; / / OK } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 16

slide-17
SLIDE 17

Generic Types in Java Higher-order Programming In Java The Stack ADT

An Issue: Subtyping

public class Subtyping { public static void boxTest (Box<Number> n){ System . out . p r i n t l n ( n ) ; } public static void main ( String [ ] args ) { Box<Number> box = new Box<Number>(); box . add (new Integer ( 1 0 ) ) ; / / OK box . add (new Double ( 1 0 . 1 ) ) ; / / OK boxTest (new Box<Integer > ( ) ) ; / / NOT OK } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 17

slide-18
SLIDE 18

Generic Types in Java Higher-order Programming In Java The Stack ADT

Subtyping Explanation: Cages and Animals

public class Animal {} public class Lion extends Animal {} public class B u t t e r f l y extends Animal {} public class Cage<E> extends HashSet<E> implements Collection <E> {}

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 18

slide-19
SLIDE 19

Generic Types in Java Higher-order Programming In Java The Stack ADT

Subtyping Explanation: Cages and Animals

Lion king = new Lion ( ) ; Animal a = king ; Cage<Lion> lionCage = new Cage<Lion >(); lionCage . add ( king ) ; B u t t e r f l y monarch = new B u t t e r f l y ( ) ; Cage<B u t t e r f l y > butterflyCage = new Cage<B u t t e r f l y >(); butterflyCage . add ( monarch ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 19

slide-20
SLIDE 20

Generic Types in Java Higher-order Programming In Java The Stack ADT

Subtyping Explanation: Cages and Animals

Cage<Animal> animalCage = new Cage<Animal >(); animalCage . add ( king ) ; animalCage . add ( monarch ) ; animalCage = lionCage ; / / compile−time error animalCage = butterflyCage ; / / compile−time error

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 20

slide-21
SLIDE 21

Generic Types in Java Higher-order Programming In Java The Stack ADT

1

Generic Types in Java

2

Higher-order Programming In Java

3

The Stack ADT

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 21

slide-22
SLIDE 22

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List

List <Integer > myIntegerList = new ArrayList <Integer >(); myIntegerList . add ( 4 ) ; myIntegerList . add ( 1 3 ) ; myIntegerList . add ( 5 ) ; List <Integer > myIntegerListSquare = ListMap . integerListSquare ( myIntegerList ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 22

slide-23
SLIDE 23

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List

public class ListMap { public static List <Integer > integerListSquare ( List <Integer > a ) { ArrayList <Integer > r e s u l t L i s t = new ArrayList <Integer >(); for ( Integer i : a ) r e s u l t L i s t . add ( i ∗ i ) ; return r e s u l t L i s t ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 23

slide-24
SLIDE 24

Generic Types in Java Higher-order Programming In Java The Stack ADT

Is this practical?

Situation Each time we want to apply an operation on all elements of a list, we need to write a new loop. Idea We encode the operation to be applied on each element as an

  • bject.

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 24

slide-25
SLIDE 25

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (2)

List <Integer > mySecondIntegerListSquare = ListMap . integerListMap ( myIntegerList , new Square ( ) ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 25

slide-26
SLIDE 26

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (2)

public class ListMap { public static List <Integer > integerListMap ( List <Integer > l i s t , IntegerToInteger f ) { ArrayList <Integer > r e s u l t L i s t = new ArrayList <Integer >(); for ( Integer i : l i s t ) r e s u l t L i s t . add ( f . apply ( i ) ) ; return r e s u l t L i s t ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 26

slide-27
SLIDE 27

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (2)

abstract class IntegerToInteger { abstract Integer apply ( Integer i ) ; }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 27

slide-28
SLIDE 28

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (2)

public class Square extends IntegerToInteger { public Integer apply ( Integer x ) { return x ∗ x ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 28

slide-29
SLIDE 29

Generic Types in Java Higher-order Programming In Java The Stack ADT

Is this practical?

Situation Each time we want to apply an operation on all elements of a list, we need to write a new class for the operation (e.g. Square). Idea Place the class where it is needed!

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 29

slide-30
SLIDE 30

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (3)

List <Integer > myThirdIntegerListSquare = ListMap . integerListMap ( myIntegerList , new IntegerToInteger ( ) { Integer apply ( Integer i ) { return i ∗ i ; } } ) ; p r i n t I n t L i s t ( myThirdIntegerListSquare ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 30

slide-31
SLIDE 31

Generic Types in Java Higher-order Programming In Java The Stack ADT

Is this practical?

Situation Each time we want to apply an operation on all elements of a list, we need to write a new class for the function type (e.g. IntegerToInteger) Idea Make the function generic!

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 31

slide-32
SLIDE 32

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (4)

abstract class IntegerToInteger { abstract Integer apply ( Integer i ) ; } abstract class Function<Any> { abstract Any apply ( Any i ) ; }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 32

slide-33
SLIDE 33

Generic Types in Java Higher-order Programming In Java The Stack ADT

Squaring the Elements of a List (4)

List <Integer > myFourthIntegerListSquare = ListMap . listMap ( myIntegerList , new Function<Integer >(){ Integer apply ( Integer i ) { return i ∗ i ; } } ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 33

slide-34
SLIDE 34

Generic Types in Java Higher-order Programming In Java The Stack ADT

Another Example: Function Composition

(define (compose f g) (lambda (x) (f (g x)))) (define square-after-inc (compose square inc)) (square-after-inc 6)

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 34

slide-35
SLIDE 35

Generic Types in Java Higher-order Programming In Java The Stack ADT

Another Example: Function Composition

public static <Any> Function<Any> compose( f i n a l Function<Any> f , f i n a l Function<Any> g ) { return new Function<Any>(){ Any apply ( Any i ) { return f . apply ( g . apply ( i ) ) ; } }; }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 35

slide-36
SLIDE 36

Generic Types in Java Higher-order Programming In Java The Stack ADT

Another Example: Function Composition

Function<Integer > squareAfterInc = Function . compose( new Function<Integer >(){ Integer apply ( Integer i ) { return i ∗ i ; } } , new Function<Integer >(){ Integer apply ( Integer i ) { return i + 1; } } ) ; System . out . p r i n t l n ( squareAfterInc . apply ( 6 ) ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 36

slide-37
SLIDE 37

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

1

Generic Types in Java

2

Higher-order Programming In Java

3

The Stack ADT Stack Model Implementation of Stacks Applications

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 37

slide-38
SLIDE 38

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Motivation

Purpose of stacks Collections that serve as intermediate storage of data items

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 38

slide-39
SLIDE 39

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Stack Model

Stack access Only the top element of a stack is accessible through top and pop operations Stack discipline Last in—first out: LIFO

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 39

slide-40
SLIDE 40

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Implementation of Stacks using ArrayList

class ArrayStack<E> extends ArrayList <E> { public boolean empty ( ) { return size ( ) == 0; } public E push (E item ) { add ( item ) ; return item ;} public E top ( ) { i f ( empty ( ) ) throw new java . u t i l . EmptyStackException ( ) ; else return get ( size () −1); } public E pop ( ) { i f ( empty ( ) ) throw new java . u t i l . EmptyStackException ( ) ; else return remove ( size () −1); } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 40

slide-41
SLIDE 41

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Example of Usage

Stack<Task> s = new ArrayStack<Task >(); s . push (new Task ( . . . ) ) ; s . push ( computeSomeTask ( . . . ) ) ; s . top ( ) . displayTask ( ) ; s . pop ( ) . completeTask ( ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 41

slide-42
SLIDE 42

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Implementation of Stacks using LinkedList

class LinkedListStack <E> extends LinkedList <E> { public boolean empty ( ) { return size ( ) == 0; } public E push (E item ) { add ( item , 0 ) ; return item ; } public E top ( ) { i f ( empty ( ) ) throw new java . u t i l . EmptyStackException ( ) ; else return get ( 0 ) ; } public E pop ( ) { i f ( empty ( ) ) throw new java . u t i l . EmptyStackException ( ) ; else return remove ( 0 ) ; } }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 42

slide-43
SLIDE 43

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Stacks in Practice

Stack classes are so similar to List classes that often Lists are used directly as stacks. Example: Stack<Task> s = new LinkedList <Task >(); s . add (0 ,new Task ( . . . ) ) ; s . add (0 , computeSomeTask ( . . . ) ) ; s . get ( 0 ) . displayTask ( ) ; s . remove ( 0 ) . completeTask ( ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 43

slide-44
SLIDE 44

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Balancing symbols

Check whether the parentheses in expression are properly nested; parentheses occur in pairs around proper expressions. Algorithm Stack<Character> s = new Stack<Character >(); while ( ! empty ( input ) ) { Character c = input . nextCharacter ( ) ; i f ( closingPar ( c ) ) { i f ( s . empty ( ) ) error ( ” not proper ” ) ; else checkPar ( s . pop ( ) , c ) ; } else i f ( openingPar ( c ) ) s . push ( c ) ; }

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 44

slide-45
SLIDE 45

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Expression Evaluation

Infix notation Easy for humans, but need precedences, parentheses etc. Example 6 ∗ (5 + (2 + 3) ∗ 8 + 3) Postfix notation Place operator after arguments; no ambiguities or parentheses Example 6 5 2 3 + 8 ∗ + 3 + ∗

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 45

slide-46
SLIDE 46

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Evaluation of Postfix Expressions

Idea Keep intermediate values in a stack. Algorithm Proceed from left to right number: push on stack

  • perator: pop first two numbers from stack, apply
  • peration on them, and push result back onto stack

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 46

slide-47
SLIDE 47

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Operand Stack in JVM

2 + 3 * 8 is compiled to bipush 2 bipush 3 bipush 8 imul iadd

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 47

slide-48
SLIDE 48

Generic Types in Java Higher-order Programming In Java The Stack ADT Stack Model Implementation of Stacks Applications

Method Calls

Runtime stack Data structure to keep track of the state of the JVM that needs to be restored when methods return Runtime stack frames Program counter (code address to be returned to) Local variable area (for arguments and local variables) Operand stack (for intermediate values) Example int i = 5; System . out . p r i n t l n ( f (7) + i ) ;

CS1102S: Data Structures and Algorithms 04 A: Lists, Stacks, and Queues III 48