🔑🔑博客主页:阿客不是客
🍓🍓系列专栏:渐入佳境之数据结构与算法
欢迎来到泊舟小课堂
😘博客制作不易欢迎各位👍点赞+⭐收藏+➕关注
栈(Stack)是一种常见的数据结构,它是一种“后进先出”(Last In First Out,LIFO)的数据结构。栈可以看做是一种特殊的线性表,只能在栈顶进行插入和删除操作。栈顶是允许操作的,而栈底是固定的。


当我们了解栈的定义之后,我们就能大概知晓其实现方式无非就是顺序表或者单链表。根据其实现方式,我们又能将栈分为顺序栈与链式栈。

因为单链表头插的效率O(1)明显比尾差O(N)更高,所以我们用单链表实现栈时最好以链表的头为栈顶。如果一定要以尾节点作为栈顶的话,最好以双向链表来实现。本章实现链表栈时以头节点作为栈顶。
顺序栈的声明需要一个指向一块空间的指针a,指向栈顶下一个元素的top,以及标志栈大小的capacity。
- typedef int STDataType;
-
- typedef struct Stack
- {
- STDataType* a;//动态数组
- int top;//栈顶的后一个
- int capacity;//数组大小
- }ST;
当然也有实现top指向当前栈顶元素的,只不过这时top初始化要为-1,这样才能在填入元素时刚好指向栈顶元素。
链式栈的声明只需要一个top指针,以及栈的容量capacity。
- typedef struct SListNode
- {
- STDataType data;
- struct SListNode* next;
- }SListNode;
-
- typedef struct Stack
- {
- SListNode* top;
- int size;
- }Stack;
顺序栈与链式栈的初始化分别与顺序表,链表的初始化大致相同。顺序栈先预先分配一块空间,而链式栈可以先初始为NULL,我们本章节就以顺序栈为例来进行讲解
- void StackInit(ST* ps)
- {
- assert(ps);
- ps->a = NULL;
- ps->top = 0;
- ps->capacity = 0;
- }
- void StackDestroy(ST* ps)
- {
- assert(ps);
- free(ps->a);
- ps->a = NULL;
- ps->top = ps->capacity = 0;
- }
- void StackPush(ST* ps, STDataType x)
- {
- assert(ps);
-
- if (ps->top = ps->capacity)
- {
- STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
- if (tmp == NULL)
- {
- perror("realloc fail");
- }
- ps->a = tmp;
- ps->capacity = newcapacity;
- }
-
- ps->a[ps->top] = x;
- ps->top++;
- }
- void StackPop(ST* ps)
- {
- assert(ps);
- assert(ps->top > 0);
-
- ps->top--;
- }
- STDataType StackTop(ST* ps)
- {
- assert(ps);
- assert(ps->top > 0);
-
- return ps->a[ps->top - 1];
- }
- int StackSize(ST* ps)
- {
- assert(ps);
-
- return ps->top;
- }
- bool StackEmpty(ST* ps)
- {
- assert(ps);
-
- return ps->top == 0;
- }
注意:虽然出栈等操作代码简单,但也需要严格使用函数接口,尽可能避免自己写代码,否则容易造成结构混乱。
- #include
- #include
- #include
- #include
-
- typedef int STDataType;
-
- typedef struct Stack
- {
- STDataType* a;//动态数组
- int top;//栈顶的后一个
- int capacity;//数组大小
- }ST;
-
- // 初始化栈
- void StackInit(ST* ps);
- // 销毁栈
- void StackDestroy(ST* ps);
- // 入栈
- void StackPush(ST* ps, STDataType data);
- // 出栈
- void StackPop(ST* ps);
- // 获取栈顶元素
- STDataType StackTop(ST* ps);
- // 获取栈中有效元素个数
- int StackSize(ST* ps);
- // 检测栈是否为空,如果为空返回非零结果,如果不为空返回0
- bool StackEmpty(ST* ps);
- #include"Stack.h"
-
- void StackInit(ST* ps)
- {
- assert(ps);
- ps->a = NULL;
- ps->top = 0;
- ps->capacity = 0;
- }
-
- void StackDestroy(ST* ps)
- {
- assert(ps);
- free(ps->a);
- ps->a = NULL;
- ps->top = ps->capacity = 0;
- }
-
- void StackPush(ST* ps, STDataType x)
- {
- assert(ps);
-
- if (ps->top = ps->capacity)
- {
- STDataType newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
- if (tmp == NULL)
- {
- perror("realloc fail");
- }
- ps->a = tmp;
- ps->capacity = newcapacity;
- }
-
- ps->a[ps->top] = x;
- ps->top++;
- }
-
- void StackPop(ST* ps)
- {
- assert(ps);
- assert(ps->top > 0);
-
- ps->top--;
- }
-
- STDataType StackTop(ST* ps)
- {
- assert(ps);
- assert(ps->top > 0);
-
- return ps->a[ps->top - 1];
- }
-
- int StackSize(ST* ps)
- {
- assert(ps);
-
- return ps->top;
- }
-
- bool StackEmpty(ST* ps)
- {
- assert(ps);
-
- return ps->top == 0;
- }