原题链接:[编程入门]链表之节点删除
#include<bits/stdc++.h>
using namespace std;
struct node{//定义链表结点,头结点 xuehao 变量保存链表结点数
int xuehao,score;
struct node *Next;
};
typedef struct node Node;
void tailinsert(int xuehao,int score,Node *L){//尾插法插入结点
Node *node = (Node*)malloc(sizeof(Node));
node->xuehao = xuehao;
node->score = score;
node->Next = NULL;
if(L->xuehao==0){
L->Next = node;
L->xuehao++;
}
else{
Node *temp = L->Next;
while(temp->Next!=NULL){
temp = temp->Next;
}
temp->Next = node;
L->xuehao++;
}
}
void printlist(Node *L){
cout << L->xuehao << endl;
Node *temp = L->Next;
if(temp==NULL){
printf("Link list is empty\n");
return;
}
while(temp!=NULL){
printf("%d %d\n",temp->xuehao,temp->score);
temp = temp->Next;
}
}
void deletenode(Node *L1,Node *L2){//删除实现函数
Node *temp1 = L1;
Node *temp2 = L2;
Node *t;
while(temp2->Next!=NULL){
temp1 = L1;
while(temp1->Next!=NULL){
if(temp1->Next->xuehao==temp2->Next->xuehao){
t = temp1->Next;
temp1->Next = t->Next;
t->Next = NULL;
free(t);
L1->xuehao--;
break;
}
else{
temp1 = temp1->Next;
}
}
temp2 = temp2->Next;
}
}
int main() {
Node *headnode1 = (Node*)malloc(sizeof(headnode1));//申请两个链表的头结点
Node *head1 = headnode1;
head1->xuehao = 0;
head1->score = 0;
head1->Next = NULL;
Node *headnode2 = (Node*)malloc(sizeof(headnode2));
Node *head2 = headnode2;
head2->xuehao = 0;
head2->score = 0;
head2->Next = NULL;
int a,b,xuehao,score;
cin >> a >> b;
for(int i=0;i<a;i++){
cin >> xuehao >> score;
tailinsert(xuehao,score,head1);
}
for(int i=0;i<b;i++){
cin >> xuehao >> score;
tailinsert(xuehao,score,head2);
}
deletenode(head1,head2);
printlist(head1);
return 0;
}0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复