当前位置:网站首页>Chain storage of stack
Chain storage of stack
2022-07-07 05:59:00 【Wukong doesn't buy vegetables anymore】
We talked about sequential storage before , make a concrete analysis , Look at the sequential storage of stacks , Now let's talk about the operation with linked list :
When storing sequentially , I use a cursor top_of_index To control top Corner marker , So how to control the top of the stack in a single linked list ,
Just keep plugging in , Head inserting is inserting at the top of the stack .
What about the stack , The stack is nothing more than to operate from scratch , Because the head is the top of the stack , We will destroy this node directly ( The premise is that this node allocates memory space ) Or the next node that points to the first node is ok 了 .
in addition , About the insertion and deletion of single linked list , Please see the design of single linked list , In addition, for some common operations and sequential storage, please see my other article .
In a word? , The general design idea is just four words : Start from scratch .
Don't talk much , Code up :
linkstack.h
1 #ifndef _LINKLIST_H_
2 #define _LINKLIST_H_
3
4 // The element type is still controlled in this
5 typedef int element_type;
6
7 typedef struct _t_link_stack link_stack;
8
9 struct _t_link_stack{
10 link_stack *next;// Pointer to the next node
11 element_type value;
12 };
13
14 link_stack *create_stack();
15 int is_empty(link_stack *t_stack);
16 void destroy_stack(link_stack *t_stack);
17 element_type top_stack(link_stack *t_stack);
18 void pop_stack(link_stack *t_stack);
19 void push_stack(link_stack *t_stack,element_type value);
linkstack.c
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
// Create a stack
link_stack *create_stack()
{
link_stack *t_stack = (link_stack*)malloc(sizeof(link_stack));
if(t_stack != NULL) {
t_stack->next = NULL;
t_stack->value = 0;// This can represent the number of elements in the stack
}
return t_stack;
}
// Determine whether it is null
int is_empty(link_stack *t_stack)
{
int res = -1;
if(t_stack != NULL) {
res = (t_stack->next == NULL);
}
return res;
}
// Destroy the stack
void destroy_stack(link_stack *t_stack)
{
// Pay attention to a problem , Stack destroyed , Can you traverse it , You can't , The stack can only be top Top of stack operations , therefore
if(t_stack != NULL) {
free(t_stack);
}
}
// Get the element at the top of the stack
element_type top_stack(link_stack *t_stack)
{
// The top of the stack is the first element pointed to by the header node
if(t_stack == NULL) {
printf(" error \n");
} else {
return t_stack->next->value;
}
}
void pop_stack(link_stack *t_stack)
{
// Here's the stack , Let's make the head node point to the next node of the first node
if(t_stack != NULL && !is_empty(t_stack)) {
link_stack *p_first = t_stack->next;
t_stack->next = p_first->next;
free(p_first);
t_stack->value--;
}
}
// insert data , When inserting data, remember to allocate corresponding memory space to the data
void push_stack(link_stack *t_stack,element_type value)
{
link_stack *p_node = (link_stack *)malloc(sizeof(link_stack));
if(t_stack != NULL && p_node != NULL) {
p_node->value = value;
p_node->next = t_stack->next;
t_stack->next = p_node;
t_stack->value++;
}
}
Then look at the test file :
main.c
#include <stdio.h>
#include <stdlib.h>
#include "linkstack.h"
int main()
{
link_stack *t_stack = create_stack();
printf(" The number of elements in the stack %d\n",t_stack->value);
element_type value1 = 1;
element_type value2 = 2;
element_type value3 = 3;
// Into the stack
push_stack(t_stack,value1);
push_stack(t_stack,value2);
push_stack(t_stack,value3);
printf(" The number of elements in the stack after insertion %d\n",t_stack->value);
// Loop out the stack
while(!is_empty(t_stack)) {
element_type value = top_stack(t_stack);
printf("%d\n",value);
pop_stack(t_stack);
}
printf(" Length after stack %d\n",t_stack->value);
// Destroy the stack
destroy_stack(t_stack);
return 0;
}
Running results :
边栏推荐
- Flinksql read / write PgSQL
- 如何提高网站权重
- Introduction to yarn (one article is enough)
- 搞懂fastjson 对泛型的反序列化原理
- cf:C. Column Swapping【排序 + 模擬】
- Win configuration PM2 boot auto start node project
- Nvisual network visualization
- Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
- Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
- 原生小程序 之 input切换 text与password类型
猜你喜欢
The solution of a simple algebraic problem
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
[InstallShield] Introduction
404 not found service cannot be reached in SAP WebService test
数字IC面试总结(大厂面试经验分享)
R语言【逻辑控制】【数学运算】
老板总问我进展,是不信任我吗?(你觉得呢)
Three level menu data implementation, nested three-level menu data
Reading notes of Clickhouse principle analysis and Application Practice (6)
An example of multi module collaboration based on NCF
随机推荐
驱动开发中platform设备驱动架构详解
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
[solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
数据中心为什么需要一套基础设施可视化管理系统
Message queue: how to deal with message backlog?
Web authentication API compatible version information
CMD permanently delete specified folders and files
cf:C. Column Swapping【排序 + 模拟】
What EDA companies are there in China?
Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
EMMC打印cqhci: timeout for tag 10提示分析与解决
Introduction to distributed transactions
Reptile exercises (III)
PTA 天梯赛练习题集 L2-004 搜索树判断
PowerPivot - DAX (function)
Things about data storage 2
win配置pm2开机自启node项目
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
Explication contextuelle du langage Go
Red hat install kernel header file