#pragma once
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int STpye;
typedef struct Stack {
STpye* a;
int top;//zhan ding
int capacity;
}St;
void StackInit(St* ps);
void StackDestory(St* ps);
void StackPush(St* ps, STpye x);
void StackPop(St* ps);
STpye StackTop(St* ps);
int StackSize(St* ps);
bool StackEmpty(St* ps);
#include"Stack.h"
#include<assert.h>
void StackInit(St* ps)//初始化数据
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackDestory(St* ps) {
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void StackPush(St* ps, STpye x) {
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STpye* tmp = (STpye*)realloc(ps->a, sizeof(STpye) * newcapacity);
if (tmp == NULL) {
printf("不成了");//增容增好
exit(-1);
}
ps->a = tmp;
ps->capacity = newcapacity;
}
//ps->a[ps->top++] = x;
ps->a[ps->top ] = x;
ps->top++;
}
void StackPop(St* ps) {
assert(ps);
assert(ps->top > 0);
ps->top--;
}
STpye StackTop(St* ps) {
assert(ps);
//assert(ps->top >= 0);
return ps->a[ps->top-1];
}
int StackSize(St* ps) {
assert(ps);
return ps->top;
}
bool StackEmpty(St* ps) {
assert(ps);
/*if (ps->top == 0) {
return true;
}
else {
return false;
}*/
return ps->top == 0;
}
#include"Stack.h"
void tee() {
St t;
StackInit(&t);
StackPush(&t, 1);
StackPush(&t, 2);
while (!StackEmpty(&t)) {
printf("%d ", StackTop(&t));
StackPop(&t);
}
StackDestory(&t);
}
int main() {
tee();
return 0;
}