Download presentation
Presentation is loading. Please wait.
Published byEdgar Gilbert Modified over 9 years ago
1
Computer Science 112 Fundamentals of Programming II Introduction to Stacks
2
The Stack Collection: Overview Formal properties Applications Implementations
3
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
4
LIFO Access DDDDDDD push DDD pop Top of stack
5
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 sTrue if item is in stack, False otherwise s1 + s2Returns a new stack with items in s1 and s2 s1 == s2Equality test for two stacks Minimal Set of Stack Operations
6
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 sTrue if item is in stack, False otherwise s1 + s2Returns a new stack with items in s1 and s2 s1 == s2Equality 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
7
Stack Implementations Array-based Singly linked structure Always a single set of abstract operations stack1 = LinkedStack() stack2 = ArrayStack([1, 2, 3, 4])
8
The Stack Resources LinkedStackArrayStack StackInterface AbstractStack AbstractCollection
9
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
10
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?
11
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
12
Example: N! N! = 1, when N = 1 N! = 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:
13
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
14
Example: factorial(4) 4 n return value Activation record for factorial(4) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
15
Activations Are Added Dynamically 4 n return value Activation record for factorial(4) 3 n return value Activation record for factorial(3) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
16
Number of Activations = # Calls 4 n return value Activation record for factorial(4) 3 n return value Activation record for factorial(3) 2 n return value Activation record for factorial(2) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
17
Recursive Process Bottoms out 4 n return value Activation record for factorial(4) 3 n return value Activation record for factorial(3) 2 n return value Activation record for factorial(2) 1 n return value Activation record for factorial(1) def factorial(n): if n == 1: return 1
18
Recursive Process Unwinds 4 n return value Activation record for factorial(4) 3 n return value Activation record for factorial(3) 2 n return value Activation record for factorial(2) 11 n return value Activation record for factorial(1) def factorial(n): if n == 1: return 1
19
Activations Are Deallocated 4 n return value Activation record for factorial(4) 3 n return value Activation record for factorial(3) 22 n return value Activation record for factorial(2) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
20
Activations Are Deallocated 4 n return value Activation record for factorial(4) 36 n return value Activation record for factorial(3) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
21
Value Returned Is in the First Activation 424 n return value Activation record for factorial(4) def factorial(n): if n == 1: return 1 else: return n * factorial(n – 1)
22
def factorialWithLoopAndStack(n): stack = LinkedStack() Loop with Explicit Stack
23
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
24
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
25
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
26
Matching Parentheses expression = { letter } | "(" expression ")" a ab (a) ((a)) (a(b)c) () (()) a( )( a) ((()) Grammar: Legal expressions: Illegal expressions: An expression is either a sequence of zero or more letters or an expression within parentheses.
27
Create a new stack Set match to true For 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 stack If the stack is not empty Set match to false Return match Parentheses Matching Algorithm
28
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
29
For Monday More stack applications
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.