Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

29
Computer Science 112 Fundamentals of Programming II Introduction to Stacks

Transcript of Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Page 1: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Computer Science 112

Fundamentals of Programming IIIntroduction to Stacks

Page 2: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

The Stack Collection: Overview

• Formal properties

• Applications

• Implementations

Page 3: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Formal Properties of Stacks

• Stacks are like lists in that elements are ordered by position (linear)

• However, access is only at one end, called the top

• A stack enforces last-in, first-out (LIFO) access

Page 4: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

LIFO Access

D

D

D

D

D

D

Dpush

D

D

Dpop

Top of stack

Page 5: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

s.isEmpty() Returns True if empty, False otherwise

len(s) Returns the number of items in the stack

str(s) Returns a string representation

iter(s) Supports a for loop

item in s True if item is in stack, False otherwise

s1 + s2 Returns a new stack with items in s1 and s2

s1 == s2 Equality test for two stacks

Minimal Set of Stack Operations

Page 6: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

s.isEmpty() Returns True if empty, False otherwise

len(s) Returns the number of items in the stack

str(s) Returns a string representation

iter(s) Supports a for loop

item in s True if item is in stack, False otherwise

s1 + s2 Returns a new stack with items in s1 and s2

s1 == s2 Equality test for two stacks

s.push(item) Adds item to the top of the stack

s.pop() Removes and returns top item

s.peek() Returns item at the top of the stack

The precondition of pop and peek is that the stack is not empty.

Minimal Set of Stack Operations

Page 7: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Stack Implementations

• Array-based

• Singly linked structure

• Always a single set of abstract operations

stack1 = LinkedStack()stack2 = ArrayStack([1, 2, 3, 4])

Page 8: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

The Stack Resources

LinkedStack ArrayStack

StackInterface

AbstractStack

AbstractCollection

Page 9: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

stack = LinkedStack() # or ArrayStack()

stack.push("A string")stack.push("Another string")

for item in stack: print(item)

while not stack.isEmpty(): print(stack.pop())

Example Use of a Stack

We have a single set of abstract operations and two implementing classes, ArrayStack and LinkedStack

Page 10: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

The Stack Iterator

stack1 = LinkedStack([1, 2, 3, 4])

stack2 = ArrayStack(stack1)

for item in stack1: print(item)

for item in stack2: print(item)

stack3 = ArrayStack(stack2)

Does the iterator start at the top or the bottom of the stack?

Page 11: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Stack Applications

Stacks are useful for algorithms that must backtrack to the most recent data element in a series of elements– Run-time support of recursion– Language processing– Game playing, puzzle solving– Tree and graph traversals

Page 12: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Example: N!N! = 1, when N = 1N! = N * (N - 1)!, otherwise

Recursive definition:

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Recursive function:

factorial(4) -> 4 * factorial(3) -> 3 * factorial(2) -> 2 * factorial(1) -> 1 <- 2 <- 6 <-24 <-

Runtime trace:

Page 13: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

How Recursion Works

• Each call of a function generates an instance of that function

• An instance of a function contains– memory for each parameter– memory for each local variable– memory for the return value– a pointer to the caller’s next instruction (return

address)

• This chunk of memory is also called an activation record

Page 14: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Example: factorial(4)

4nreturn value Activation record for factorial(4)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 15: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Activations Are Added Dynamically

4nreturn value Activation record for factorial(4)

3nreturn value Activation record for factorial(3)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 16: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Number of Activations = # Calls

4nreturn value Activation record for factorial(4)

3nreturn value Activation record for factorial(3)

2nreturn value Activation record for factorial(2)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 17: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Recursive Process Bottoms out

4nreturn value Activation record for factorial(4)

3nreturn value Activation record for factorial(3)

2nreturn value Activation record for factorial(2)

1nreturn value Activation record for factorial(1)

def factorial(n): if n == 1: return 1

Page 18: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Recursive Process Unwinds

4nreturn value Activation record for factorial(4)

3nreturn value Activation record for factorial(3)

2nreturn value Activation record for factorial(2)

1

1

nreturn value Activation record for factorial(1)

def factorial(n): if n == 1: return 1

Page 19: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Activations Are Deallocated

4nreturn value Activation record for factorial(4)

3nreturn value Activation record for factorial(3)

2

2

nreturn value Activation record for factorial(2)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 20: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Activations Are Deallocated

4nreturn value Activation record for factorial(4)

3

6

nreturn value Activation record for factorial(3)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 21: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Value Returned Is in the First Activation

4

24

nreturn value Activation record for factorial(4)

def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)

Page 22: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

def factorialWithLoopAndStack(n): stack = LinkedStack()

Loop with Explicit Stack

Page 23: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

def factorialWithLoopAndStack(n): stack = LinkedStack()

# Load up the stack with the sequence of numbers from n # down to 1. while n >= 1: stack.push(n) n -= 1

Loop with Explicit Stack

Page 24: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

def factorialWithLoopAndStack(n): stack = LinkedStack()

# Load up the stack with the sequence of numbers from n # down to 1. while n >= 1: stack.push(n) n -= 1

# Pop the top two numbers and push their product onto the # stack, until there is just one number left on the stack. while len(stack) > 1: operand1 = stack.pop() operand2 = stack.pop() product = operand1 * operand2 stack.push(product)

Loop with Explicit Stack

Page 25: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

def factorialWithLoopAndStack(n): stack = LinkedStack()

# Load up the stack with the sequence of numbers from n # down to 1. while n >= 1: stack.push(n) n -= 1

# Pop the top two numbers and push their product onto the # stack, until there is just one number left on the stack. while len(stack) > 1: operand1 = stack.pop() operand2 = stack.pop() product = operand1 * operand2 stack.push(product)

# Return the number at the top of the stack. return stack.peek()

Loop with Explicit Stack

Page 26: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Matching Parentheses

expression = { letter } | "(" expression ")"

a ab (a) ((a)) (a(b)c) () (())

a( )( a) ((())

Grammar:

Legalexpressions:

Illegalexpressions:

An expression is either a sequence of zero or moreletters or an expression within parentheses.

Page 27: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

Create a new stackSet match to trueFor each character in the string If the character equals '(' Push the character onto the stack Else If the character equals ')' If the stack is empty Set match to false Break Else Pop the top '(' from the stackIf the stack is not empty Set match to falseReturn match

Parentheses Matching Algorithm

Page 28: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

def inputOk(s): stack = LinkedStack() match = True for ch in s: if ch == '(': stack.push(ch) elif ch == ')': if stack.isEmpty(): # No matching ( match = False break else: stack.pop() if not stack.isEmpty(): # No matching ) match = False return match

Python Code

Page 29: Computer Science 112 Fundamentals of Programming II Introduction to Stacks.

For Monday

More stack applications