文章目录
一、什么是栈
栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶;
出栈:栈的删除操作叫做出栈。出数据也在栈顶.
栈就像是一堆摞起来的书,拿到新书只能把它放在书堆的最上面,取书时也只能从最上面开始取。
例题:
一个栈的入栈序列为ABCDE,则不可能的出栈序列为( )
A.ABCDE
B.EDCBA
C.DCEBA
D.ECDBA
A选项,我们可以入一个出一个:入A->出A->入B->出B->入C->出C->入D->出D->入E->出E
B选项,入完再出:入A->B->C->D->E->出E->D->C->B->A
C选项,入A->入B->入C->入D->出D->出C->入E->出E->出B->出A
而对于D选项,是不可能再出完E后直接出C的,必须先出D,所以这题选B。
二、栈的实现
栈的实现一般可以使用数组或者链表实现,用数组实现的栈,我们叫作顺序栈,用链表实现的栈,我们叫作链式栈。相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
这里我们采用顺序栈。
2.1 栈的结构
由于我们不知道链表中存储数据的类型,所以我们能将数据类型重命名,方便修改。
typedef int STDataType;
因为我们使用的是顺序栈,所以一个STDataType*
类型的指针,top
指向栈顶数据的下一个位置,capacity
表示容量大小。
typedef struct Stack
{
STDataType* a;//存储元素
int top; //top 指向栈顶数据的下一个位置
int capacity;//容量
}Stack;
2.2 栈的几个功能
- 初始化栈
- 入栈
- 出栈
- 获取栈顶元素
- 获取栈中有效元素个数
- 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
- 销毁栈
分别由以下函数来实现:
- void StackInit(Stack* ps);
- void StackPush(Stack* ps, STDataType data);
- void StackPop(Stack* ps);
- STDataType StackTop(Stack* ps);
- int StackSize(Stack* ps);
- int StackEmpty(Stack* ps);
- void StackDestroy(Stack* ps);
2.2.1 初始化栈
注意,此时top
指向栈顶数据的下一个位置。
如果想让
top
指向栈顶元素,top
需要初始化为-1,因为当top++之后就指向下一个位置了,top
初始化为-1就要先++再赋值,此时top
指向栈顶的元素。
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
2.2.2 销毁栈
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
2.2.3 入栈
特殊情况:当容量满时(top == capacity
),需要进行扩容,使用realloc
函数。
我们可以定义一个newCapacity
,当原来的容量为0时,初始化为4,这时realloc
的作用相当于malloc
,当原来的容量为非0时,扩容两倍。
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a,newCapacity*sizeof(STDataType));
if (tmp == NULL)//扩容失败
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
//入栈操作
ps->a[ps->top] = data;
ps->top++;
}
2.2.4 判空
检测栈是否为空,如果为空返回非零结果,如果不为空返回0。
只需要判断top
的值即可。
int StackEmpty(Stack* ps)
{
assert(ps);
if (ps->top == 0)
return 1;
else return 0;
}
2.2.5 出栈
相比于入栈,出栈就显得尤其简单,但要注意特殊情况(栈为空)。
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
2.2.6 获取栈顶元素
top指向栈顶元素的下一个,需要-1。
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
2.2.7 获取栈中有效元素个数
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}
三、代码
3.1 Stack.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType* a;//存储元素
int top; //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);
3.2 Stack.c
#include "Stack.h"
void StackInit(Stack* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackDestroy(Stack* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void StackPush(Stack* ps, STDataType data)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a,newCapacity*sizeof(STDataType));
if (tmp == NULL)//扩容失败
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newCapacity;
}
ps->a[ps->top] = data;
ps->top++;
}
int StackEmpty(Stack* ps)
{
assert(ps);
if (ps->top == 0)
return 1;
else return 0;
}
void StackPop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}
STDataType StackTop(Stack* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}
int StackSize(Stack* ps)
{
assert(ps);
return ps->top;
}