#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语言代码)错误???浏览:602 |
WU-判定字符位置 (C++代码)浏览:1471 |
有关字符,字符串的输入输出函数说明浏览:498 |
杨辉三角 (C语言代码)浏览:505 |
C语言程序设计教程(第三版)课后习题6.2 (C语言代码)浏览:569 |
C二级辅导-等差数列 (C语言代码)浏览:806 |
C二级辅导-同因查找 (C语言代码)浏览:618 |
分糖果 (C语言代码)浏览:980 |
拆分位数 (C语言代码)浏览:558 |
多输入输出练习2 (C语言代码)浏览:1710 |