#include<stdio.h> #include<stdlib.h> struct Data{ int nian; int yue; int ri; }; struct student{ char xh[15]; char xm[10]; char xb[4]; int nl; struct Data rx; int cj1; int cj2; }; typedef struct node{ struct student st; struct node *next; }linklist2; int main() { int i,j; struct student t1,t2,*p1,*p2; struct student stu[10]={ {"2020B200301","刘滨","男",18,{2020,9,20},70,85}, {"2020B200302","阚美娟","女",17,{2020,9,20},87,95}, {"2020B200303","胡大勇","男",17,{2020,9,20},69,80}, {"2020B200305","黎丽","女",18,{2020,9,20},68,88}, {"2020B200309","金鑫","男",19,{2019,9,1},90,85} }; printf("数组数据输出:\n"); printf(" 学号 姓 名 性别 年龄 入学日期 成绩1 成绩2\n"); for(i=0;i<5;i++){ printf("%s %-6s %s %d %4d-%2d-%2d %d %d\n", stu[i].xh,stu[i].xm, stu[i].xb,stu[i].nl,stu[i].rx.nian,stu[i].rx.yue, stu[i].rx.ri,stu[i].cj1,stu[i].cj2); } printf("\n"); linklist2 *h=(linklist2 *) malloc(sizeof(linklist2)),*p; h->next =NULL;//建立空链表h for(i=4;i>=0;i--){//数组中最后元素,先写入链表h p=(linklist2 *) malloc(sizeof(linklist2)); //生成新节点 p p->st =stu[i]; //将数组数据写入新节点 p p->next =h->next; h->next=p;//用头插入法将节点p插入链表 h } printf("链表h中的数据:\n"); printf(" 学号 姓 名 性别 年龄 入学日期 成绩1 成绩2\n"); p=h->next ; while(p){ printf("%s %-6s %s %d %4d-%2d-%2d %d %d\n", p->st.xh,p->st.xm, p->st.xb,p->st.nl,p->st.rx.nian,p->st.rx.yue, p->st.rx.ri,p->st.cj1,p->st.cj2); p=p->next ;} return 0; } 在上述的链表中间和末尾分别增加一个节点。节点数据如下: {“2020B200304”,”齐鲁田”,”男”,18,{2020,9,20},79,90} {“2020B200310”,”潘云”,”女”,19,{2020,9,20},91,80}
时间: 2024-01-10 08:04:15 浏览: 32
可以按照下面的代码进行添加:
```
// 在链表中间插入新节点
p1 = (linklist2 *) malloc(sizeof(linklist2));
p2 = h->next->next; // 找到链表中第二个节点
p1->st = (struct student){"2020B200304","齐鲁田","男",18,{2020,9,20},79,90};
h->next->next = p1;
p1->next = p2;
// 在链表末尾添加新节点
p1 = (linklist2 *) malloc(sizeof(linklist2));
p1->st = (struct student){"2020B200310","潘云","女",19,{2020,9,20},91,80};
p1->next = NULL;
p = h;
while(p->next) p = p->next;
p->next = p1;
```
添加完成后,可以再次遍历链表,检查新节点是否被正确添加:
```
printf("\n链表h中的数据(包含新添加的节点):\n");
printf(" 学号 姓 名 性别 年龄 入学日期 成绩1 成绩2\n");
p = h->next;
while(p){
printf("%s %-6s %s %d %4d-%2d-%2d %d %d\n",
p->st.xh,p->st.xm,p->st.xb,p->st.nl,p->st.rx.nian,p->st.rx.yue,
p->st.rx.ri,p->st.cj1,p->st.cj2);
p = p->next;
}
```
相关推荐
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)