Stack Applications in Data structure

22
Stack Applications Stack Applications

Transcript of Stack Applications in Data structure

Page 1: Stack Applications in Data structure

Stack ApplicationsStack Applications

Page 2: Stack Applications in Data structure

What is stack ?What is stack ?

Stack is a special type of data structure.

Compared to a container.Last In First Out.

Page 3: Stack Applications in Data structure

What is push ?What is push ?Inserting a element into stack.

Page 4: Stack Applications in Data structure

What is stack overflow ?What is stack overflow ?Pushing elements when stack is

full.When top exceeds size of stack .

Page 5: Stack Applications in Data structure

Algorithm pushAlgorithm pushProcedure Push (Value)If TOP is equal to MAX Output an error that the Stack is fullElseAdd 1 to TOPPut data in Value into TOP positionEnd IfEnd Procedure

Page 6: Stack Applications in Data structure

What is pop ?What is pop ?Removing the top element from

the stack.

Page 7: Stack Applications in Data structure

What is stack underflow ?What is stack underflow ?Performing pop operation when a

stack is empty.When top equals zero.

Page 8: Stack Applications in Data structure

Algorithm popAlgorithm popProcedure PopIf TOP is equal to Zero thenStack is empty “Underflow”ElseOutput value from Stack at TOP position Subtract 1 from TOPEnd IfEnd Procedure

Page 9: Stack Applications in Data structure

Stack Applications

Page 10: Stack Applications in Data structure

Applications of stackApplications of stack

Start up & Shut down.Function calling.

Argument passing in c.

Page 11: Stack Applications in Data structure

System StartupSystem Startup

Page 12: Stack Applications in Data structure
Page 13: Stack Applications in Data structure

System ShutdownSystem Shutdown

Page 14: Stack Applications in Data structure
Page 15: Stack Applications in Data structure

Function callingFunction calling

Page 16: Stack Applications in Data structure

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();}

Page 17: Stack Applications in Data structure

OutputOutput

Main startedOne startedTwo startedThree startedThree endedTwo endedOne endedMain ended

Page 18: Stack Applications in Data structure

Argument passing in CArgument 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.

3 5 6 6

Page 19: Stack Applications in Data structure

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.

Page 20: Stack Applications in Data structure

3

5

6

6

%d %d %d %d

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.

Page 21: Stack Applications in Data structure

ConclusionConclusionStack is one of the efficient way

to implement discipline to system.

Page 22: Stack Applications in Data structure

Thank You.Thank You.