当前位置:网站首页>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 :
边栏推荐
- Sidecar mode
- PTA 天梯赛练习题集 L2-004 搜索树判断
- Question 102: sequence traversal of binary tree
- Red hat install kernel header file
- [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]
- 关于服装ERP,你知道多少?
- 线性回归
- I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
- Five core elements of architecture design
- Data storage 3
猜你喜欢
Cf:c. column swapping [sort + simulate]
Mac version PHP installed Xdebug environment (M1 version)
Message queuing: how to ensure that messages are not lost
【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现
Reading notes of Clickhouse principle analysis and Application Practice (6)
Introduction to distributed transactions
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
Introduction to the extension implementation of SAP Spartacus checkout process
如果不知道这4种缓存模式,敢说懂缓存吗?
Determine whether the file is a DICOM file
随机推荐
Differences and introduction of cluster, distributed and microservice
TCC of distributed transaction solutions
[SQL practice] a SQL statistics of epidemic distribution across the country
pytorch_ 01 automatic derivation mechanism
Classic questions about data storage
Understand the deserialization principle of fastjson for generics
力扣102题:二叉树的层序遍历
Interview skills of software testing
The solution of a simple algebraic problem
Hcip eighth operation
win配置pm2开机自启node项目
Reptile exercises (III)
CTFshow--常用姿势
Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
Introduction to the extension implementation of SAP Spartacus checkout process
Distributed global ID generation scheme
[InstallShield] Introduction
线性回归
Web architecture design process
每秒10W次分词搜索,产品经理又提了一个需求!!!(收藏)