当前位置:网站首页>Sequential storage of stacks
Sequential storage of stacks
2022-07-07 05:59:00 【Wukong doesn't buy vegetables anymore】
Stack (stack):

Let's talk about several concepts and common operations of stack :
Design idea of stack :
Now let's talk about the principle of stack sequential storage :

Specific code design ideas :
Don't talk much , Go straight to the code :
stack.h:
#ifndef _STACK_H_
#define _STACK_H_
#define EMPTY_INDEX -1 // There are no elements in the stack
#define MAX_SIZE 100 // The design here can be larger , Prevent data overflow
// Define abstract data storage types
typedef int element_type;// If you don't want this type in the future, change it from here
typedef struct _t_seq_stack
{
int top_of_index;// The index corner mark at the top of the current stack
element_type array[MAX_SIZE];
}t_seq_stack;
// Create a stack
t_seq_stack *create_stack();
// Judge whether the stack is empty
int is_empty(t_seq_stack *stack);
// Destroy the stack
void destroy_stack(t_seq_stack *stack);
// Theoretically, clear the stack , That is to change the length into -1, But the elements are still stored in the corresponding array space
void make_empty(t_seq_stack *stack);
// Out of the stack , Theoretically, it comes out of the stack , The data still exists in the element
void pop_stack(t_seq_stack *stack);
// Push
void push_stack(t_seq_stack *stack,element_type value);
// Get to the top of the stack
element_type top_stack(t_seq_stack *stack);
#endif
stack.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "stack.h"
// Create a stack
t_seq_stack *create_stack()
{
t_seq_stack *stack = (t_seq_stack*)malloc(sizeof(t_seq_stack));
if(stack != NULL) {
stack->top_of_index = EMPTY_INDEX;
// Initialize this space
memset(stack->array,0,sizeof(element_type));
}
return stack;
}
// Judge whether the stack is empty
int is_empty(t_seq_stack *stack)
{
int res = -1;
if(stack != NULL) {
res = stack->top_of_index == EMPTY_INDEX;
}
return res;
}
// Destroy the stack
void destroy_stack(t_seq_stack *stack)
{
if(stack != NULL) {
free(stack);
}
}
// Theoretically, clear the stack , That is, change the current index to -1, But the elements are still stored in the corresponding array space
void make_empty(t_seq_stack *stack)
{
if(stack != NULL) {
stack->top_of_index = EMPTY_INDEX;
}
}
// Out of the stack , Theoretically, it comes out of the stack , The data still exists in the element
void pop_stack(t_seq_stack *stack)
{
if(stack != NULL) {
stack->top_of_index--;// This is quite a change top The location of
}
}
// Push
void push_stack(t_seq_stack *stack,element_type value)
{
if(stack != NULL) {
stack->array[++stack->top_of_index] = value;
}
}
// Get to the top of the stack
element_type top_stack(t_seq_stack *stack)
{
if(stack == NULL || is_empty(stack)) {
printf(" Access error \n");
} else {
return stack->array[stack->top_of_index];
}
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main() {
element_type value1 = 1;
element_type value2 = 2;
element_type value3 = 3;
t_seq_stack *stack = create_stack();
printf(" The number of elements in the stack :%d\n",stack->top_of_index + 1);// Because from -1 At the beginning
// Insert elements into the stack
push_stack(stack,value1);
push_stack(stack,value2);
push_stack(stack,value3);
printf(" The number of elements in the stack :%d\n",stack->top_of_index + 1);
// Loop out the stack
while(!is_empty(stack)) {
// Then we get the top element , Then keep coming out of the stack
element_type value = top_stack(stack);
printf("%d\n",value);
pop_stack(stack);// Here is to keep reducing the index
}
destroy_stack(stack);
return 0;
}
Execution results :

We can see , When I went in, it was 1 2 3, When I came out 3 2 1.
边栏推荐
- 原生小程序 之 input切換 text與password類型
- SAP Spartacus checkout 流程的扩展(extend)实现介绍
- Pytorch builds neural network to predict temperature
- cf:C. Column Swapping【排序 + 模擬】
- nVisual网络可视化
- Five core elements of architecture design
- Value range of various datetimes in SQL Server 2008
- 【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
- 目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
- JVM命令之 jinfo:实时查看和修改JVM配置参数
猜你喜欢

软件测试面试技巧

Say sqlyog deceived me!

PowerPivot——DAX(函数)

Differences and introduction of cluster, distributed and microservice

一个简单的代数问题的求解

Web authentication API compatible version information

Distributed global ID generation scheme

Realize GDB remote debugging function between different network segments
![[SQL practice] a SQL statistics of epidemic distribution across the country](/img/ba/639a23d87094d24572a69575b565b9.png)
[SQL practice] a SQL statistics of epidemic distribution across the country

Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
随机推荐
深度聚类:将深度表示学习和聚类联合优化
VScode进行代码补全
What is message queuing?
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
Why does the data center need a set of infrastructure visual management system
Reading notes of Clickhouse principle analysis and Application Practice (6)
CMD permanently delete specified folders and files
解决pod install报错:ffi is an incompatible architecture
MySQL performance_ Schema common performance diagnosis query
Distributed global ID generation scheme
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
Win configuration PM2 boot auto start node project
数字IC面试总结(大厂面试经验分享)
Detailed explanation of platform device driver architecture in driver development
On the difference between FPGA and ASIC
Nvisual network visualization
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
Understand the deserialization principle of fastjson for generics
The boss always asks me about my progress. Don't you trust me? (what do you think)
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction