在主函数中实现多个节点的插入与删除
本代码的背景是输入学号,成绩,以0,0,终止,后输入删除节点个数,删除者学号,再然后输入添加节点个数,添加者的学号,成绩。
小编初学写的比较复杂,各位大佬还请轻点喷
代码如下:
#include<bits/stdc++.h>
using namespace std;
#define LEN sizeof(struct student)
int n;
struct student
{
long long int num;
double score;
struct student *next;
};
struct student *pt;
struct student *creat(void)//创建链表
{
struct student *head,*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);//划分空间
//数据初始化
scanf("%lld%lf",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n+=1;
if(n==1)//head 指向队头
{
head=p1;
}
else
{
p2->next=p1; //将数据添加到链尾
}
//重置指针
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%lld %lf",&p1->num,&p1->score);
}
//将链表尾部地址归零
p2->next=NULL;
return (head);
}
//输出链表函数
void print(st