The Array implementation of stack has three files Stack.h, Stack.c, main.c. Create three different files one is a .h file,other 2 are .c files. I separate the implementation of the code from the usage thus showing the encapsulation principle involved.
Stack.h
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
typedef enum {true=1,false=0} bool;
void StackClear(void); //clear the stack
void StackPush(int item); //Push item on to the stack
int StackPop(void); //Pop item of the stack
bool StackEmpty(void); //=TRUE if Empty the stack
bool StackFull(void); //=True if Stack is full
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Stack.c
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include"Stack.h"
#define maxsize 100
int TopIndex=-1;
int Array[maxsize];
void StackClear(void)
{
TopIndex=-1;
}
void StackPush(int item)
{
if(StackFull()){printf("Stack is Full");}
Array[++TopIndex]=item;
}
int StackPop(void)
{
if(StackEmpty()){printf("Stack is Empty");return 0;}
return Array[TopIndex--];
}
bool StackFull(void)
{
if(TopIndex>=maxsize){return true;}
else
return false;
}
bool StackEmpty(void)
{
if(TopIndex<0){return true;}
else
{return false;}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
main.c
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h>
#include"Stack.h"
int main(void)
{
char buf[100];int i;
gets(buf);
StackClear();
for ( i = 0 ; buf[i] != 0 ; ++i )
{
if ( !StackFull ( )) // Stack is not Full
{ StackPush ( buf[i] ); }
}
printf("Reversed String:");
while( StackEmpty()==0 )
{ printf("%c",StackPop()); }
printf("\n"); return 0;
}