0% found this document useful (0 votes)
22 views

Data Structures and Algorithms: Stacks

Stacks are a data structure that follow the LIFO (last-in, first-out) principle. They have two main operations - push which adds an item to the top of the stack, and pop which removes an item from the top. Stacks can be implemented using arrays or linked lists. Stacks are important in computer programs as the call stack is used to manage function calls and returns. The call stack allows recursive function calls by pushing the stack frame for each function onto the stack.

Uploaded by

man07bcc035
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Data Structures and Algorithms: Stacks

Stacks are a data structure that follow the LIFO (last-in, first-out) principle. They have two main operations - push which adds an item to the top of the stack, and pop which removes an item from the top. Stacks can be implemented using arrays or linked lists. Stacks are important in computer programs as the call stack is used to manage function calls and returns. The call stack allows recursive function calls by pushing the stack frame for each function onto the stack.

Uploaded by

man07bcc035
Copyright
© Attribution Non-Commercial (BY-NC)
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 Euclids 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)

int fib( n ) { if ( n < 2 ) return 1; else return fib(n-1) + fib(n-2); } Simple, elegant solution!

Recursion - Example
Fibonacci Numbers
C int fib( n ) { if ( n < 2 ) return 1; else return fib(n-1) + fib(n-2); }

However, many recursive functions, eg binary search, are simple, elegant and efficient!

You might also like