// 支持动态增长的栈
typedef int STDataType;//对数据类型重命名,方便后期修改类型
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;//定义结构同时重命名
需要注意的是:top的指向应该始终保持一致性
1.如果top指向栈顶元素,初始不能为0,应该指向-1
2.如果top初始为0,其应该指向栈顶元素的下一个元素
对应的判定栈满和栈空有所不同
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
判空 判断是否需要扩容(top和capacity相等) 扩容步骤: 空间二倍增长 ,更新数组指针和容量 数据插入到top位置,top位置++
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
//判断是否需要扩容
if (ps->top == ps->capacity)
{
int newcapa = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapa);
if (tmp == NULL)
{
perror("realloc\n");
exit(1);
}
ps->a = tmp;
ps->capacity = newcapa;
}
//确定空间足够之后再插入数据
ps->a[ps->top] = data;
ps->top++;
}
(该方法对于栈只存在一个元素的情况也可以正确处理)
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top);
ps->top--;
}
注意: 即使函数只有一两条语句也还是建议封装成函数,这样可以提高程序的可维护性和可读性
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top);
return ps->a[ps->top-1];
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
// 支持动态增长的栈
typedef int STDataType;//对数据类型重命名,方便后期修改类型
typedef struct Stack
{
STDataType* a;
int top; // 栈顶
int capacity; // 容量
}Stack;//定义结构同时重命名
// 初始化栈
void StackInit(Stack* ps);
// 入栈
void StackPush(Stack* ps, STDataType data);
// 出栈
void StackPop(Stack* ps);
// 获取栈顶元素
STDataType StackTop(Stack* ps);
// 获取栈中有效元素个数
int StackSize(Stack* ps);
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps);
// 销毁栈
void StackDestroy(Stack* ps);
#include"stack.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
//判断是否需要扩容
if (ps->top == ps->capacity)
{
int newcapa = ps->capacity == 0 ? 4 : 2 * (ps->capacity);
STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newcapa);
if (tmp == NULL)
{
perror("realloc\n");
exit(1);
}
ps->a = tmp;
ps->capacity = newcapa;
}
//确定空间足够之后再插入数据
ps->a[ps->top] = data;
ps->top++;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps);
assert(ps->top);
ps->top--;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(ps->top);
return ps->a[ps->top-1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
return ps->top == 0;
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
#include"stack.h"
void test1()
{
Stack st ;
StackInit(&st);
if (StackEmpty(&st))
{
printf("栈空\n");
}
else
{
printf("栈非空\n");
}
StackPush(&st, 1);
StackPush(&st, 2);
StackPush(&st, 3);
StackPush(&st, 4);
if (StackEmpty(&st))
{
printf("栈空\n");
}
else
{
printf("栈非空\n");
}
printf("栈中元素个数:%d\n", StackSize(&st));
printf("%d\n", StackTop(&st));
StackPop(&st);
printf("%d\n", StackTop(&st));
StackPop(&st);
printf("%d\n", StackTop(&st));
StackPop(&st);
printf("%d\n", StackTop(&st));
StackPop(&st);
if (StackEmpty(&st))
{
printf("栈空\n");
}
else
{
printf("栈非空\n");
}
StackDestroy(&st);
}
int main()
{
test1();
return 0;
}