首页 > 代码库 > 无表头单链表的总结----如何将已经初始化的结构体数组加入链表关系

无表头单链表的总结----如何将已经初始化的结构体数组加入链表关系

 1 struct Student
 2 {
 3     char ID[N_ID];
 4     char name[N_name];
 5     struct Student *next;
 6 }alist[LEN_A],blist[LEN_B];
 7 ////以上是结构体
 8 //初始化
 9 struct Student alist[LEN_A] = { {"101","Wang"} ,{"102","Li"},{"105","Zhang"},{"105","Wei"} };
10 struct Student blist[LEN_B] = 
11 { { "101","Zhang" } ,{ "104","Ma" },{ "105","Chen" },{ "107","Guo" },{ "108","liu" } };
12 
13 //接下来对初始化结构加入链表关系
14 #include"head.h"
15 void print(struct Student *p,int sum)   //sum是数组的长度,p是数组的头指针
16 {
17     int i;
18     struct Student *head=p;
19     for (i=1; i <= sum; i++)
20     {
21         if (i < sum)
22             p->next = head + i;//关键,使得结构体成员的next指向下一个成员 
23         else
24             p->next = NULL;//使得链表最后加入终止符号null
25         printf("%s %s\n", p->ID, p->name);
26         if (i < sum)
27             p = p->next;//链表指针移动
28     }
29 }

 

无表头单链表的总结----如何将已经初始化的结构体数组加入链表关系