stack array implementation
#include <stdio.h>
#include <stdlib.h>
/*stack array implementation*/
typedef int ElementType;
ElementType NULLELEMENT= -214748364;
#define MaxSize 15
typedef struct StackNode {
ElementType Data[MaxSize];
int CurrentP;
}Stack_Node;
//create stack
typedef Stack_Node* SP;
void Push(ElementType data,SP sp) {
if (sp->CurrentP==MaxSize-1)
{
printf("Stack is full,double large stack");
//enlarge capacity;
return;
}
sp->CurrentP++;
sp->Data[sp->CurrentP] = data;
}
ElementType Pop(SP sp) {
if (sp->CurrentP==-1)
{
printf("stack is empty");
return NULLELEMENT;
}
return sp->Data[sp->CurrentP--];
}
void ClearStack(SP sp) {
while (sp->CurrentP!=-1)
{
sp->Data[sp->CurrentP--] = 0;
}
}
void showStack(SP sp) {
int tempP=0;
for (tempP;tempP<=MaxSize-1;tempP++)
{
printf(" %d ", sp->Data[tempP]);
printf(" %d ;", tempP);
}
printf("\n");
}
void main()
{
SP sp;
sp = (SP)malloc(sizeof(Stack_Node));
//init Stack
sp->CurrentP = -1;
for (int i=0;i<=MaxSize-1;i++)
{
sp->Data[i] = 0;
}
showStack(sp);
Push(-1,sp);
Push(2, sp);
Push(5, sp);
showStack(sp);
printf("value=%d ", Pop(sp));
printf("value=%d ", Pop(sp));
Push(442, sp);
Push(532, sp);
showStack(sp);
ClearStack(sp);
showStack(sp);
}