|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
帮忙看看这道谭浩强c语言的课后题在vs2019上的警告怎么解决
题目是从课后作业里面抄的,但是在vs2019运行有报警,关闭SDL检查依旧存在,查了官方的说明,有点看不懂,有没有大佬可以讲的明白一点
//建立自己的数据类型-链表的建立,删除,打印,插入
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
long num;
float score;
struct student* next;
};
int n;
int main()
{
struct student* creat();
struct student* del(struct student*, long);
struct student* insert(struct student*, struct student*);
void print(struct student*);
struct student* head, *stu;
long del_num;
printf("input records:\n");
head = creat();
print(head);
printf("input the deleted number:");
scanf_s("%ld", &del_num);
while (del_num != 0)
{
head = del(head, del_num);
print(head);
printf("input the deleted number:");
scanf_s("%ld", &del_num);
}
printf("input the inserted record:");
stu = (struct student*)malloc(LEN);
scanf_s("%ld,%f", &stu->num, &stu->score);
while (stu->num != 0)
{
head = insert(head, stu);
print(head);
printf("input the inserted record:");
stu = (struct student*)malloc(LEN);
scanf_s("%ld,%f", &stu->num, &stu->score);
}
return 0;
}
struct student* creat()
{
struct student* head;
struct student* p1, * p2;
n = 0;
p1 = p2 = (struct student*)malloc(LEN);
scanf_s("%ld,%f", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
n = n + 1;
if (n == 1)
head = p1;
else p2->next = p1;//把上一个p2的next指向新内存p1
p2 = p1;//这时p1和p2都指向这个新内存
p1 = (struct student*)malloc(LEN);//新开辟的内存
scanf_s("%ld,%f", &p1->num, &p1->score);
}
p2->next = NULL;
//printf("%5.1f", (p1-1)->score);
return (head);
}
struct student* del(struct student* head, long num)
{
struct student* p1, * p2;
if (head == NULL)
{
printf("\nlist null!");
return(head);
}
p2=p1 = head;
while (num != p1->num && p1->next != NULL)
{
p2 = p1; p1 = p1->next;
}
if (num == p1->num)
{
if (p1 == head)head = p1->next;
else p2->next = p1->next;
printf("delete :%ld\n", num);
n = n - 1;
}
else printf("%ld not been found!\n", num);
return (head);
}
struct student* insert(struct student* head, struct student* stud)
{
struct student* p0, * p1, * p2;
p2=p1 = head;//p1指向原结构体链表
p0 = stud;//p0指向欲加入的一组数据
if (head == NULL)//判断链表是否为空,
{
head = p0; p0->next = NULL;//为空就把head指向新加入的链表
}
else //如果不为空,就寻找新接入的数据应该插入哪个位置
{
while ((p0->num > p1->num) && (p1->next != NULL))//插入中间位置
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)//
{
if (head == p1) head = p0;//链表有一个数据,而且数据标号和要插入的数据编号一样
else p2->next = p0;//插入链表中间位置
p0->next = p1;
}
else {
p1->next = p0;//插入链表末尾
p0->next = NULL;
}
}
n += 1;
return(head);
}
void print(struct student* head)
{
struct student* p;
printf("\nNow,These %d records are:\n", n);
p = head;
if (head != NULL)
do
{
printf("%ld %5.1lf\n", p->num, p->score);
p = p->next;
} while (p != NULL);
} |
|