鱼C论坛

 找回密码
 立即注册
查看: 1628|回复: 1

[已解决]链表 新手 总在输入的时候崩溃 未释放内存 谢谢大佬

[复制链接]
发表于 2018-2-18 20:40:06 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
struct school
{
        int date;
        struct school *next;
};
void scan(struct school *student)
{
        printf("请输入数据");
        scanf("%d",student->date);
}
void addstudent(struct school **head)
{
        struct school *student=NULL,*temp=NULL;
        student=(struct school *)malloc(sizeof(struct school));
        if(student==NULL)
        {
                printf("内存分配失败");
                exit(1);
        }
        scan(student);
        printf("666");
        if(*head!=NULL)
        {
                temp=*head;
                *head=student;
                student->next=temp;
        }
        else
        {
                *head=student;
                student->next=NULL;
        }


}
void printstudent(struct school *head)
{
        struct school *student;
        student=head;
        while(student!=-NULL)
        {
                printf("%d",student->date);
                student=student->next;
        }
}
int main()
{
        struct school *head=NULL;
        char ch;
        while(1)
        {        printf("是否输入y/n?");
                scanf("%c",&ch);
                if(ch=='y')
                {
                        addstudent(&head);   
                }
                if(ch=='n')
                {
                        break;
                }
        }
        printf("是否打印");
        while(ch!='y'||ch!='n')
        {
                scanf("%c",&ch);
        }
        if(ch=='y')
        {
                printstudent(head);
        }


        return 0;
}
K2JX({98W80QT4C3~ND}P[2.png
最佳答案
2018-2-18 22:12:49
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct school
  4. {
  5.         int date;
  6.         struct school *next;
  7. };
  8. void scan(struct school *student)
  9. {
  10.         printf("请输入数据");
  11.         scanf("%d",&student->date); // 关键错误 &
  12.                 student->next = NULL;
  13. }
  14. void addstudent(struct school **head)
  15. {
  16.         struct school *student=NULL,*temp=NULL;
  17.         student=(struct school *)malloc(sizeof(struct school));
  18.         if(student==NULL)
  19.         {
  20.                 printf("内存分配失败");
  21.                 exit(1);
  22.         }

  23.         scan(student);

  24.         if(*head!=NULL)
  25.         {
  26.                 temp=*head;
  27.                 *head=student;
  28.                 (*head)->next=temp;
  29.         }
  30.         else
  31.         {
  32.                 *head=student;
  33.         }


  34. }
  35. void printstudent(struct school *head)
  36. {
  37.         struct school *student;
  38.         student=head;
  39.         while(student!=NULL)
  40.         {
  41.                 printf("%d\n",student->date);
  42.                 student=student->next;
  43.         }
  44. }
  45. int main()
  46. {
  47.         struct school *head=NULL;
  48.         char ch;
  49.         while(1)
  50.         {        printf("是否输入y/n?");
  51.                 scanf("%c",&ch);
  52.                 if(ch=='y')
  53.                 {
  54.                         addstudent(&head);   
  55.                 }
  56.                 if(ch=='n')
  57.                 {
  58.                         break;
  59.                 }
  60.                         while(getchar() != '\n') continue;
  61.         }
  62.        
  63.                 while(getchar() != '\n') continue;

  64.         printf("是否打印y/n?");
  65.                 scanf("%c",&ch);
  66.         while(ch!='y'||ch!='n')
  67.         {
  68.                        
  69.                                 if(ch=='y')
  70.                                 {
  71.                                         printstudent(head);
  72.                                         break;
  73.                                 }
  74.                                 while(getchar() != '\n') continue;
  75.                                 printf("是否打印y/n?");
  76.                                 scanf("%c",&ch);
  77.         }




  78.         return 0;
  79. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-18 22:12:49 | 显示全部楼层    本楼为最佳答案   
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. struct school
  4. {
  5.         int date;
  6.         struct school *next;
  7. };
  8. void scan(struct school *student)
  9. {
  10.         printf("请输入数据");
  11.         scanf("%d",&student->date); // 关键错误 &
  12.                 student->next = NULL;
  13. }
  14. void addstudent(struct school **head)
  15. {
  16.         struct school *student=NULL,*temp=NULL;
  17.         student=(struct school *)malloc(sizeof(struct school));
  18.         if(student==NULL)
  19.         {
  20.                 printf("内存分配失败");
  21.                 exit(1);
  22.         }

  23.         scan(student);

  24.         if(*head!=NULL)
  25.         {
  26.                 temp=*head;
  27.                 *head=student;
  28.                 (*head)->next=temp;
  29.         }
  30.         else
  31.         {
  32.                 *head=student;
  33.         }


  34. }
  35. void printstudent(struct school *head)
  36. {
  37.         struct school *student;
  38.         student=head;
  39.         while(student!=NULL)
  40.         {
  41.                 printf("%d\n",student->date);
  42.                 student=student->next;
  43.         }
  44. }
  45. int main()
  46. {
  47.         struct school *head=NULL;
  48.         char ch;
  49.         while(1)
  50.         {        printf("是否输入y/n?");
  51.                 scanf("%c",&ch);
  52.                 if(ch=='y')
  53.                 {
  54.                         addstudent(&head);   
  55.                 }
  56.                 if(ch=='n')
  57.                 {
  58.                         break;
  59.                 }
  60.                         while(getchar() != '\n') continue;
  61.         }
  62.        
  63.                 while(getchar() != '\n') continue;

  64.         printf("是否打印y/n?");
  65.                 scanf("%c",&ch);
  66.         while(ch!='y'||ch!='n')
  67.         {
  68.                        
  69.                                 if(ch=='y')
  70.                                 {
  71.                                         printstudent(head);
  72.                                         break;
  73.                                 }
  74.                                 while(getchar() != '\n') continue;
  75.                                 printf("是否打印y/n?");
  76.                                 scanf("%c",&ch);
  77.         }




  78.         return 0;
  79. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-3-29 04:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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