RamAllu401
12/12/2019 - 5:21 AM

stack implementation in arrays

#include<stdio.h>
#define MAXSIZE 101
int A[MAXSIZE];
//if top=-1 implicitly saying that the stack is empty
int top=-1;

//function declerations
void push(int);
void pop(void);
int Top(void);
int IsEmpty(void);
void print(void);

int IsEmpty(void)
{
  if(top == -1)
  {
    return 1;
  }
  else
  {
    return 0;
  }
}

int Top(void)
{
  if(top == -1)
  {
    printf("ERROR : THE STACK IS EMPTY\n");
    return 0;
  }
  return A[top];
}

void pop(void)
{
  if(top == -1)
  {
    printf("ERROR : THE STACK IS EMPTY\n");
    return;
  }
  top--;
}

void push(int data)
{
  if(top == MAXSIZE-1)
  {
    printf("ERROR : THE STACK IS FULL\n");
    return;
  }
  A[++top] = data;
}
 void print(void)
 {
   int i;
   printf("Stack :");
   for(i=0;i<=top;i++)
   {
     printf("  %d",A[i]);
   }
   printf("\n");
 }

int main(void)
{
  push(9);
  print();
  push(4);
  print();
  push(8);
  print();
  pop();
  print();
  push(1);
  print();
  pop();
  print();
}