解题思路: 直接上代码,重要点已打注释
注意事项:
参考代码:
#include<stdio.h> #include<stdlib.h> typedef struct student//建立学生结构体 { int num; int scorm; struct student* next; }*node,Node; /*--------------------------------------------------------*/ node creat(int count)//创建n个节点的链表 { node header,tail,insert; header = (node)malloc(sizeof(Node)); header->next =NULL; tail=header; for(int i=0;i<count;i++) { insert = (node)malloc(sizeof(Node)); scanf("%d%d",&insert->num,&insert->scorm); insert->next = NULL; tail->next = insert; tail=insert; } return header; } /*--------------------------------------------------------*/ node delete_header(node head_a,node head_b)//删除相同节点 { int flag; node header1_a,header1_b,header2_a,header2_b; header1_a = head_a; while(header1_a->next!=NULL) { flag = 0; header1_b = head_b; while(header1_b->next!=NULL) { if(header1_a->next->num==header1_b->next->num) { header2_a=header1_a->next; header1_a->next=header2_a->next; free(header2_a); header2_b=header1_b->next; header1_b->next=header2_b->next; free(header2_b); flag = 1; } else header1_b = header1_b->next; } if(flag==0) header1_a = header1_a->next; } return head_a; } /*--------------------------------------------------------*/ void count_output(node head_a)//打印节点数和链表成员 { int n=0; node tail=head_a->next; while(tail!=NULL)//节点数 { tail = tail->next; n++; } printf("%d\n",n); tail=head_a->next; while(tail!=NULL)//成员 { printf("%d %d\n",tail->num,tail->scorm); tail = tail->next; } } /*--------------------------------------------------------*/ void free_head(node head_a,node head_b)//释放链表 { node temp=head_a;//注意释放节点要保持先后顺序,否则会发生指针漂移(先找好下个节点,再释放,最后重新继承) while(temp!=NULL)//释放a链表 { head_a=head_a->next; free(temp); temp = head_a; } temp=head_b; while(temp!=NULL)//释放b链表 { head_b=head_b->next; free(temp); temp = head_b; } } /*-----------------------主函数---------------------------------*/ int main() { int n,m; node header_a,header_b; scanf("%d%d",&n,&m); header_a = creat(n); header_b = creat(m); delete_header(header_a,header_b);//删除 count_output(header_a);//输出 free_head(header_a,header_b);//释放 return 0; }
0.0分
2 人评分
Wu-求圆的面积 (C++代码)浏览:1994 |
C语言程序设计教程(第三版)课后习题6.8 (C语言代码)浏览:544 |
字符串比较 (C语言代码)浏览:770 |
母牛的故事 (C语言代码)浏览:623 |
快速排序算法1浏览:996 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:744 |
蓝桥杯基础练习VIP-报时助手 (C++代码)浏览:1130 |
C语言程序设计教程(第三版)课后习题1.6 (C语言代码)浏览:687 |
简单的a+b (C语言代码)浏览:536 |
老王赛马 (C语言代码)浏览:1732 |