当前位置:网站首页>C language stack implementation
C language stack implementation
2022-06-23 05:18:00 【pythoncjavac++】
Stack related concepts
“ Stack (stack) Also known as the stack , It's an operationally constrained linear table . A linear table limited to insert and delete operations only at the end of the table . This end is called the top of the stack , relatively , Call the other end the bottom of the stack . Inserting a new element into a stack is also called a push 、 Push or push , It's putting a new element on top of the top of the stack , Make it the new top element ; Removing an element from a stack is also called making a stack or backing a stack , It removes the top element of the stack , Make its adjacent elements the new top of the stack .”
Expected stack view , Everyone should have learned the linked list , So the stack is relatively simple , I won't explain it here , Let's just show you the implementation code !!!
Create a stack
typedef int STDataType;
typedef struct strack
{
STDataType* a;
int top;
int capacity;
}ST;It's used here (typedef int SLTDataType;) This is to save in the stack at that time int type , Easy to modify
Initialization of stack
void StrackInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = ps->capacity = 0;
}Stack insertion
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++;
}Stack deletion
void StrackPop(ST* ps)
{
assert(ps);
assert(!StrackEmpty(ps));
ps->top--;
}Back to top of stack element
STDataType StrackTop(ST* ps)
{
assert(ps);
assert(!StrackEmpty(ps));
return ps->a[ps->top - 1];
}Judge whether the stack is empty
bool StrackEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}The number of elements in the stack
int StrackSize(ST* ps)
{
assert(ps);
return ps->top - 1;
}Destruction of the stack
void StrackDestory(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}边栏推荐
- Object structure diagram, which can quickly illustrate the internal structure of an object
- HCIP 作业 BGP总结
- Post processing of multisensor data fusion using Px4 ECL
- (IntelliJ)插件一 Background Image Plus
- insert into... where not exists插入避免重复的使用
- bat中获取bat命令结果
- BGP second test
- Hcip fifth operation
- [laravel series 7.8] broadcasting system
- Economic development is driven by new technology
猜你喜欢

Hard core, become a high-quality tester: learn to communicate with products

MVC三層架構
![[OFDM communication] simulation of OFDM multi-user resource allocation based on MATLAB [including Matlab source code 1902]](/img/ad/91a81c7f413484a86adcff8fc84e3b.jpg)
[OFDM communication] simulation of OFDM multi-user resource allocation based on MATLAB [including Matlab source code 1902]

UI automation positioning edge -xpath actual combat

Three methods of GNSS velocity calculation

rtklib新版本2.4.3 b34测试对比

GNSS速度解算的三种方法

Raspberry pie network remote access

JSP entry notes

MVC三层架构
随机推荐
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
大環境不好難找工作?三面阿裏,幸好做足了准備,已拿offer
MVC三層架構
大环境不好难找工作?三面阿里,幸好做足了准备,已拿offer
Complete the primary school project in 3 days, and teach you to complete the weather broadcast system hand in hand!
MMDeploy快速安装及使用说明
Learn to draw Er graph in an article
硬核,成为高素质测试人员:学会和产品沟通需求
掌握 Shell,一篇就够了!
Visual display of TEQC quality analysis results using teqcplot
rtklib2.4.3 b34的一个与编译器有关的bug
In unity, how to read and write a scriptableobject object in editor and runtime
Beyond chips and AI, why is hard technology capital becoming more and more "hard core"?
【C语言】关键字
Difficult to find a job in a bad environment? Ali on three sides. Fortunately, he has made full preparations and has offered
Official download and installation of QT and QT vs tools plug-ins
BGP第二次试验
OSPF shunt test
BGP second test
使用teqcplot对teqc 质量分析结果进行可视化展示