C语言 链表 创建

it2025-12-29  11

#include <stdio.h> #include <stdlib.h> typedef struct create{//结构体 int value; struct create *next;//指向下一个节点 } asd ; int main() { int a,n;//a为数据,n为储存数据个数 asd* head = NULL;//链表头部 asd* last = NULL;//始终指向链表尾部 scanf("%d",&n); for(int i = 0;i<n;i++){ scanf("%d",&a); asd* p = (asd*)malloc(sizeof(asd));//申请储存空间 p->value = a; p->next = NULL; if(!head){//** !head 等价于 head=Null ** head = p; } else{ last->next = p;//使得 last->next 指针指向 p (连接p) } last = p;// 使得 last 始终指向链表尾部(此时尾部为 p) } for(head;head;head=head->next){//验证 printf("%d\n",head->value); } }
最新回复(0)