当前位置:网站首页>Create a linked list by head interpolation and output all elements
Create a linked list by head interpolation and output all elements
2022-07-23 23:35:00 【Genius Polaris】
Create linked list by head insertion
Linklist Create_list(Linklist head){
// Head insertion creates a single chain table
head =(Linklist)malloc(sizeof(LNode));// Open up memory space for header pointers
LNode *node=NULL;// Define the working pointer
int count=0;// Number of nodes created
head->next=NULL;
node=head->next;// The pointer field of the last node is always saved as NULL
printf(" Enter the length of the linked list \n");
scanf("%d",&count);
printf(" The linked list is created !\n");
for(int i=0;i<count;i++){
node=(LNode *)malloc(sizeof(LNode));// Open up memory space for new nodes
node->data=i;// Assign a value to the new node
node->next=head->next;// The address of the next node pointed to by the header pointer , Assigned to the newly created node next
head->next=node;// Assign the address of the newly created node to the next node of the header pointer
}
return head;
}
Access all elements
void printL(Linklist L){
// Access all elements
LNode *p;
p=L->next;
while(p!=NULL){
printf(" The first %d Elements are %d\n",p->data,p->data);
p=p->next;
}
}
Complete implementation
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
// Define the structure of the structure declaration table
int data;// Data fields
struct LNode *next;// Pointer to the domain
}LNode,*Linklist;
Linklist Create_list(Linklist head){
// Head insertion creates a single chain table
head =(Linklist)malloc(sizeof(LNode));// Open up memory space for header pointers
LNode *node=NULL;// Define the working pointer
int count=0;// Number of nodes created
head->next=NULL;
node=head->next;// The pointer field of the last node is always saved as NULL
printf(" Enter the length of the linked list ");
scanf("%d",&count);
printf(" The linked list is created !\n");
for(int i=0;i<count;i++){
node=(LNode *)malloc(sizeof(LNode));// Open up memory space for new nodes
node->data=i;// Assign a value to the new node
node->next=head->next;// The address of the next node pointed to by the header pointer , Assigned to the newly created node next
head->next=node;// Assign the address of the newly created node to the next node of the header pointer
}
return head;
}
void printL(Linklist L){
// Access all elements
LNode *p;
p=L->next;
while(p!=NULL){
printf(" The first %d Elements are %d\n",p->data,p->data);
p=p->next;
}
}
void main(){
LNode *head;// Declare the header node
LNode* p = Create_list(head); // Reference the header node to create a linked list
printL(p);
}
Specific effect


summary : The head insertion method creates a linked list to realize the reverse order input , Each new element is placed after the header node , Can be used as a subsequent descending sort .
边栏推荐
- 头插法创建链表并输出所有元素
- ret2text
- DGS之文件上传
- BUUCTF -rip
- FreeRTOS personal notes - create / delete dynamic tasks, start scheduler
- Everything you need to know about parsing JSON with Jackson
- 【Error】TypeError: expected str, bytes or os. PathLike object, not int
- Introduction to mysqlbinlog command (remote pull binlog)
- anchor free yolov1
- J9 number theory: how can we overcome the fomo phenomenon in the digital industry?
猜你喜欢

How to migrate databases in the flask framework

Basic operations of AutoCAD

Analysis of mobile semantics and perfect forwarding

pwn1_sctf_2016
![[第五空间2019 决赛]PWN5](/img/51/03e6078961a8eab991fa08bb178a7b.png)
[第五空间2019 决赛]PWN5

strncat() strncmp()

TOPSIS method (matlab)

ArraysList 与顺序表 ——模拟实现
![[tensorflow] check whether tensorflow GPU is available](/img/27/e2b21b0a0cecdff27ddd2af6e34949.png)
[tensorflow] check whether tensorflow GPU is available

在openEuler社区开源的Embedded SIG,来聊聊它的多 OS 混合部署框架
随机推荐
jarvisoj_level0
[ssm] joint debugging of front and rear protocols ①
BGP基础实验
Redis管道技术/分区
DGS之N+1选择问题
麒麟OS和龙芯环境编译安装GreatSQL
System memory introduction and memory management
关于使用 Jackson 解析 JSON 你需要知道的一切
None and Nan, Nan, Nan
Analysis of video capability and future development trend based on NVR Technology
DGS之联邦(Federation)
中原证券靠谱吗?是否合法?开股票账户安全吗?
ret2text
Light up the LED light of little bear patting learning
ArraysList 与顺序表 ——模拟实现
Tensorflow one layer neural network training handwritten digit recognition
jarvisoj_ level2
Chinese NFT? NFR was born
[SSM]前后台协议联调①
pwn1_sctf_2016