当前位置:网站首页>Creation of circular linked list and controllable output
Creation of circular linked list and controllable output
2022-04-22 02:36:00 【Descosmos】
The creation and basic operation of circular linked list
In the last part, we talked about the use of The first interpolation and The tail interpolation How to create a single linked list , Two methods of comparison . Then we learn the creation of circular linked list .
As long as you learn how to create a single linked list , The creation of circular linked list becomes very simple .
Circular linked list creation
The structure of single linked list :

Circular linked list :

The so-called circular linked list , That is, the tail node of the single linked list end A pointer to the domain That is to say end->next Point to the address of the head node : end->next = head . This completes the creation of circular linked list .
It's over ? Of course, it's not that simple ~~~
Circular linked list is different from single linked list , The reason is that when we talked about single linked list last time , The tail node pointer field always points to NULL, That is to say end->next = NULL. Therefore, the sign for judging the end of the single linked list , Namely while( node->next != NULL) , But the tail node of the circular linked list does not point to NULL , It is Head node head. therefore , Our judgment criteria should also be changed .
After thinking , Decide to assign the data field of the head node to -1, So there's a sign , Used to judge whether the circular linked list is executed once .
thought without learning is perilous , Get started now !
Linklist Creat_list(Linklist head) {
LNode *node = NULL;
LNode *end = NULL;
head = (Linklist)malloc(sizeof(LNode));
head->next = NULL;
head->data = -1;
end = head;
int count = 0;
printf("Input node numbers: ");
scanf("%d", &count);
for (int i = 0; i < count; i++) {
node = (LNode*)malloc(sizeof(LNode));
node->data = i;
end->next = node;
end = node;
}
end->next = head; // Point the pointer of the tail node to the head node
return head;
}
This completes the creation of circular linked list , Next, we need to find a way to make the circular linked list execute the number of cycles as we expected .
focused , I came up with this function to judge the number of nodes in a circular linked list .
int node_number(Linklist *head) {
Linklist tem = head;
tem = tem->next; // The header of the node is the value of the field -1, Therefore, we need to start from the next node of the node
int count = 0; // Count the number of nodes
while (tem->data != -1){ // Because the data field of the head node is assigned -1, therefore -1 Equivalent to a label
count++; // Not once , One more knot point
tem = tem->next; // Next node
}
return count + 1; // Because before the first execution of the loop , Skipped the header node , So I got count No head node , When we return, we add 1
}
In this way, we get the number of nodes of the circular linked list .
As long as you have the number of nodes , We can output the number of all nodes of the circular linked list according to our requirements .
void Illustrate(Linklist head) {
Linklist tem = head;
int size = node_number(head)*2; // *2 Indicates that the circular linked list will be executed twice
for (int i = 0; i < size; i++) { // size Indicates the number of nodes to pass .
printf("%d ", tem->data);
tem = tem->next;
}
}
In this way, we have completed the creation of circular linked list and control output .
版权声明
本文为[Descosmos]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220230319617.html
边栏推荐
- uniapp实现出生日期/时间选择效果
- Embedded AI
- 金融信息安全实训——22/4/19(上)
- The flitter does not use the navigation bar of the status bar
- 循环链表的创建及可控输出
- Training set: alexnet classification
- The night can also be colorful, and the full-color night vision system can be realized by deep learning
- Install and deploy phpstudy + DVWA vulnerability drill platform
- Why can the Internet industry attract more and more young people? Especially programmers
- 软件测试·坏味道
猜你喜欢

Can you really use ` timescale?

How to provide CPU branch prediction efficiency at the code level

Install and deploy phpstudy + DVWA vulnerability drill platform

DOS 命令行基本命令及实践

Translation of yolov3 papers

(进阶用法) C语言字符串函数

MySQL Chapter 1 Introduction to database

Analysis and interpretation of the current situation and challenges faced by enterprise operation and maintenance in the digital era

STM32 flash operation

Five cool login pages that can be used
随机推荐
Translation of yolov3 papers
Use of swift universal types anyobject and any
开发管理·华为IPD
Software testing · bad taste
刷题笔记:剑指offer 04.二维数组中的查找——一个报错
Alibaba P9 explains high concurrency in detail, and shows you how Taobao can resist large-scale second kill activities such as double 11
Five cool login pages that can be used
STM32 FLASH操作
[paper reading] active class incremental learning for balanced datasets
二叉排序树基本性质详解
Development of smart party building system and construction of information management platform for smart team members
Development management · Huawei IPD
二元交叉熵损失函数
Leetcode answer notes (I)
Information Security Overview
JS Baidu map positioning
Can you really use ` timescale?
Mysql的索引为什么使用B+树而不使用跳表?
Interview question: use the program to realize the alternating printing of odd and even numbers from 0 to 100 by two threads
The flitter does not use the navigation bar of the status bar