Stack.h
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int DATATYPE;
typedef struct Stack
{
DATATYPE* _arr;
int top;
int capicity;
}Stack;
void StackInit(Stack* ps);
void StackPush(Stack* ps,DATATYPE x);
void StackPop(Stack* ps);
DATATYPE StackTop(Stack *ps);
int StackEmpty(Stack* ps);
int StackSize(Stack* ps);
void StackPrint(Stack* ps);
Stack.c
#include"Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->_arr = NULL;
ps->top = 0;
ps->capicity = 0;
}
void StackPush(Stack* ps, DATATYPE x)
{
assert(ps);
int newc = 0;
if (ps->capicity == ps->top)
{
newc = (ps->capicity == 0 ? 10 : 2 * ps->capicity);
ps->_arr = (DATATYPE*)realloc(ps->_arr,newc*sizeof(DATATYPE));
ps->capicity = newc;
}
ps->_arr[ps->top++] = x;
}
void StackPop(Stack* ps)
{
assert(ps);
if (ps->top == 0)
{
return;
}
ps->top--;
}
DATATYPE StackTop(Stack *ps)
{
assert(ps);
return ps->_arr[ps->top - 1];
}
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0 ? 0 : 1;
}
int StackSize(Stack *ps)
{
assert(ps);
return ps->top;
}
void StackPrint(Stack* ps)
{
assert(ps);
while (StackEmpty(ps) == 1)
{
printf("%d ", StackTop(ps));
StackPop(ps);
}
printf("\n");
}
#include"Stack.h"
int main()
{
test();
system("pause");
return 0;
}