0% found this document useful (0 votes)
108 views20 pages

L5 Stacks PDF

The document discusses stacks and their implementation and applications. It specifically covers: 1) Stacks are data structures that only allow insertions and deletions at one end, following the last-in first-out (LIFO) principle. 2) The function call stack tracks function calls and returns by pushing stack frames for local variables and return addresses onto the stack. 3) A stack overflow occurs if the function call stack exceeds available memory from too many nested function calls. 4) Common stack operations like isEmpty, push, pop and peek are implemented recursively and iteratively.

Uploaded by

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

L5 Stacks PDF

The document discusses stacks and their implementation and applications. It specifically covers: 1) Stacks are data structures that only allow insertions and deletions at one end, following the last-in first-out (LIFO) principle. 2) The function call stack tracks function calls and returns by pushing stack frames for local variables and return addresses onto the stack. 3) A stack overflow occurs if the function call stack exceeds available memory from too many nested function calls. 4) Common stack operations like isEmpty, push, pop and peek are implemented recursively and iteratively.

Uploaded by

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

CPSC 122 – DATA STRUCTURES

Washington State University -- EECS


STACKS
Stacks

■ Data Structure that holds a sequence of elements

■ Insertions (PUSHes) may only be made at the top and deletions (POPs)
may only be made at the top
– A stack is referred to as a last-in, first-out (LIFO) data structure
– A stack is a restricted or constrained list

■ Step through the link list and delete every node


The Function-Call Stack

■ Also known as the program-execution stack, run-time stack,


program stack, or simply “the stack”
■ Works behind the scenes – supports the function
call/return mechanism – LIFO
– Necessary to track sequence of called functions
■ Refer to D & D Section 6.11
The Function-Call Stack

■ Supports the creation, maintenance, and destruction of


each called function’s local variables
■ Call stack memory is placed in RAM; monitored closely by
CPU
■ When a function declares a variable, it is “pushed” onto the
stack (dynamic memory is not though!)
■ Parameters are also passed using the call stack
Stack Frame

■ Each called function must eventually return control to the calling function:

■ The system must track the return address that each called function needs to return
control to the calling function – the function- call stack handles this info
Stack Frame
■ If called function returns, instead of calling another function
before returning, then the stack frame for the function call is
popped, and control transfers to the return address in the stack
frame

■ The information required for the called function to return to its


caller is always at the top of the call stack!

■ If a called function makes a call to another function, then the


stack frame for the new function is pushed to the top of the
stack
Stack Frames & Local Variables

■ Local variables including parameters and variables declared


by the function are reserved in the stack frame

■ The reason is these variables need to remain active if a


function makes a call to another function and “go away”
when the function returns to its caller
Stack Overflow

■ If more function calls occur than can be handled by the


finite amount of memory for the function call-stack, then an
error called stack overflow occurs

■ There is high potential for this occurring with recursion, on


problems that require a lot of recursive steps!
STACK
IMPLEMENTATION
Stack Functionalities

■ isEmpty

■ push

■ pop

■ peek
Print List recursive implementation

void printStackRecursive (StackNode *pStack) {


if (!isEmpty (pStack)) // Recursive step {
printf (“| %d |\n”, pStack -> data);
printf (“ | \n”); // Trying to imitate link
printf (“ V \n”);
// Get to the next item
pStack = pStack -> pNext;
printStackRecursive (pStack);
}
else // Base case {
printf (“NULL\n”);
}
}
Another Pop Implementation
char pop(StackNode **pStack) {
StackNode *pTop = NULL;
char retData = ‘\0’;
if (!isEmpty (*pStack)) // Stack is not empty; defensive design

{
pTop = *pStack; // Temp storage of top of stack
retData = (*pStack) -> data; // Keep data in top node
*pStack = (*pStack) -> pNext;
free (pTop); // Remove the top node
}
return retData;
}
Stack Application
■ Reversing strings
■ Checking for palindromes
■ Searching for a path in a maze
■ Tower of Hanoi
■ Evaluating infix expressions
■ Function call stacks
■ Many others...
Stock Span Problem

■ is commonly asked in Google and Amazon interviews and taught as the application
of stack data structure in universities. Let’s define the problem :

Given a list of prices of a single stock for N number of days, find stock span for each
day. Stock span is defined as a number of consecutive days prior to the current day
when the price of a stock was less than or equal to the price at current day.

■ For example
– {100, 60, 70, 65, 80, 85, 200} span will be {1, 1, 2, 1, 4, 5, 6}.
– {100, 80, 60, 70, 60, 75, 85} span will be {1, 1, 1, 2, 1, 4, 6}.
100 80

75 85
70
60
60
Iterative/Brute Force Solution
■ For each day, say current day, scan all days prior to it and
increment span till the price of the stock is higher than
current day. Simple implementation, however complexity
is O(n2) where n is number of days.
Stock Span Stack Solution

Go through each day stock price, check if the current price on top of
the stack is less than the current day’s priceà
■ If yes, pop out till price on top of the stack is greater than current
day’s price, stock span of the current day is the difference
between the day of price on top of the stack and current day.
■ Storing index of last greatest stock price would make things easier
as compared to storing actual stock price on the stack. Hence day
is store i on stack, price[i] will give us the price of stock on day i.
Stock Span Stack Solution

1. Initialize span of day 1 (i=0) as 1 and put on to stack.


2. For i=1 to n, do following
1. While price[stackpeek()] < price[i] and !stack.isEmpty()
■ stack.pop()
2. If price[stack.peek()] > price[i],
■ span = (i - stack.peek())
3. Push current day index i on to stack.

You might also like