当前位置:网站首页>C语言栈实现
C语言栈实现
2022-06-23 03:55:00 【pythoncjavac++】
栈有关概念
“栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。”
应为看栈,大家应该都学过了链表,所以栈就比较简单,在这里就不讲解,就直接给大家看实现的代码了!!!
创建栈
typedef int STDataType;
typedef struct strack
{
STDataType* a;
int top;
int capacity;
}ST;这里使用了(typedef int SLTDataType;)这是为了到时栈里存的不是int类型,方便修改
栈的初始化
void StrackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}栈的插入
void StrackPush(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 StrackPop(ST* ps)
{
assert(ps);
assert(!StrackEmpty(ps));
ps->top--;
}返回栈顶元素
STDataType StrackTop(ST* ps)
{
assert(ps);
assert(!StrackEmpty(ps));
return ps->a[ps->top - 1];
}判断栈是否为空
bool StrackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}栈中元素的个数
int StrackSize(ST* ps)
{
assert(ps);
return ps->top - 1;
}栈的销毁
void StrackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}边栏推荐
- Go learning record II (window)
- STP summary
- [laravel series 7.8] broadcasting system
- Arduino flame sensor (with code)
- Post processing of multisensor data fusion using Px4 ECL
- Getting started with the shutter AppBar
- Difficult to find a job in a bad environment? Ali on three sides. Fortunately, he has made full preparations and has offered
- 3 天完成小学期项目,手把手教你完成天气播报系统!
- UI自动化定位利器-xpath实战
- OSPF分流实验
猜你喜欢

rtklib新版本2.4.3 b34测试对比

【C语言】关键字

李宏毅《机器学习》丨5. Tips for neural network design(神经网络设计技巧)

Dolphin scheduler 2.0.5 task test (spark task) reported an error: container exited with a non zero exit code 1

The propeller framework v2.3 releases the highly reusable operator library Phi! Restructure development paradigm to reduce cost and increase efficiency

Jetpack Compose 从开门到入门之 MenuBar桌面菜单(Desktop Menu)

HCIP 交换机实验

dolphinscheduler 1.2.1 数据迁移到 dolphinscheduler 2.0.5方法及迁移后数据测试记录

MVC三層架構

百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
随机推荐
MVC three-tier architecture
Cookie-Session讲解
1183. electricity
In unity, how to read and write a scriptableobject object in editor and runtime
insert into... where not exists插入避免重复的使用
气象绘图软件Panoply使用教程 (不定时更新)
微信小程序:爱情保证书制作生成
UI automation positioning edge -xpath actual combat
Parameter passing of 18 generator function
Swiftui 2.0 course notes Chapter 4
985 test engineer is hanged. Who is more important in terms of education and experience?
功能测试人员如何做到花一个月的时间进阶自动化软件测试工程师
Course design C for freshmen -- clothing management system
servlet自学笔记
飞桨框架v2.3发布高可复用算子库PHI!重构开发范式,降本增效
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
3 天完成小学期项目,手把手教你完成天气播报系统!
This markdown artifact will be charged!
MySQL stored procedure
精密星历介绍与使用