0% found this document useful (0 votes)
10 views9 pages

Stacks

- Stacks are a special type of data structure that follow the LIFO (last-in, first-out) principle. They have two main operations: push to add an item to the top, and pop to remove an item from the top. - Stacks can be implemented using arrays or linked lists. They are useful for modeling function calls and returns, where the call stack stores stack frames with local variables and return addresses. - Recursion involves functions that call themselves, either directly or indirectly. It provides an elegant solution for problems like calculating Fibonacci numbers or traversing recursive data structures.

Uploaded by

virulentus999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Stacks

- Stacks are a special type of data structure that follow the LIFO (last-in, first-out) principle. They have two main operations: push to add an item to the top, and pop to remove an item from the top. - Stacks can be implemented using arrays or linked lists. They are useful for modeling function calls and returns, where the call stack stores stack frames with local variables and return addresses. - Recursion involves functions that call themselves, either directly or indirectly. It provides an elegant solution for problems like calculating Fibonacci numbers or traversing recursive data structures.

Uploaded by

virulentus999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Data Structures and Algorithms

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

int IsEmpty( Stack s );


/* Return TRUE if empty */
void *Top( Stack s );
/* Return the item at the top,
without deleting it */
Stacks - Implementation
• Arrays
• Provide a stack capacity to the constructor
• Flexibility limited but matches many real uses
• Capacity limited by some constraint
• Memory in your computer
• Size of the plate stacker, etc
• push, pop methods
• Variants of AddToC…, DeleteFromC…
• Linked list also possible
• Stack:
• basically a Collection with special semantics!
Stacks - Relevance
• Stacks appear in computer programs
• Key to call / return in functions & procedures
• Stack frame allows recursive calls
• Call: push stack frame
• Return: pop stack frame
• Stack frame
• Function arguments
• Return address
• Local variables
Stack Frames - Functions in HLL
• Program
function f( int x, int y) {
int a;
if ( term_cond ) return …;
a = ….;
return g( a );
}

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

Pseudo-code fib( n ) = if ( n = 0 ) then 1


else if ( n = 1 ) then 1
else fib(n-1) + fib(n-2)
C int fib( n ) {
if ( n < 2 ) return 1;
else return fib(n-1) + fib(n-2);
}

Simple, elegant solution!


Recursion - Example
• Fibonacci Numbers
er ! ! ! !
di sast
C int fib( n ) { e - t im
r un
if ( n i<ca2se), areturn 1;
nacc
e Fi bo return fib(n-1) + fib(n-2);
else
t , i n th }
Bu

However, many recursive functions,


eg binary search, are simple, elegant and efficient!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy