Stack data structure.

■ what is stack?
                            Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first, comes out last.
 
■ Diagram-
          
                 
■  real life example-
                               
                               There are many real-life examples of a stack. Consider an example of plates stacked over one another in the canteen. The plate which is at the top is the first one to be removed, i.e. the plate which has been placed at the bottommost position remains in the stack for the longest period of time. So, it can be simply seen to follow LIFO(Last In First Out)/FILO(First In Last Out) order.

■ Program to implement stack's operation.        using Array=>

#include<stdio.h>
#include<conio.h>
void PUSH();
void POP();
void TRAVERSE();
int stk[20],i,N,item,top=-1;
void main()
{
int choice;
clrscr();
printf("Enter the size of stack less than 20=");
scanf("%d",&N);
printf("\nPress 1 for push.");
printf("\nPress 2 for pop.");
printf("\nPress 3 for traverse.");
printf("\nPress 4 for exit.");
do
{
 printf("\nEnter your choice=");
 scanf("%d",&choice);
 switch(choice)
 {
  case 1:
               PUSH();
               break;
 case 2:
               POP();
               break;
 case 3:
               TRAVERSE();
               break;
 case 4:
               printf("Exit.");                 
               break;
 default:
                printf("wrong choice. ");
 }
}
while(choice!=4);
getch();
}
void PUSH()
{
 if(top==N-1)
 {
  printf("Stack is overflow.");
 }
 else
 {
  printf("Enter the item to be pushed=");
  scanf("%d",&item);
  top=top+1;
  stk[top]=item;
 }
}
void POP()
 {
 if(top==-1)
 {
 printf("Stack is underflow. ");
 }
 else
 {
  item=stk[top];
  printf("the poped element is =%d",item);
  top=top-1;
 }
}
void TRAVERSE()
 {
if(top>=0)
{
 printf("Stack elements are=\n");
 for(i=top; i>=0; i--)
 {
 printf("%d\n",stk[i]);
 }
}
else
{
printf("stack is empty.");
}
}

■ Output-



           


Comments

Popular posts from this blog

Introduction of java Programming language.