Wednesday, 28 February 2018

Most Simple Array Stack in C

Here goes the most simple Array Stack program in C:


#include<stdio.h>
#define MAX 50
int stack[MAX],top=-1,i;
void push(int item)
{
    if(top>=MAX) printf("\nStack Overflow!");
    else
        stack[++top]=item;
}
void pop()
{
    if(top==-1) printf("\nStack Underflow!");
    else
        printf("\n%d had been popped out",stack[top--]);
}
void traverse()
{
    if(top==-1) printf("\nNo Elements to display!");
    else
     for(i=0;i<=top;i++)
         printf("\n stack[%d]=%d",i,stack[i]); 
}
void main()
{
  for(i=0;i<50;i++)
     push(i);
    traverse();
    for(i=0;i<50;i++)
     pop();
    traverse();
}

No comments:

Post a Comment