Stack Applications
What is stack ?

 Stack is a special type of data
  structure.
 Compared to a container.
 Last In First Out.
What is push ?
   Inserting a element into stack.
What is stack overflow ?
 Pushing elements when stack is full.
 When top exceeds size of stack .
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 ?
   Removing the top element from the
    stack.
What is stack underflow ?
 Performing pop operation when a
  stack is empty.
 When top equals zero.
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 startedn");
printf("Three endedn");
}
void two()
{
printf("Two startedn");
three();
printf("Two endedn");
}
void one()
{
printf("One startedn");
two();
printf("One endedn");
}
void main()
{
clrscr();
printf("Main startedn");
one();
printf("Main endedn");
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   Top
%d %d

 6      Called function ie, printf(…);

        Here the input is taken from stack
 6      So, the data order will be 6 6 5 3.

 5      Therefore the o/p will be 6 6 5 3.

 3
Conclusion
   Stack is one of the efficient way to
    implement discipline to system.
Thank You.

Stack Data Structure & It's Application

  • 1.
  • 2.
    What is stack?  Stack is a special type of data structure.  Compared to a container.  Last In First Out.
  • 3.
    What is push?  Inserting a element into stack.
  • 4.
    What is stackoverflow ?  Pushing elements when stack is full.  When top exceeds size of stack .
  • 5.
    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
  • 6.
    What is pop?  Removing the top element from the stack.
  • 7.
    What is stackunderflow ?  Performing pop operation when a stack is empty.  When top equals zero.
  • 8.
    Algorithm pop Procedure Pop IfTOP 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
  • 9.
  • 10.
    Applications of stack Start up & Shut down. Function calling. Argument passing in c.
  • 11.
  • 13.
  • 15.
  • 16.
    void three() { printf("Three startedn"); printf("Threeendedn"); } void two() { printf("Two startedn"); three(); printf("Two endedn"); } void one() { printf("One startedn"); two(); printf("One endedn"); } void main() { clrscr(); printf("Main startedn"); one(); printf("Main endedn"); getch(); }
  • 17.
    Output Main started One started Twostarted Three started Three ended Two ended One ended Main ended
  • 18.
    Argument passing inC • 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
  • 19.
     But o/p is6 6 5 3 Because the argument are passed from right to left in a stack and then sent to printf function.
  • 20.
    %d %d Top %d %d 6 Called function ie, printf(…); Here the input is taken from stack 6 So, the data order will be 6 6 5 3. 5 Therefore the o/p will be 6 6 5 3. 3
  • 21.
    Conclusion  Stack is one of the efficient way to implement discipline to system.
  • 22.