BE北北苏 2021-12-26 14:56 采纳率: 69.2%
浏览 5
已结题

大家帮我看看我这个单向链表哪里出问题了??

问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct student
{
    char number[10];
    char name[10];
    int garde[3];
    struct student *next;
};
struct student* head_insertion(struct student *);
struct student* tail_insertion(struct student *);
struct student* number_insertion(struct student *);
void  s_printf(struct student *);
struct student* _delete (struct student *);
int main()
{
    char number[10];
    int choice;
    char ch;
    struct student *head = NULL;
    struct student *prv, *corrent;
    printf("Please input student information,input\\n to quit\n");
    printf("intput 1 to start,q to quit:");
    while ((scanf("%d",&choice))==1) //录入信息
    {    printf("intput your student number:");
        while ((ch = getchar()) != '\n')
        {
            continue;
        }
         gets(number);
        corrent = (struct student *)malloc(sizeof(struct student));
        if (head == NULL)
        {
            head = corrent;
        }
        else
            prv->next = corrent;
        strcpy(corrent->number, number);
        printf("please input your name:");
        gets(corrent->name);
        printf("please input your grade:");
        scanf("%d %d %d", &corrent->garde[0], &corrent->garde[1], &corrent->garde[2]);
        while ((ch = getchar()) != '\n')
        {
            continue;
        }
        prv = corrent;
        printf("please input next student information\n");
        printf("intput 1 to start,q to quit:");
    }
     while ((ch = getchar()) != '\n')
        {
            continue;
        }
    printf("1.链表部插入新学生结点    2.链表尾部插入新学生结点\n");
    printf("3.在某一学号学生结点之前插入新学生    4.输出该链表全部学生信息\n");
    printf("5.删除指定学号的学生结点\n");
    printf("请输入你要查找的功能:(输入q退出)");
    while ((scanf("%d", &choice)) == 1)
    {
        while ((ch = getchar()) != '\n')
        {
            continue;
        }
        switch (choice)
        {
        case 1:
             head=head_insertion(head); //链表头部插入新学生结点
            break;
        case 2:
             head=tail_insertion(head); //链表尾部插入新学生结点
            break;
        case 3:
             head=number_insertion(head); //在某一学号学生结点之前插入新学生
            break;
        case 4:
             s_printf(head); //输出该链表全部学生信息
            break;
        case 5:
             head=_delete (head); //删除指定学号的学生结点
            break;
        default:
            break;
        }
        printf("请输入你要查找的功能:(输入q退出)");
    }

    system("pause");
    return 0;
}
struct student* head_insertion(struct student *k)//表头插入
{
    char ch;
    struct student *put=NULL,*put2=k;
    put = (struct student *)malloc(sizeof(struct student));
    put->next = put2;
    printf("please input your number:");
    gets(put->number);
    printf("please input your name:");
    gets(put->name);
    printf("please input your grade:");
    scanf("%d %d %d", &put->garde[0], &put->garde[1], &put->garde[2]);
    while ((ch = getchar()) != '\n')
    {
        continue;
    }
    put2 = put;
    return k;
}
struct student* tail_insertion(struct student *k)//表尾插入
{
    char ch;
    struct student *put=NULL,*put2=k;
    put = (struct student *)malloc(sizeof(struct student));
    while (put2 != NULL)
    {
        put2 = put2->next;
    }
    put->next = put2;
    printf("please input your number:");
    gets(put->number);
    printf("please input your name:");
    gets(put->name);
    printf("please input your grade:");
    scanf("%d %d %d", &put->garde[0], &put->garde[1], &put->garde[2]);
    while ((ch = getchar()) != '\n')
    {
        continue;
    }
    put2 = put;
    return k;
}
struct student* number_insertion(struct student *k)//学号结点前插入
{
    char number[10], ch;
    struct student *put=NULL,*put2=k;
    put = (struct student *)malloc(sizeof(struct student));
    printf("please input student number:");
    gets(number);
    for (; put2 != NULL; put2 = put2->next)
    {
        if (strcmp(number, put2->number) == 0)
        {
            put->next = put2;
            printf("please input your number:");
            gets(put->number);
            printf("please input your name:");
            gets(put->name);
            printf("please input your grade:");
            scanf("%d %d %d", &put->garde[0], &put->garde[1], &put->garde[2]);
            while ((ch = getchar()) != '\n')
            {
                continue;
            }
            put2 = put;
            break;
        }
    }
    return k;
}
 void s_printf(struct student *k)//显示结果
{   struct student*put=k;
    while (put!=NULL)
    {
        printf("%s %s %d %d %d\n",k->number,k->name,k->garde[0],k->garde[1],k->garde[2]);
        put=put->next;
    }
    
}
struct student* _delete (struct student *k)//删除结点
{   struct student *put=k;
    char number[10];
    printf("please input the number of student you want to delete:");
    gets(number);
    for ( ; put!=NULL; put=put->next)
    {
          if (strcmp(number,put->next->number)==0)
          {
              put->next=put->next->next;
              break;
          }
          
    }
return k;
}

img

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 1月3日
    • 创建了问题 12月26日

    悬赏问题

    • ¥15 ul做导航栏格式不对怎么改?
    • ¥20 用户端如何上传图片到服务器和数据库里
    • ¥15 现在研究生在烦开题,看了一些文献,但不知道自己要做什么,求指导。
    • ¥30 vivado封装时总是显示缺少一个dcp文件
    • ¥100 pxe uefi启动 tinycore
    • ¥15 我pycharm运行jupyter时出现Jupyter server process exited with code 1,然后打开cmd显示如下
    • ¥15 可否使用carsim-simulink进行四轮独立转向汽车的联合仿真,实现四轮独立转向汽车原地旋转、斜向形式、横移等动作,如果可以的话在carsim中如何进行相应设置
    • ¥15 Caché 2016 在Java环境通过jdbc 执行sql报Parameter list mismatch错误,但是同样的sql使用连接工具可以查询出数据
    • ¥15 疾病的获得与年龄是否有关
    • ¥15 opencv.js内存,CPU飙升