Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 22

Stack Applications

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

Inserting a element into stack.

What is stack overflow ?


Pushing elements when stack is full. y When top exceeds size of stack .
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

Removing the top element from the stack.

What is stack underflow ?


Performing pop operation when a stack is empty. y When top equals zero.
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

Consider the following program :-

# include <stdio.h> # include <conio.h> void main() { int a=3; clrscr(); printf(%d %d%d%d,a++,++a,++a,a++); getch(); }

Expected o/p. 3566

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

Stack is one of the efficient way to implement discipline to system.

Thank You.

You might also like