Stack | C Program | Data Structure |

STACK operation in Data structure: 
 

Code:

 

#include<stdio.h>
#define max 5    /*maximum 5 elements*/
int top=-1;
int stk[max];
void push(int);
void display();
int pop();
int item;
void main()
{
 int x; 
 while(1)
 {
  printf("Choose any option:\n1.push\n2.pop\n3.Display\n\n TOP=%d \n\n",top+1);
  scanf("%d",&x);
  switch(x)
  {
   case 1:
    printf("Insert a value\n");
    scanf("%d",&item);
    push(item);
    break;
   case 2: 
    {
     if(top!=(-1))
     {
      printf("The element you pop is %d\n",pop());
                                  break;
     }
     else
     {
      printf("Underflow\n");
      break;
     }
    }
   case 3:
    if(top!=(-1))
     {
      printf("The elements are:\n");
      display();
      break;
     }
    else
               printf("stack is NULL!!\n");
               break; 
   
   default:
    break; 
  }
 }
}
void push(item)
{
 if(max-1==top)
  printf("Stack is overflow!\n");
 else
 {
  top=top+1;
  stk[top]=item;
 }
}
void display()
{
 int i;
 for(i=top;i>=0;i--)
  printf("%d\n",stk[i]);
}
int pop()
{
 int t;
 t=stk[top];
    top=top-1;
    return t;
}

Output:



Comments

Post a Comment

Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)

Popular posts from this blog

Print name in a pattern | name as abbreviation | C-Program

Print Fibonacci Series using Recursion | C program :