1 Star 2 Fork 0

钦某/c-language-learning

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
stack.c 1.04 KB
Copy Edit Raw Blame History
aaa authored 2024-05-06 22:16 +08:00 . .
#define _CRT_SECURE_NO_WARNINGS 1
#include"stack.h"
void STInit(ST* pst)
{
assert(pst);
pst->capacity = 0;
pst->top = 0; //指向栈顶数据的下一个位置
pst->data = NULL;
}
void STDestroy(ST* pst)
{
assert(pst);
free(pst->data);
pst->data = NULL;
pst->capacity = pst->top = 0;
}
void STPush(ST* pst, STDataType x)
{
assert(pst);
if (pst->capacity == pst->top)
{
int newcapacity = pst->capacity == 0 ? 4 : pst->capacity * 2;
STDataType* tmp = (STDataType*)realloc(pst->data, sizeof(STDataType) * newcapacity);
if (tmp == NULL)
{
perror("realloc is fail");
return;
}
pst->data = tmp;
pst->capacity = newcapacity;
//free(tmp); //因为pst和tmp的指向是一样的,不能释放掉开辟的空间,会导致野指针
}
pst->data[pst->top] = x;
pst->top++;
}
void STPop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
pst->top--;
}
STDataType STTop(ST* pst)
{
assert(pst);
assert(pst->top > 0);
return pst->data[pst->top - 1];
}
bool STEmpty(ST* pst)
{
assert(pst);
return !pst->top;
}
int STSize(ST* pst)
{
assert(pst);
return pst->top;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/wang-qin928/c-language-learning.git
git@gitee.com:wang-qin928/c-language-learning.git
wang-qin928
c-language-learning
c-language-learning
master

Search