当前位置:网站首页>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 :
边栏推荐
- 【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
- CTFshow--常用姿势
- Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
- How much do you know about clothing ERP?
- cf:C. Column Swapping【排序 + 模擬】
- 每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)
- 从“跑分神器”到数据平台,鲁大师开启演进之路
- [SQL practice] a SQL statistics of epidemic distribution across the country
- nVisual网络可视化
- CMD permanently delete specified folders and files
猜你喜欢
【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
PowerPivot - DAX (function)
Five core elements of architecture design
pytorch_ 01 automatic derivation mechanism
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
Introduction to distributed transactions
关于STC单片机“假死”状态的判别
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL | Web框架Gin(九)
随机推荐
PTA 天梯赛练习题集 L2-002 链表去重
Message queue: how to deal with message backlog?
zabbix_get测试数据库失败
SAP Spartacus checkout 流程的扩展(extend)实现介绍
线性回归
《ClickHouse原理解析与应用实践》读书笔记(6)
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
Industrial Finance 3.0: financial technology of "dredging blood vessels"
EMMC打印cqhci: timeout for tag 10提示分析与解决
PTA ladder game exercise set l2-004 search tree judgment
一个简单的代数问题的求解
驱动开发中platform设备驱动架构详解
TCC of distributed transaction solutions
On the difference between FPGA and ASIC
async / await
搞懂fastjson 对泛型的反序列化原理
C nullable type
Nodejs get client IP
话说SQLyog欺骗了我!
PTA ladder game exercise set l2-002 linked list de duplication