目录
栈的概念及结构
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除 操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out) 的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈。出数据也在栈顶。
栈的实现方式
实现栈有两种方式:1.用数组实现,2.用链表实现。
数组栈
优点:元素进栈或出栈时可以直接用下标来操作。
缺点:当空间不够需要增容时,会带来一定的空间浪费。(数组通常成倍的增容)
链式栈
1.用尾做栈顶:
如果用尾做栈顶,进栈入栈对应尾插尾删,需要设计成双向链表,若设计成单链表,则每次尾插尾删都需要找尾,时间复杂度为O(n),效率低。
2.用头做栈顶:
如果用头做栈顶,进栈入栈对应头插头删,可以设计成单链表。
两种栈的实现都可以,若选择其中一种,数组栈结构稍微好一点。下面以数组的形式来实现栈。
数组栈的实现
先创建一个动态增长栈的结构体:
指针a指向数组栈,top为栈顶,capacity为数组栈的当前容量。
typedef int StackDataType;
typedef struct Stack
{
StackDataType* a;
int top;
int capacity;
}Stack;
下面给出需要实现的接口:
//初始化栈
void StackInit(Stack* ps);
//栈顶插入删除数据
//入栈
void StackPush(Stack* ps,StackDataType x);
//出栈
void StackPop(Stack* ps);
//获取栈顶元素
StackDataType StackTop(Stack* ps);
//获取栈中有效元素个数
int StackSize(Stack* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
//销毁栈
void StackDestroy(Stack* ps);
初始化栈
初始时先开辟一个大小为4个StackDataType的空间
void StackInit(Stack* ps)
{
assert(ps);
ps->a = (StackDataType*)malloc(sizeof(StackDataType) * 4);
if (ps->a == NULL)
{
printf("malloc fail\n");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;
}
入栈
当栈未满时,元素入到下标为top的位置上。
当栈已满,此时top与capacity相等,需要对栈进行增容,这里以每次增容当前容量的2倍为例,然后再将元素入到下标为top的位置上。
void StackPush(Stack* ps, StackDataType x)
{
assert(ps);
//栈满了
if (ps->top == ps->capacity)
{
StackDataType* tmp = (StackDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(StackDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
出栈
对栈顶元素出栈时,只需将栈顶减少 1 ,即让 top 减少 1。
void StackPop(Stack* ps)
{
assert(ps);
//栈空了,调用Pop,直接中止程序报错
assert(ps->top > 0);
ps->top--;
}
获取栈顶元素
由于已有栈顶,故只需返回下标为 top-1 的元素即可。
StackDataType StackTop(Stack* ps)
{
assert(ps);
//栈空了,调用Top,直接中止程序报错
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
检测栈是否为空
如果为空返回非零结果,如果不为空返回0。
bool 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 <stdio.h>
#include <stdbool.h>
#include <assert.h>
#include <stdlib.h>
typedef int StackDataType;
typedef struct Stack
{
StackDataType* a;
int top;
int capacity;
}Stack;
//初始化栈
void StackInit(Stack* ps);
//栈顶插入删除数据
//入栈
void StackPush(Stack* ps,StackDataType x);
//出栈
void StackPop(Stack* ps);
//获取栈顶元素
StackDataType StackTop(Stack* ps);
//获取栈中有效元素个数
int StackSize(Stack* ps);
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool StackEmpty(Stack* ps);
//销毁栈
void StackDestroy(Stack* ps);
void StackInit(Stack* ps)
{
assert(ps);
ps->a = (StackDataType*)malloc(sizeof(StackDataType) * 4);
if (ps->a == NULL)
{
printf("malloc fail\n");
exit(-1);
}
ps->capacity = 4;
ps->top = 0;
}
//入栈
void StackPush(Stack* ps, StackDataType x)
{
assert(ps);
//满了
if (ps->top == ps->capacity)
{
StackDataType* tmp = (StackDataType*)realloc(ps->a, ps->capacity * 2 * sizeof(StackDataType));
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
ps->a = tmp;
ps->capacity *= 2;
}
}
ps->a[ps->top] = x;
ps->top++;
}
//出栈
void StackPop(Stack* ps)
{
assert(ps);
//栈空了,调用Pop,直接中止程序报错
assert(ps->top > 0);
ps->top--;
}
//获取栈顶元素
StackDataType StackTop(Stack* ps)
{
assert(ps);
//栈空了,调用Top,直接中止程序报错
assert(ps->top > 0);
return ps->a[ps->top - 1];
}
//获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
//检测栈是否为空,如果为空返回非零结果,如果不为空返回0
bool 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;
}
测试用例
void TestStack()
{
ST st;
StackInit(&st);
StackPush(&st,1);
StackPush(&st,2);
StackPush(&st,3);
printf("%d\n", StackTop(&st));
StackPop(&st);
printf("%d\n", StackTop(&st));
StackPush(&st,3);
StackPush(&st,4);
StackPush(&st,5);
printf("%d\n", StackTop(&st));
while (!StackEmpty(&st))
{
printf("%d ", StackTop(&st));
StackPop(&st);
}
printf("\n");
StackDestory(&st);
}
int main()
{
TestStack();
return 0;
}