当前位置:网站首页>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.
边栏推荐
- 数字IC面试总结(大厂面试经验分享)
- Things about data storage 2
- [cloud native] what is the microservice architecture?
- [SQL practice] a SQL statistics of epidemic distribution across the country
- Question 102: sequence traversal of binary tree
- Flask1.1.4 Werkzeug1.0.1 源碼分析:啟動流程
- 云加速,帮助您有效解决攻击问题!
- Reading notes of Clickhouse principle analysis and Application Practice (6)
- Nodejs get client IP
- Type de texte de commutation d'entrée et de mot de passe de l'applet natif
猜你喜欢
Detailed explanation of platform device driver architecture in driver development
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
《ClickHouse原理解析与应用实践》读书笔记(6)
如何提高网站权重
JVM命令之 jinfo:实时查看和修改JVM配置参数
老板总问我进展,是不信任我吗?(你觉得呢)
[InstallShield] Introduction
Differences and introduction of cluster, distributed and microservice
Hcip seventh operation
Hcip eighth operation
随机推荐
[daily training -- Tencent selected 50] 292 Nim games
Go language context explanation
Things about data storage 2
980. Different path III DFS
Sidecar mode
原生小程序 之 input切换 text与password类型
SAP ABAP BDC (batch data communication) -018
软件测试面试技巧
What are the common message queues?
What is message queuing?
Storage of dental stem cells (to be continued)
Three level menu data implementation, nested three-level menu data
Value range of various datetimes in SQL Server 2008
往图片添加椒盐噪声或高斯噪声
CMD permanently delete specified folders and files
Hcip eighth operation
线性回归
C. colonne Swapping [tri + Simulation]
MFC BMP sets the resolution of bitmap, DPI is 600 points, and gdiplus generates labels
The 2022 China low / no code Market Research and model selection evaluation report was released