A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner....

23
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item inserted, and not yet removed, will be the first (next) item dispensed. Real World Examples a spring-loaded cafeteria tray dispenser. a driveway wide that is one- car wide The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

description

(i.e. the class specification) Domain sequence of Object Constructor public Stack() post: this == sequence{} Query methods public boolean isEmpty() post: result == ( this-> size() == 0) public Object top() pre: ! isEmpty() post: result == this-> first() Update methods public void push(Object z) post: this == -> prepend( z ) public void pop() pre: ! isEmpty() (throws java.util.EmptyStackException) post: this == -> subSequence( 2, -> size() ) Stack The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

Transcript of A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner....

Page 1: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner.

LIFO - Last In First OutThe last (most recent) item inserted, and not yet

removed, will be the first (next) item dispensed.

Real World Examples a spring-loaded cafeteria tray

dispenser. a driveway wide that is one-car wide

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 2: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

Stack Operationspush

insert a new item into the stackpop remove one item from the containertop inspect one item from the container, but don’t remove it

An Abstract Picture

push

pop

top

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 3: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

(i.e. the class specification)

Domain sequence of Object

Constructor public Stack()

post: this == sequence{}

Query methods public boolean isEmpty()

post: result == (this->size() == 0)

public Object top()pre: ! isEmpty()post: result == this->first()

Update methods public void push(Object z)

post: this == this@pre->prepend(z)

public void pop()pre: ! isEmpty() (throws java.util.EmptyStackException)post: this == this@pre->subSequence( 2, this@pre->size() )

Stack

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 4: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

Stack + Stack() + boolean isEmpty() + void push(Object z) + void pop() + Object top()

Example Stack s1, s2;

String str;

s1 = new Stack();s1.push( “humble” );s1.push( “bumble” );s1.push( “mumble” );s1.pop();s1.push( “tumble” );str = (String) s1.top();s1.push( “rumble” );s1.push( “crumble” );s1.pop();s1.pop();s1.push( s1.top() );

s2 = new Stack();while ( !s1.isEmpty() ) { s2.push( s1.top() ); s1.pop();}

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 5: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

How would you represent ...... a bounded stack?

Would you use inheritance or aggregation?

... an unbounded stack?

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 6: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

public class Stack private SimplerSingleList theList;

/* post: theList.isEmpty() */public Stack() { theList = new SimplerSingleList();}/* post: result == theList.isEmpty() */public boolean isEmpty() { return theList.isEmpty();}/* post: theList == theList@pre with z appended to front*/public void push(Object z) { theList.start(); theList.insertBefore(z);}

//continued …

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 7: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

/* pre: ! theList.isEmpty() post: theList == theList@pre with first item removed */public void pop() { theList.start(); theList.removeAt();}/* pre: ! theList.isEmpty() post: result == first item of theList */public Object top() { theList.start(); return theList.item();}

}

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 8: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

Automatic Data During Program Execution Each method call causes an activation record (local variables &

parameters) to be pushed upon the run-time stack.

When a method returns its activation record is popped off the run-time stack.

Compilers (parsing) Each time the compiler encounters a { it is pushed upon the

parse stack. Each time the compiler encounters a } it is pops the matching } from the parse stack.

Automata TheoryThe finite state machine can be augmented by adding a stack. Such a device is called a push-down automaton.

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 9: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

Stacks are useful for evaluating many kinds of expressions.

Reverse Polish notation In Reverse Polish Notation (RPN) operands occur before their

associated operator, and operators occur as soon as possible. (Also called postfix.)

Examples Infix RPN

7 - 3 7, 3, -

1 + (2*3) - 4 1, 2, 3, *, +, 4, -

8 * 4 + 5 8, 4, *, 5, +

((9+8)*7)-(6*(5+(4/3))) 9,8,+,7,*,6,5,4,3,/,+,*,-

Rules:1) Operands occur left-to-right in the same order as infix

expression2) Operation follows immediately after its

operand/subexpression

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 10: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

Page 11: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

9

Page 12: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

98

Page 13: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

17

Page 14: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

177

Page 15: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

119

Page 16: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

1196

Page 17: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

11965

Page 18: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

119654

Page 19: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

1196543

Page 20: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

119651

Page 21: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

11966

Page 22: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

11936

Page 23: A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.

9, 8, +, 7, *, 6, 5, 4, 3, /, +, *, -

Example

OperandStack

Scan expression left to right, and…

For each operand - push

For each operator - replace two top operands by their subexpression value

(Note: top of stack is right operand, second in stack is left.)

The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.

83