原题链接:[编程入门]链表之节点删除
#include#includetypedef struct Node{ int id; int score; struct Node* next; }Node; void AddAtEnd(Node**head,int id, int score){ Node* new_node = (Node*)malloc(sizeof(Node)); new_node->id = id; new_node->score = score; new_node->next = NULL; if(*head == NULL) { *head = new_node; return; } Node* cur = *head; while(cur->next!=NULL){ cur = cur->next; } cur->next = new_node; } void deleteNode(Node** head, int pos){ Node *cur = *head; Node *prev = NULL; if(pos == 0) //要删除头节点 *head = (*head)->next; else { while(pos--) { prev = cur; cur = cur->next; //这样只是为了找到要删的节点 } prev->next = cur->next; } } void showNode(Node* head){ if(head ==NULL) { printf("Link list is empty\n"); return; } Node*cur = head; printf("%d %d\n",cur->id,cur->score); while(cur->next!=NULL) { cur = cur->next; printf("%d %d\n",cur->id,cur->score); } } int main(){ int m,n,id,score; scanf("%d%d",&m,&n); Node*A = NULL; Node*B = NULL; int m1=m,n1=n; if(m==0) printf("没输入"); while(m1--) { scanf("%d%d",&id,&score); AddAtEnd(&A,id,score); } while(n1--) { scanf("%d%d",&id,&score); AddAtEnd(&B,id,score); } Node* curA = A; Node* curB = B; Node*cur = NULL; int cnt = 0;//更新删除位置 for (int i = 0; i < m; i++) { // printf("第%d趟开始时,curA的id是%d\n",i,curA->id); curB = B; for (int j = 0; j < n; j++) { //printf("这是第%d趟的第%d个\n",i,j); //printf("curA和curB的id分别为%d %d\n",curA->id,curB->id); cur = curA->next; if(curA->id == curB->id) { deleteNode(&A,i-cnt); cnt++; //printf("删除了%d之后的链表A\n",curA->id); //showNode(A); break; } curB = curB->next; } curA = cur; } printf("%d\n",m-cnt); showNode(A); return 0; }
解题思路:
注意事项:
参考代码:
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复