1.概念
栈是一种遵循先进后出的特殊线性表。
入数据只能从栈顶入,出数据只能从栈顶出。
所以实现栈,通过数组实现最优,因为总是从最后一个拿出数据。
2.代码展示
Stack.h
#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdlib.h>
#include<stdio.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);
Stack.c
#include"Stack.h"
// 初始化栈
void StackInit(Stack* ps)
{
assert(ps);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
// 入栈
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->_capacity == ps->_top)//检查容量,扩容
{
int newcapcity = ps->_capacity == 0 ? 2 : 2 * ps->_capacity;
ps->_a = (STDataType*)realloc(ps->_a,newcapcity*sizeof(STDataType));
ps->_capacity = newcapcity;
}
ps->_a[ps->_top++] = data;
}
// 出栈
void StackPop(Stack* ps)
{
assert(ps && ps->_top>0);
--ps->_top;
}
// 获取栈顶元素
STDataType StackTop(Stack* ps)
{
assert(ps);
return ps->_a[ps->_top-1];
}
// 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->_top;
}
// 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
int StackEmpty(Stack* ps)
{
assert(ps);
if (ps->_top == 0)
{
return 0;
}
else
{
return 1;
}
}
// 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->_a);
ps->_a = NULL;
ps->_capacity = ps->_top = 0;
}
void test()
{
Stack s;
StackInit(&s);
StackPush(&s, 1);
StackPush(&s, 2);
StackPop(&s);
StackPush(&s, 3);
StackPush(&s, 4);
while (StackEmpty(&s) != 0)
{
printf("%d ",StackTop(&s));
StackPop(&s);
}
StackDestroy(&s);
}
test.c
#include"Stack.h"
int main()
{
test();
system("pause");
return 0;
}
3.结果展示
4.心得体会
空栈top为0,注意检查容量。