Stack Applications in Data structure
-
Upload
jsaddam709 -
Category
Documents
-
view
3.087 -
download
2
Transcript of Stack Applications in Data structure
Stack ApplicationsStack Applications
What is stack ?What is stack ?
Stack is a special type of data structure.
Compared to a container.Last In First Out.
What is push ?What is push ?Inserting a element into stack.
What is stack overflow ?What is stack overflow ?Pushing elements when stack is
full.When top exceeds size of stack .
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
What is pop ?What is pop ?Removing the top element from
the stack.
What is stack underflow ?What is stack underflow ?Performing pop operation when a
stack is empty.When top equals zero.
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
Stack Applications
Applications of stackApplications of stack
Start up & Shut down.Function calling.
Argument passing in c.
System StartupSystem Startup
System ShutdownSystem Shutdown
Function callingFunction 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();}
OutputOutput
Main startedOne startedTwo startedThree startedThree endedTwo endedOne endedMain ended
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
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.
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.
ConclusionConclusionStack is one of the efficient way
to implement discipline to system.
Thank You.Thank You.