鱼C论坛

 找回密码
 立即注册
查看: 859|回复: 2

[已解决]输入学号和成绩到列表内,在输入学号删除相同学号的列表

[复制链接]
发表于 2020-11-17 22:15:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# include <stdio.h>
# include <stdlib.h>


int n;
struct student
{
    int num;
    float score;
    struct student *next;
};

struct student *addlist();
struct student *sublist(struct student * , int num);
void print(struct student *);

int main(void)
{
    int i;
    struct student *head;
    head = addlist();
    printf("请输入需要删除的学号,不需要输入‘0’退出\n");
    scanf("%d",&i);
    if(i != 0)
    {
        head = sublist(head ,i);
    }
    print(head);

   
}

struct student *addlist()
{
    struct student *p1 , *p2 , *head;

    do
    {
        int a ;
        a =  sizeof(struct student);
        p1 = malloc(a);
        p2 = p1;
        printf("输入学号\n");
        scanf("%d",&p1->num);
        printf("输入成绩\n");
        scanf("%f",&p1->score);
        if(p1->num != 0)
        {
            n = n+1;
            if(n == 1 )
            {
                head = p1;
            }
            else
            {
                p2->next = p1;
            }
            p2 = p1;
        }
    }while (p1->num != 0);
    p2->next = NULL;
    return  head;
}


struct student *sublist(struct student *head ,int num)
{
    struct student *p1 ,  *p2 ;
   
    p1 = head;
    if(head->num == num)
    {
        head = head->next;
        n = n -1;
    }
    else
    {
      
        while (p1 != NULL)
        {
            p2 =p1;
            p1 =p1->next;
            if(p1->num == num)
            {
                p2->next = p1->next;
                n= n-1;
                break;
            }
        }
    }
   
    return head;
}

void print(struct student *head)
{
    struct student *p1;
    p1 = head;
    printf("链表中共有%d\n个元素\n\n\n",n);
    while (p1 != NULL)
    {
        printf("学号:%d  成绩:%f \n",p1->num,p1->score);
        p1 = p1->next;
    }
   
}

报错:Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)
求大佬们帮我看看到底哪里出了问题·····十分感谢!!
最佳答案
2020-11-18 09:04:51
建立链表的函数有问题
  1. struct student *addlist()
  2. {
  3.     struct student *p1 , *p2 , *head;

  4.     do
  5.     {
  6.         int a ;
  7.         a =  sizeof(struct student);

  8.         p1 = (student *)malloc(a);
  9.                 //将next 赋空指针
  10.                 p1->next = 0;
  11.                
  12.                 //这里不能给P2赋值
  13.                 //p1为链表新节点,P2为链表尾节点,
  14.         //p2 = p1;

  15.         printf("输入学号\n");
  16.         scanf("%d",&p1->num);

  17.         printf("输入成绩\n");
  18.         scanf("%f",&p1->score);

  19.         if(p1->num != 0)
  20.         {
  21.             n = n+1;
  22.             if(n == 1 )
  23.             {
  24.                 head = p1;
  25.             }
  26.             else
  27.             {
  28.                 p2->next = p1;
  29.             }
  30.             p2 = p1;
  31.         }
  32.     }while (p1->num != 0);
  33.     p2->next = NULL;
  34.     return  head;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-11-18 09:04:51 | 显示全部楼层    本楼为最佳答案   
建立链表的函数有问题
  1. struct student *addlist()
  2. {
  3.     struct student *p1 , *p2 , *head;

  4.     do
  5.     {
  6.         int a ;
  7.         a =  sizeof(struct student);

  8.         p1 = (student *)malloc(a);
  9.                 //将next 赋空指针
  10.                 p1->next = 0;
  11.                
  12.                 //这里不能给P2赋值
  13.                 //p1为链表新节点,P2为链表尾节点,
  14.         //p2 = p1;

  15.         printf("输入学号\n");
  16.         scanf("%d",&p1->num);

  17.         printf("输入成绩\n");
  18.         scanf("%f",&p1->score);

  19.         if(p1->num != 0)
  20.         {
  21.             n = n+1;
  22.             if(n == 1 )
  23.             {
  24.                 head = p1;
  25.             }
  26.             else
  27.             {
  28.                 p2->next = p1;
  29.             }
  30.             p2 = p1;
  31.         }
  32.     }while (p1->num != 0);
  33.     p2->next = NULL;
  34.     return  head;
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-18 14:36:43 | 显示全部楼层
xieglt 发表于 2020-11-18 09:04
建立链表的函数有问题

感谢老哥。我马虎了!!!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-9 08:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表