Stacks
Stacks
Stacks
Stacks
• Stacks are a special form of collection
with LIFO semantics
• Two methods
• int push( Stack s, void *item );
- add item to the top of the stack
• void *pop( Stack s );
- remove an item from the top of the stack
• Like a plate stacker
• Other methods
function g( int z ) {
int p, q;
p = …. ; q = …. ;
return f(p,q);
}
Context
for execution of f
Recursion
• Very useful technique
• Definition of mathematical functions
• Definition of data structures
• Recursive structures are naturally
processed by recursive functions!
Recursion
• Very useful technique
• Definition of mathematical functions
• Definition of data structures
• Recursive structures are naturally
processed by recursive functions!
• Recursively defined functions
• factorial
• Fibonacci
• GCD by Euclid’s algorithm
• Fourier Transform
• Games
• Towers of Hanoi
• Chess
Recursion - Example
• Fibonacci Numbers