Stack Applications in Data Structure
Stack Applications in Data Structure
What is stack ?
Stack is a special type of data structure. y Compared to a container. y Last In First Out.
y
What is push ?
y
Algorithm push
Procedure Push (Value) If TOP is equal to MAX Output an error that the Stack is full Else Add 1 to TOP Put data in Value into TOP position End If End Procedure
What is pop ?
y
Algorithm pop
Procedure Pop If TOP is equal to Zero then Stack is empty Underflow Else Output value from Stack at TOP position Subtract 1 from TOP End If End Procedure
Stack Applications
Applications of stack
Start up & Shut down. Function calling. Argument passing in c.
System Startup
System Shutdown
Function calling
void three() { printf("Three started\n"); printf("Three ended\n"); } void two() { printf("Two started\n"); three(); printf("Two ended\n"); } void one() { printf("One started\n"); two(); printf("One ended\n"); } void main() { clrscr(); printf("Main started\n"); one(); printf("Main ended\n"); getch(); }
Output
Main started One started Two started Three started Three ended Two ended One ended Main ended
Argument passing in C
# include <stdio.h> # include <conio.h> void main() { int a=3; clrscr(); printf(%d %d%d%d,a++,++a,++a,a++); getch(); }
But o/p is 6 6 5 3
Because the argument are passed from right to left in a stack and then sent to printf function.
%d %d %d %d 6 6 5 3
Top
Called function ie, printf(); Here the input is taken from stack So, the data order will be 6 6 5 3. Therefore the o/p will be 6 6 5 3.
Conclusion
y
Thank You.