Saturday, December 19, 2015

Write A Program to implement stack operation PUSH & POP using array.

Solution:-
 
#include<stdio.h>
#include<conio.h>
#include<process.h>
# define maxsize 5
int stack[maxsize];
int tos=-1;
void push( );
void pop( );
void display( );
void main( )
{
        int choice;
        clrscr() ;
        do
        {
                  printf("\n 1.PUSH");
                  printf("\n 2.POP");
                  printf("\n 3.DISPLAY");
                  printf("\n 4.EXIT");
                  printf("\n Enter your choice: \t");
                  scanf("%d",&choice);
                  switch(choice)
                  {
                        case 1:push();
                             break;
                        case 2:pop();
                             break;
                        case 3:display();
                             break;
                        case 4:exit(0);
                             break;
                  }
        }
        while(choice!=4);
}
 
void push()
{
        int item;
        if(tos==maxsize-1)
        printf("\n STACK OVERFLOW");
        else
        {
                printf("\n Enter item: \t");
                scanf("%d",&item);
                tos=tos+1;
                stack[tos]=item;
                printf("\n %d is pushed successfully",item);
        }
}
 
void pop()
{
        int item;
        if(tos==-1)
        printf("\n STACK UNDERFLOW");
        else
        {
                item=stack[tos];
                tos=tos-1;
                printf("\n %d is poped successfully",item);
        }
}
 
void display()
{
         int i;
         if(tos==-1)
         printf("\n NO ITEM");
         else
         {
                    for(i=tos;i>=0;i--)
                    printf("\n %d",stack[i]);
         }
}