当前位置:网站首页>栈的基本操作(C语言实现)
栈的基本操作(C语言实现)
2022-06-28 01:56:00 【Brant_zero】
期末终于结束,接下来就会继续更新数据结构相关的内容了,本篇文章带来数据结构——栈,来实现它的基本功能。
难度偏低,因为之前我们已经实现过顺序表了,栈不过是顺序表的一种特殊形式。
一、 栈的概念与结构
概念:栈是一种特殊的线性表,其只允许在固定的一端插入和删除操作。进行数据插入和删除操作的一端为栈顶,另一端为栈底。栈中的数据元素遵守后进先出(Last In First Out)的原则。
压栈:栈的插入操作叫做进栈/压栈/入栈,即从栈顶插入数据。
出栈:栈的删除操作叫做出栈,即从栈顶删除数据。

二、 栈的框架定义
栈的实现可以使用数组和链表,相对而言数组的结构实现更优,因为数组在尾上插入数据的代价较小,数组的特征也更符合栈的特性。

接下来我们来看看我们栈的类型定义以及我们此次要实现的功能。
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
typedef int STDateType;
typedef struct Stack
{
STDateType* a;
int top; //标识栈顶的数据
int capacity; //动态型 空间不够则扩容
}ST;
void StackInit(ST* ps); //栈的初始化
void StackDestory(ST* ps); //栈的销毁
void StackPush(ST* ps, STDateType x); //栈的插入
void StackPop(ST* ps); //删除数据
STDateType StackTop(ST* ps); //取栈顶的元素
bool StackEmpty(ST* ps); //判断栈是否为空
int StackSize(ST* ps); //得知栈的大小三、 栈的功能实现
3.1 栈的初始化
在我们创建一个自定义栈型的变量时,第一件事即是变量的初始化。
//栈的初始化
void StackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}3.2 栈的销毁
栈销毁相当于栈空间的释放加上对数据的删除,其实与初始化差不多,这里也初始化一同实现。
//栈的销毁
void StackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}3.3 插入数据
栈的关键性功能。首先,判断我们栈是否需要扩容;然后将数据插入到栈顶的位置;最后将栈顶向上移动。
这里会有两种情况:①初始设置栈顶top为0,即Top=0,那我们是先将数据插入,然后再将Top向上移动。②如果我们初始设置栈顶为-1,即Top=-1,那么我们在插入数据的时候要先+1,然后再将数据放入。这里我们采用第一种方式。

//插入数据
void StackPush(ST* ps, STDateType x)
{
assert(ps);
//如果top等于当前的容量,则扩容
if (ps->capacity == ps->top)
{
int NewCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDateType* temp = (STDateType * )realloc(ps->a, sizeof(STDateType) * NewCapacity);
if (temp == NULL)
{
printf("realloc fail");
exit(-1);
}
ps->a = temp;
ps->capacity = NewCapacity;
}
//将值放入栈中。
ps->a[ps->top] = x;
//Top向上移动
ps->top++;
}3.4 删除数据
删除数据就非常简单了,数组的天然优势,直接将栈顶向下移动一个单位即可。
void StackPop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
ps->top--;
}3.5 取栈顶元素
STDateType StackTop(ST* ps)
{
assert(ps);
assert(!StackEmpty(ps));
return ps->a[ps->top - 1];
}3.6 栈的判空
我们这里不用使用if语句判断,直接返回ps->top==0这句代码,会自动判断其真假。如果top==0,则直接会返回true,简洁、方便。
bool StackEmpty(ST* ps)
{
assert(ps);
//直接返回表达式。
return ps->top == 0;
}3.7 栈的大小
int StackSize(ST* ps)
{
assert(ps);
return ps->top;
}四、功能展示
这里我们来尝试插入数据并将其全部输出出来,这些会用到我们上面的全部功能,以来检测这些功能是否实现成功。
这里注意一点,栈是不能遍历的,如果我们想打印栈中的数据,则要打印一个栈顶元素,然后将其弹出,直到栈为空则停止操作。

五、 总结
对于栈我们要领会其功能和特征,其中一些细节我们不必太在乎,我们在乎的主要是它的结构和特性。
栈的使用是非常广泛的,这里我们使用数组实现栈,是因为数组的特点很利于实现栈的特性,如果之前有实现过顺序表的兄弟会发现栈的实现和顺序表非常类似,因为栈本身就是一种特殊的顺序表类型。如果大家有时间可以去尝试用链表实现栈,在之前实现过单链表来说的话,也不算太难。
好的,本篇文章就到此结束了,感想大家的阅读,下篇文章会带来队列的基本功能实现,我们下期再见。
边栏推荐
- Simple elk configuration to realize production level log collection and query practice
- Raspberry pie - environment settings and cross compilation
- 论文阅读:Generative Adversarial Transformers
- 微信小程序中生成二维码
- Gateway microservice routing failed to load microservice static resources
- 树莓派-环境设置和交叉编译
- Heartless sword Chinese English bilingual poem 004 Sword
- 读书,使人内心宁静
- Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
- Why are so many people keen on big factories because of the great pressure and competition?
猜你喜欢

拾光者,云南白药!

The first in the industry! MOS sub evaluation model for subjective video quality experience that can run on mobile devices!
![[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code](/img/ef/a26127284fe57ac049a4313d89cf97.png)
[today in history] June 25: the father of notebook was born; Windows 98 release; First commercial use of generic product code

Online JSON to plaintext tool

一位博士在华为的22年(干货满满)
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper

将PCAP转换为Json文件的神器:joy(安装篇)
![[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics](/img/91/d7d6137b95f6348f71692164614340.png)
[today in history] June 12: the United States entered the era of digital television; Mozilla's original developer was born; 3com merges with American Robotics

JDBC and MySQL databases

2021年软件测试工具总结——模糊测试工具
随机推荐
Interview: how do lists duplicate objects according to their attributes?
导致系统性能失败的十个原因
初始线性回归
Apache——阿帕奇簡介
Built in functions for MySQL database operations
Tips for visiting the website: you are not authorized to view the recovery method of this page
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on
[522. longest special sequence II]
Artifact for converting pcap to JSON file: joy (installation)
The horizontal scrolling recycleview displays five and a half in one screen, which is lower than the five average distributions
adb双击POWER键指令
Why are so many people keen on big factories because of the great pressure and competition?
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
[postgraduate] bit by bit
Usage differences between isempty and isblank
Opencv -- geometric space transformation (affine transformation and projection transformation)
Is online stock investment exchange group safe? Is it reliable to open an account for free?
JDBC and MySQL databases
Gateway微服務路由使微服務靜態資源加載失敗
分布式事务TCC浅析