文章目录
- 1. 栈的概念
- 2. 栈的分类
- 3. 栈的实现(数组栈)
- 3.1 接口设计(Stack.h)
- 3.2 接口实现(Stack.c)
- 1)初始化销毁
- 2)栈顶插入删除
- 3)栈顶元素、空栈、大小
- 3.3 完整代码
- Stack.h
- Stack.c
- test.c
- 注意:
- 运行效果
1. 栈的概念
栈:一种特殊的线性表,其只允许在固定的一端进行插入和删除元素操作。 进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。 栈中的数据元素遵守后进先出LIFO(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈,出数据也在栈顶。
遵循的原则是:后进先出。
2. 栈的分类
栈的实现有3种方式:
2.1 数组栈:栈底是数组头,栈顶是数组尾。
2.2 链式栈:
1)双向链表实现: 栈顶可以是尾也可以是头
2)单向链表实现: 栈顶只能是头(开销小)
栈的实现一般可以使用以上方式实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价比较小。
3. 栈的实现(数组栈)
下面将其分为3个模块进行实现Stack.h,Stack.c,test.c
3.1 接口设计(Stack.h)
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType *a;
//这里是可以指向栈顶的,那下面的写法就要做对应修改
int top; //这里则是标识栈顶位置下一个
int capacity;
}ST;
//初始化销毁扩容
void STInit(ST* pst);
void STDestroy(ST* pst);
void CheckCapacity(ST* pst);
//栈顶插入删除
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
//栈顶元素、空栈、大小
STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
3.2 接口实现(Stack.c)
1)初始化销毁
这里需要讨论一些 top
的指向问题:
如果 top
指向栈顶元素位置,则初始化时 top = -1
;这时要push时要先对 top
++后赋值。
如果 top
指向栈顶元素下一个位置,则初始化时 top == 0
;这时要push时要先对 top
赋值后++。
这里采用第二种写法。
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
这里初始化为-1,表示指向栈顶元素
//pst->top = 0;
//这里初始化为0,表示指向栈顶下一个元素
pst->top = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
2)栈顶插入删除
删除这里要判断 top > 0
,和结构定义一致
void STCheckCapacity(ST* pst)
{
if (pst->capacity == pst->top)
{
int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("relloc fail\n");
exit(-1);
}
pst->a = tmp;
pst->capacity = newCapacity;
}
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
STCheckCapacity(pst);
pst->a[pst->top++] = x;
}
void STPop(ST* pst)
{
assert(pst);
//不为空
assert(pst->top > 0);
pst->top--;
}
3)栈顶元素、空栈、大小
在判断空栈有个小技巧:即直接返回判断语句即可,因为他们的结果也是对或错
STDataType STTop(ST* pst)
{
assert(pst);
//不为空
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
1
//if (pst->top == 0)
//{
// return true;
//}
//else
//{
// return false;
//}
2
//return pst->top == 0 ? true : false;
//3
return pst->top == 0;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
3.3 完整代码
Stack.h
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
typedef int STDataType;
typedef struct Stack
{
STDataType *a;
int top; //标识栈顶位置
int capacity;
}ST;
//初始化销毁
void STInit(ST* pst);
void STDestroy(ST* pst);
void STCheckCapacity(ST* pst);
//栈进栈出
void STPush(ST* pst, STDataType x);
void STPop(ST* pst);
//栈顶元素、空栈、大小
STDataType STTop(ST* pst);
bool STEmpty(ST* pst);
int STSize(ST* pst);
Stack.c
#include "Stack.h"
//初始化销毁
void STInit(ST* pst)
{
assert(pst);
pst->a = NULL;
pst->capacity = 0;
这里初始化为-1,表示指向栈顶元素
//pst->top = 0;
//这里初始化为0,表示指向栈顶下一个元素
pst->top = 0;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->a);
pst->a = NULL;
pst->top = pst->capacity = 0;
}
void STCheckCapacity(ST* pst)
{
if (pst->capacity == pst->top)
{
int newCapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->a, newCapacity * sizeof(STDataType));
if (tmp == NULL)
{
perror("relloc fail\n");
exit(-1);
}
pst->a = tmp;
pst->capacity = newCapacity;
}
}
//栈进栈出
void STPush(ST* pst, STDataType x)
{
assert(pst);
STCheckCapacity(pst);
pst->a[pst->top++] = x;
}
void STPop(ST* pst)
{
assert(pst);
//不为空
assert(pst->top > 0);
pst->top--;
}
//栈顶元素、空栈、大小
STDataType STTop(ST* pst)
{
assert(pst);
//不为空
assert(pst->top > 0);
return pst->a[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
1
//if (pst->top == 0)
//{
// return true;
//}
//else
//{
// return false;
//}
2
//return pst->top == 0 ? true : false;
//3
return pst->top == 0;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
test.c
#include "Stack.h"
void STTest()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
STPush(&s, 5);
STPush(&s, 6);
STPush(&s, 7);
STPush(&s, 8);
STPush(&s, 9);
printf("栈顶元素是 %d, 栈个数为 %d\n", STTop(&s), STSize(&s));
//访问栈只能先打印栈顶,然后出栈,然后继续访问
//入栈顺序和出栈顺序是一对多的关系
printf("打印栈全部元素: \n");
while (!STEmpty(&s))
{
printf("%d ", STTop(&s));
STPop(&s);
}
printf("\n");
printf("销毁栈\n");
STDestroy(&s);
}
int main()
{
STTest();
return 0;
}
注意:
1)访问栈只能先打印栈顶,然后出栈,然后继续访问,访问完成栈也就为空了
2)入栈顺序和出栈顺序是一对多的关系,判断顺序可以模拟过程就能判断哪种顺序是错的