#include <stdio.h> #include <stdlib.h> typedef struct linknode { int data; struct linknode *next;//struct linknode是一个结构体类型名,需要作为整理而使用 }node;// typed struct A B;表示为结构体struct A起一个别名 B. 那么在后面创建一个变量x的时候可以用struct A x;或者B x; int main() {node*head,*p,*s;//p:指向前驱节点;s指向当前待加入节点;head指向头节点。 int x,cycle=1; //cycle是循环控制变量 head=(node *)malloc(sizeof(node)); p=head; while(cycle) { scanf("%d",&x); if(x!=0) {s=(node*)malloc(sizeof(node)); s->data=x; p->next=s; //把s结点链接到前面建立的单链表中 p=s; } else {cycle=0; //表示结束加入新节点 p->next=NULL; } } return 0; }
数据结构学习笔记7--c语言建立一个链表(已测正确无误)
最新推荐文章于 2023-12-11 21:25:46 发布