目录
栈的概念:栈是一种特殊的线性表,其只允许在固定的一端进行插入和删除元素,进行数据的插入和删除的一端称为栈顶,另一端称为栈底,栈中的数据元素遵循后进先出(LIFO)或者先进后出的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。
出栈:栈的删除操作叫做出栈,出数据也在栈顶。
栈的结构:

要实现栈无非就两种结构:数组和链表。
但是用数组来实现栈是更好的,因为栈是在栈顶进行插入删除的,而顺序表的尾插和尾删效率是非常高的,并且CPU高速缓存利用率也高。
- typedef int STDataType;
- typedef struct Stack
- {
- STDataType* a;
- int top;
- int capacity;
- }ST;
top是用来标识栈顶的位置,当然我这里是实现的动态栈,因为实现静态栈跟用链表来实现栈一样不太好。
- //初始化栈
- void StackInit(ST* ps);
-
- //销毁栈
- void StackDestroy(ST* ps);
-
- //进栈
- void StackPush(ST* ps, STDataType x);
-
- //出栈
- void StackPop(ST* ps);
-
- //获取栈顶元素
- STDataType StackTop(ST* ps);
-
- //判空
- bool StackEmpty(ST* ps);
-
- //栈的元素个数
- int StackSize(ST* ps);
- void StackInit(ST* ps)
- {
- assert(ps);
- ps->a = NULL;
- ps->top = 0;
- ps->capacity = 0;
- }
这个就没什么好说的了。重点在后面。
首先要判断一下空间够不够,不够就扩容,原理和顺序表的一样。相信熟悉顺序表的你对这个应该是信手拈来。但是插入数据也有两种情况
第一种:
先插入后加加
第二种:
先加加后插入

代码实现:
- void StackPush(ST* ps, STDataType x)
- {
- assert(ps);
- if (ps->top == ps->capacity)
- {
- int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)* newcapacity);
- if (tmp == NULL)
- {
- printf("realloc fail\n");
- exit(-1);
- }
- ps->a = tmp;
- ps->capacity = newcapacity;
- }
- ps->a[ps->top] = x;
- ps->top++;
- }
出栈就简单了,只需要将top--就好了,但是在此之前不要忘记判断一下栈是否为空。
- void StackPop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
- ps->top--;
- }
只需要判断栈顶位置为不为0即可。
- bool StackEmpty(ST* ps)
- {
- assert(ps);
- return ps->top == 0;
- }
我们是用数组实现栈,所以我们可以用下标访问栈顶元素。 可别忘了top-1才是栈顶元素的下标。
- STDataType StackTop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
- return ps->a[ps->top - 1];
- }
top值就是栈的元素个数,所以只需返回top值就行。
- int StackSize(ST* ps)
- {
- assert(ps);
- return ps->top;
- }
- void StackDestroy(ST* ps)
- {
- assert(ps);
- free(ps->a);
- ps->a = NULL;
- ps->top = ps->capacity = 0;
- }
- #pragma once
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <assert.h>
-
- 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 x);
-
- //出栈
- void StackPop(ST* ps);
-
- //获取栈顶元素
- STDataType StackTop(ST* ps);
-
- //判空
- bool StackEmpty(ST* ps);
-
- //栈的元素个数
- int StackSize(ST* ps);
- #define _CRT_SECURE_NO_WARNINGS 1
- #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)
- {
- int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
- STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType)* newcapacity);
- if (tmp == NULL)
- {
- printf("realloc fail\n");
- exit(-1);
- }
- ps->a = tmp;
- ps->capacity = newcapacity;
- }
- ps->a[ps->top] = x;
- ps->top++;
- }
-
- void StackPop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
- ps->top--;
- }
-
- STDataType StackTop(ST* ps)
- {
- assert(ps);
- assert(!StackEmpty(ps));
- return ps->a[ps->top - 1];
- }
-
- bool StackEmpty(ST* ps)
- {
- assert(ps);
- return ps->top == 0;
- }
-
- int StackSize(ST* ps)
- {
- assert(ps);
- return ps->top;
- }
总体来说,学习了顺序表之后,栈的实现还是很简单的,无非就是在顺序表的基础上变了下形。
觉得内容有用的话,就给博主三连吧!!!
