当前位置:网站首页>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 .
边栏推荐
- This article will show you what typescript is
- [第五空间2019 决赛]PWN5
- ciscn_ 2019_ n_ one
- A deserialized CTF question sharing
- A great open source micro community light forum source code
- Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework
- 第四章、实现用例
- ret2text
- STM32 CAN初始化详解
- pwn1_sctf_2016
猜你喜欢

难怪国内企业ERP应用效果普遍不理想

Redis管道技术/分区

cannot meet the needs of the people? How can programmers take private jobs to effectively increase their income?

How does the easynvr platform turn off anonymous login?

Strncat () strncmp ()

bjdctf_ 2020_ babystack

JS convert numbers to uppercase

Mongodb database + graphical tools download, installation and use

【Error】TypeError: expected str, bytes or os. PathLike object, not int
![[SSM]前后台协议联调②](/img/ed/6a42e19f384fa0d4a039e52f5a95f4.png)
[SSM]前后台协议联调②
随机推荐
[audio and video technology] video quality evaluation MSU vqmt & Netflix vmaf
[ssm] joint debugging of front and rear protocols ①
Detailed explanation of pseudo instructions in assembly language (with examples)
Analysis of video capability and future development trend based on NVR Technology
Federation of DGS
二,数字逻辑功能单元
solo 文章正文含有 &lt;&gt; 标签会影响到页面样式
中原证券靠谱吗?是否合法?开股票账户安全吗?
warmup_csaw_2016
Sql156 average completion rate of each video
ret2shellcode
Baidu editor uploads pictures and sets custom directories
The basic syntax of go language (variables, constants, basic data types, for, switch, case, array, slice, make, new, map)
No wonder the application effect of ERP in domestic enterprises is generally not ideal
Quickly learn to use file permissions
[Fifth space 2019 finals]pwn5
Arrayslist and sequence table -- Simulation Implementation
bjdctf_2020_babystack
Chinese NFT? NFR was born
Android金九银十的面试你准备的怎么样了?最新Android面试真题汇总助你备战