当前位置:网站首页>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;
}边栏推荐
- Implementation of slider view
- In unity, how to read and write a scriptableobject object in editor and runtime
- Automatically add watermark to screenshot
- PRCS-1016 : Failed to resolve Single Client Access Name
- 985 test engineer is hanged. Who is more important in terms of education and experience?
- Arduino火焰传感器(含代码)
- VMware network connection error unit network service not found
- rtklib2.4.3 b34 单点定位的一个bug
- 【Leetcode】最长递增子序列问题及应用
- This markdown artifact will be charged!
猜你喜欢
随机推荐
UI自动化定位利器-xpath实战
树莓派网络远程访问
微信小程序;AI智能配音助手
MVVM has become history, and Google has fully turned to MVI
Three tier architecture experiment
Cookie session explanation
Image noise reduction denoise AI
Beyond chips and AI, why is hard technology capital becoming more and more "hard core"?
计算欧式距离和余弦相似度
MVC three-tier architecture
Missing essential plugin
[C language] keyword
Emergency response HW review
C'est dur de trouver un emploi? Ali des trois côtés, heureusement qu'il s'est bien préparé et qu'il a pris un produit.
图片降噪DeNoise AI
大环境不好难找工作?三面阿里,幸好做足了准备,已拿offer
C语言栈实现
基金业绩评价
[OFDM communication] simulation of OFDM multi-user resource allocation based on MATLAB [including Matlab source code 1902]
rtklib2.4.3 b34 单点定位的一个bug









