当前位置:网站首页>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.
边栏推荐
- Web authentication API compatible version information
- Five core elements of architecture design
- 关于STC单片机“假死”状态的判别
- mac版php装xdebug环境(m1版)
- 980. Different path III DFS
- Polynomial locus of order 5
- Interview skills of software testing
- yarn入门(一篇就够了)
- 2pc of distributed transaction solution
- @pathvariable 和 @Requestparam的详细区别
猜你喜欢
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
VScode进行代码补全
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
Differences and introduction of cluster, distributed and microservice
The solution of a simple algebraic problem
Reading notes of Clickhouse principle analysis and Application Practice (6)
如果不知道这4种缓存模式,敢说懂缓存吗?
[SQL practice] a SQL statistics of epidemic distribution across the country
Interview questions and salary and welfare of Shanghai byte
C. colonne Swapping [tri + Simulation]
随机推荐
What EDA companies are there in China?
三级菜单数据实现,实现嵌套三级菜单数据
Five core elements of architecture design
数据中心为什么需要一套基础设施可视化管理系统
JVM命令之 jstat:查看JVM统计信息
Question 102: sequence traversal of binary tree
Message queuing: how to ensure that messages are not lost
@pathvariable 和 @Requestparam的详细区别
cf:C. Column Swapping【排序 + 模拟】
2pc of distributed transaction solution
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
《ClickHouse原理解析与应用实践》读书笔记(6)
C. colonne Swapping [tri + Simulation]
上海字节面试问题及薪资福利
An example of multi module collaboration based on NCF
Bat instruction processing details
The boss always asks me about my progress. Don't you trust me? (what do you think)
R language [logic control] [mathematical operation]
PTA ladder game exercise set l2-004 search tree judgment
Introduction to the extension implementation of SAP Spartacus checkout process