当前位置:网站首页>I implement "stack" with C I
I implement "stack" with C I
2022-06-30 16:30:00 【MT_ one hundred and twenty-five】
Catalog
One 、 Understand the structural characteristics of stack
One 、 Understand the structural characteristics of stack
1、 Stack is a special linear table , Only data is allowed in and out from one end , Called LIFO , First in, then out .
Pressing stack : Stack insertion is called stack entry / Pressing stack / Push , The input data is at the top of the stackOut of the stack : The deletion of the stack is called out of the stack . The output data is also at the top of the stack

Two 、 Concrete realization
Because stack is essentially a linear table , Therefore, it can be realized in two ways : The order sheet or Linked list
Here I use a method similar to the sequence table to realize .
The code is as follows :
typedef char Stacktype;
typedef struct Stack
{
int top;
Stacktype* data;
int capacity;
}Stack;
void Stack_init(Stack* pphead); // Initialization of stack
void Stack_destory(Stack* pphead); // Empty the stack
void Stack_push(Stack* pphead, Stacktype data); // insert data , Pressing stack
void Stack_pop(Stack* pphead); // Out of the stack ( Delete data )
bool Stack_Empty(Stack* pphead); // Judge whether the stack is empty
Stacktype Stack_Top(Stack* pphead); // Call out top of stack element
int Stack_Size(Stack* pphead); // Check the number of data
// Initialization of stack
void Stack_init(Stack* pphead)
{
pphead->top = 0;
pphead->capacity = 0;
pphead->data = NULL;
}
// Empty the stack
void Stack_destory(Stack* pphead)
{
pphead->top = 0;
pphead->capacity = 0;
free(pphead->data);
pphead->data = NULL;
}
// insert data , Pressing stack
void Stack_push(Stack* pphead, Stacktype data)
{
assert(pphead);
if (pphead->top == pphead->capacity)
{
int Newcapacity = (pphead->capacity == 0) ? 4 : ((pphead->top) * 2);
Stacktype* temp = NULL;
temp = (Stacktype*)realloc(pphead->data, sizeof(Stacktype) * Newcapacity);
if (temp == NULL)
{
printf("Stack_push");
exit(-1);
}
pphead->data = temp;
pphead->top = Newcapacity;
}
(pphead->data)[pphead->capacity] = data;
pphead->capacity++;
}
// Out of the stack ( Delete data )
void Stack_pop(Stack* pphead)
{
assert(pphead);
assert(Stack_Empty(pphead));
pphead->capacity--;
}
// Judge whether the stack is empty
bool Stack_Empty(Stack* pphead)
{
assert(pphead);
return pphead->capacity != 0;
}
// Call out top of stack element
Stacktype Stack_Top(Stack* pphead)
{
assert(pphead);
assert(Stack_Empty(pphead));
return pphead->data[pphead->capacity - 1];
}
// Check the number of data
int Stack_Size(Stack* pphead)
{
assert(pphead);
return pphead->top;
}边栏推荐
- There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
- 技不压身,快速入门ETH智能合约开发,带你进入ETH世界
- 云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
- 附加:(还没写,别看~~~)WebMvcConfigurer接口;
- 从第三次技术革命看企业应用三大开发趋势
- Go micro installation
- Interesting research on mouse pointer interaction
- Policy Center-Permissions and APIs that Access Sensitive Information
- Arcmap操作系列:80平面转经纬度84
- 360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
猜你喜欢

KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system

Policy Center > Google Play‘s Target API Level Policy

Generating verification code with sring

ASP. Net core Middleware

Go micro installation
![[leetcode] linked list sorting (gradually increasing the space-time complexity)](/img/0e/b8d0305babb42b6fab85b45c47d705.png)
[leetcode] linked list sorting (gradually increasing the space-time complexity)

How cloudxr promotes the future development of XR

Imeta | Ye Mao / Shi Yu reviewed the dynamic shuttle and ecological function of intracellular and extracellular genes in the environmental microbiome

Policy Center > Device and Network Abuse

Policy Center > Deceptive Behavior
随机推荐
Openresty built in variable
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
ASP. Net core signalr series hub tutorial
Arcmap操作系列:80平面转经纬度84
猎头5万挖我去VC
MySQL master-slave configuration
MicroBlaze serial port learning · 2
24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
波导的种类
“低代码”在企业数字化转型中扮演着什么角色?
There are so many kinds of coupons. First distinguish them clearly and then collect the wool!
IIS无法加载字体文件(*.woff,*.svg)的解决办法
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
[download attached] installation and use of penetration test artifact Nessus
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.
ASP. Send information in sinalr controller of net core
实时渲染和预渲染有什么区别
Create a new MySQL database under Linux and import SQL files
2022新消费半年盘点:行业遇冷,但这九个赛道依然吸金
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例