Ggzy_ 2021-10-27 00:22 采纳率: 66.7%
浏览 23
已结题

能不能看看这个C语言链表为什么跑不起来吧

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stud_node {
int num;
char name[20];
int score;
struct stud_node* next;
};
struct stud_node* Create_Stu_Doc();//新建链表
struct stud_node* InsrtDoc(struct stud_node* head, struct stud_node* stud);//插入
struct stud_node* DeleteDoc(struct stud_node* head, int num);//删除
void Print_stu_Doc(struct stud_node* head);//遍历
int main()
{
struct stud_node* head, * p;
int choice, num, score;
char name[20];
head = NULL;
int size = sizeof(struct stud_node);

do {
    printf("1:create 2:insert 3:delete 4:print 0:exit\n");
    scanf_s("%d", &choice);
    switch (choice) {
    case 1:
        head= Create_Stu_Doc();
        break;
    case 2:
        printf("Input num,name and score:\n");
        scanf_s("%d%s%d",&num,name,&score);
        p = (struct stud_node*)malloc(size);
        p->num = num;
        strcpy_s(p->name, name);
        p->score = score;
        head = InsrtDoc(head, p);
        break;
    case 3:
        printf("Input num:\n");
        scanf_s("%d", &num);
        head = DeleteDoc(head, num);
        break;
    case 4:
        Print_stu_Doc(head);
        break;
    case 0:
        break;
    }
} while (choice != 0);

return 0;

}
//新建链表
struct stud_node* Create_Stu_Doc()
{
struct stud_node* head, * p;
int num, score;
char name[20];
int size = sizeof(struct stud_node);

head = NULL;
printf("Input num,name and score:\n");
scanf_s("%d%s%d",&num,name,&score);
while (num != 0)
{
    p = (struct stud_node*)malloc(size);
    p->num = num;
    strcpy_s(p->name, name);
    p->score = score;
    head = InsrtDoc(head, p);     //调用插入函数
    scanf_s("%d%s%d", &num, name, &score);
}
return head;

}
//插入
struct stud_node* InsrtDoc(struct stud_node* head, struct stud_node* stud)
{
struct stud_node* ptr, * ptr1, * ptr2;
ptr2 = head;
ptr = stud;
if (head == NULL)
{
head = ptr;
head->next = NULL;
}
else{
while ((ptr->num > ptr2->num) && (ptr2->next != NULL)) {
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if (ptr->num <= ptr2->num) {
if (head == ptr2) head = ptr;
else ptr1->next = ptr;
ptr->next = ptr2;
}
else {
ptr2->next = ptr;

  • 写回答

1条回答 默认 最新

  • CSDN专家-link 2021-10-27 00:45
    关注

    scanf_s输入字符串时,后面要指定字符串最大长度

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月4日
  • 已采纳回答 10月27日
  • 创建了问题 10月27日

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?