当前位置:网站首页>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;
}
边栏推荐
- Policy Center > Deceptive Behavior
- Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0
- In depth analysis of the core code of the gadgetinspector
- Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
- Interview experience of service end test engineer
- 19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
- MySQL8.0开启远程连接权限的方法步骤
- Log4j2 进阶使用
- Policy Center > Malware > Malware
- Unsupported major. minor version 52.0
猜你喜欢
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键
Reptile (1) - Introduction to basic reptile theory
Create a new MySQL database under Linux and import SQL files
Policy Center > Malware > Malware
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
中国传奇教授李泽湘,正在批量制造独角兽
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
Simulation of two-color ball system to judge the winning situation
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
随机推荐
Sword finger offer II 080 Combinatorial backtracking with k elements
How cloudxr promotes the future development of XR
CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
Mysql8 error: error 1410 (42000): you are not allowed to create a user with grant solution
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
Deep understanding Net (2) kernel mode 1 Kernel mode construct event event
BYD is more and more like Huawei?
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
Google Play 索引表
How the edge computing platform helps the development of the Internet of things
The difference between intermodulation and intermodulation
Interview experience of service end test engineer
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Finally understand science! 200 pictures to appreciate the peak of human wisdom
String common API
几百行代码实现一个 JSON 解析器
Map reduce case super detailed explanation
Policy Center-User Data
Swagger's asp Net core web API help page