csc 205 – java programming ii

16
CSC 205 – Java Programming II Lecture 30 April 3, 2002

Upload: shelby

Post on 25-Jan-2016

34 views

Category:

Documents


0 download

DESCRIPTION

CSC 205 – Java Programming II. Lecture 30 April 3, 2002. Stack. A stack is an abstract data type (ADT) that contains a sequence of elements and honors the last-in-first-out (LIFO) rule. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSC 205 – Java Programming II

CSC 205 – Java Programming II

Lecture 30

April 3, 2002

Page 2: CSC 205 – Java Programming II

Stack

• A stack is an abstract data type (ADT) that contains a sequence of elements and honors the last-in-first-out (LIFO) rule.

• Conceptually, the only element that can be removed, accessed, modified is the one which was most recently inserted.– Pop and push are the two basic stack

operations

Page 3: CSC 205 – Java Programming II

Stack

PUSH

POP

top

last-in-first-out

Page 4: CSC 205 – Java Programming II

Examples

Green

Red

Red

Yellow

Red

Yellow

Green

Red

Yellow

poppop

pushgreen

Page 5: CSC 205 – Java Programming II

Java Stack Class

• The Java Stack class represents a stack of objects.– It outdated the Java Collections Framework– Available since JDK1.0

• It extends class Vector with 5 additional operations.

• With its top at the index size()-1

top

Page 6: CSC 205 – Java Programming II

Java Stack API

boolean empty()           Tests if this stack is empty.

Object peek()           Looks at the object at the top of this stack without removing it from the stack.

Object pop()           Removes the object at the top of this stack and returns that object as the value of this function.

Object push(Object item)           Pushes an item onto the top of this stack.

int search(Object o)           Returns the 1-based position where an object is on this stack.

Page 7: CSC 205 – Java Programming II

Example

• Checking for balanced braces– abc{defg{ijk}{l{mn}}op}qr balanced

– abc{def}}ghij{kl}m not balanced

• The braces are balanced if– Each time you encounter a }, it matches an

already encountered {– When you reach the end of the string, you have

matched each {

Page 8: CSC 205 – Java Programming II

Pseudocode Solution

• A simplified solution in pseudocodewhile (not at the end of the string) {

if (the next character is a ‘{’) {stack.push(‘{’)

} else if (the character is a ‘}’)

openBrace = stack.pop() }}

Page 9: CSC 205 – Java Programming II

Examples

{{{ {

{a{b}c}1. push “}”2. push “}”3. pop4. popStack empty balanced

{a{bc}1. push “}”2. push “}”3. popStack not empty not balanced

{{ab}c}

1. push “}”2. push “}”3. pop4. popStack empty balanced

{{{ {

Page 10: CSC 205 – Java Programming II

Problems w/ The Stack Class

• All the Vector methods can be used– For examples

myBookStack.get(7);

myBookStack.remove(0);

//the element right at the “bottom” can be removed!

Page 11: CSC 205 – Java Programming II

Convenient or Risky?

myBookStack.get(7);

myBookStack.remove(0);

Page 12: CSC 205 – Java Programming II

Alternative Designs

• Implement a RealStack class– Use a LinkedList object as its only field– Provide push and pop methods

public Object push(Object o) {

list.add(o);

return o;

}

public Object pop(Object o) {

return list.removeLast(o);

}

Page 13: CSC 205 – Java Programming II

Activation Stack

• Stacks are used almost exclusively by computer systems

• An activation stack is used by a compiler /interpreter to save the method information when a method is called

• The stack is part of the main memory.

• Other parts include a heap to hold data

Page 14: CSC 205 – Java Programming II

Activation Records

• Each activation record includes– The return address– The value of each argument– The values of the method’s other local

variables

Page 15: CSC 205 – Java Programming II

Decimal To Binary

public void processInput(String s) { writeBinary(Integer.parseInt(s)); //RA1}

public void writeBinary(int n) {if (n<=1) throw new IllegalArgumentException();if (n<=1) gui.println(n);else { writeBinary(n/2); //RA2 gui.println(n%2);}

}

Page 16: CSC 205 – Java Programming II

Example

RA16

RA16

RA16

RA16

RA16

RA23

RA23

RA23

RA21