鱼C论坛

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

[已解决]链表输入问题

[复制链接]
发表于 2021-2-2 14:58:28 | 显示全部楼层 |阅读模式

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

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

x
请问为什么输入有问题?

#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct nb)
int n;

struct nb
{
        long xh;
        float cj;
        struct nb *next;
};

struct nb *p()
{
        n=0;
        struct nb *head,*p1,*p2;
        p1=p2=(struct nb *)malloc(LEN);
        head=NULL;
        scanf("%d,%f",&p1->xh,&p1->cj);
        while(p1->xh)
        {
                n=n+1;
                if(n==1)
                        head=p1;
                else
                        p2->next=p1;
                p2=p1;
                p1=(struct nb *)malloc(LEN);
                scanf("%d,%f",p1->xh,p1->cj);
        }
        p2->next=NULL;
        return head;
}

void sc(struct nb *head)
{
        struct nb *p;
        p=head;
        if(head)
        {
                do
                {
                        printf("%d   %f",p->xh,p->cj);
                        p=p->next;
                }while(p);
        }
}

void main()
{
        struct nb *b;
        b=p();
        sc(b);
}



最佳答案
2021-2-3 15:44:05
问题多多
  1. p1=(struct nb *)malloc(LEN);
  2.                 scanf("%d,%f",p1->xh,p1->cj);
复制代码

这里要用
  1. scanf("%d,%f",&p1->xh,&p1->cj);
复制代码

scanf函数的引号里面的逗号输入的时候要输
  1. void main()
  2. {
  3.         struct nb *b;
  4.         b=p();
  5.         sc(b);
  6. }
复制代码

这里的函数p()的返回值是在p里面定义的,调用完就被释放了,不能这样做 ,要在main里面定义,然后在p函数里用二维指针修改
还有,链表用完了要释放内存,加了一个release函数
这是我改完的
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define LEN sizeof(struct nb)

  4. struct nb
  5. {
  6.         long xh;
  7.         float cj;
  8.         struct nb *next;
  9. };

  10. void p(struct nb **head)
  11. {
  12.         struct nb *p1,*p2;
  13.         p1=p2=(struct nb *)malloc(LEN);
  14.         scanf("%d%f",&p1->xh,&p1->cj);
  15.         while(p1->xh)//第一个数字输入0时结束
  16.         {
  17.                 if((*head)==NULL)
  18.                         (*head)=p1;
  19.                 else
  20.                         p2->next=p1;
  21.                        
  22.                 p2=p1;
  23.                 p1=(struct nb *)malloc(LEN);
  24.                 scanf("%d%f",&p1->xh,&p1->cj);
  25.         }
  26.         p2->next=NULL;
  27. }

  28. void sc(struct nb *head)
  29. {
  30.         struct nb *p;
  31.         p=head;
  32.         if(head!=NULL)
  33.         {
  34.                 do
  35.                 {
  36.                         printf("%d   %f\n",p->xh,p->cj);
  37.                         p=p->next;
  38.                 }
  39.                 while(p!=NULL);
  40.         }
  41. }

  42. void release(struct nb **head)
  43. {
  44.         struct nb *temp;
  45.         while((*head)!=NULL)
  46.         {
  47.                 temp=*head;
  48.                 *head=(*head)->next;
  49.                 free(temp);
  50.         }
  51. }

  52. int main(void)
  53. {
  54.         struct nb *head=NULL;
  55.        
  56.         p(&head);
  57.         sc(head);
  58.         release(&head);
  59.        
  60.         return 0;
  61. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-2-2 15:07:08 | 显示全部楼层
本帖最后由 jackz007 于 2021-2-2 15:10 编辑
  1.         scanf("%d,%f",&p1->xh,&p1->cj);
  2. . . . . . .
  3.               scanf("%d,%f",p1->xh,p1->cj);
复制代码

      去掉 scanf() 函数格式描述符之间的逗号试试,像这样
  1.         scanf("%d%f",&p1->xh,&p1->cj);
  2. . . . . . .
  3.               scanf("%d%f",p1->xh,p1->cj);
复制代码

        scanf() 格式描述符字符串中,最好不要出现与格式描述符无关的任何字符。像楼主的这种情况,在键盘输入的时候,两个数之间必须要有逗号相隔。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-2-3 15:44:05 | 显示全部楼层    本楼为最佳答案   
问题多多
  1. p1=(struct nb *)malloc(LEN);
  2.                 scanf("%d,%f",p1->xh,p1->cj);
复制代码

这里要用
  1. scanf("%d,%f",&p1->xh,&p1->cj);
复制代码

scanf函数的引号里面的逗号输入的时候要输
  1. void main()
  2. {
  3.         struct nb *b;
  4.         b=p();
  5.         sc(b);
  6. }
复制代码

这里的函数p()的返回值是在p里面定义的,调用完就被释放了,不能这样做 ,要在main里面定义,然后在p函数里用二维指针修改
还有,链表用完了要释放内存,加了一个release函数
这是我改完的
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. #define LEN sizeof(struct nb)

  4. struct nb
  5. {
  6.         long xh;
  7.         float cj;
  8.         struct nb *next;
  9. };

  10. void p(struct nb **head)
  11. {
  12.         struct nb *p1,*p2;
  13.         p1=p2=(struct nb *)malloc(LEN);
  14.         scanf("%d%f",&p1->xh,&p1->cj);
  15.         while(p1->xh)//第一个数字输入0时结束
  16.         {
  17.                 if((*head)==NULL)
  18.                         (*head)=p1;
  19.                 else
  20.                         p2->next=p1;
  21.                        
  22.                 p2=p1;
  23.                 p1=(struct nb *)malloc(LEN);
  24.                 scanf("%d%f",&p1->xh,&p1->cj);
  25.         }
  26.         p2->next=NULL;
  27. }

  28. void sc(struct nb *head)
  29. {
  30.         struct nb *p;
  31.         p=head;
  32.         if(head!=NULL)
  33.         {
  34.                 do
  35.                 {
  36.                         printf("%d   %f\n",p->xh,p->cj);
  37.                         p=p->next;
  38.                 }
  39.                 while(p!=NULL);
  40.         }
  41. }

  42. void release(struct nb **head)
  43. {
  44.         struct nb *temp;
  45.         while((*head)!=NULL)
  46.         {
  47.                 temp=*head;
  48.                 *head=(*head)->next;
  49.                 free(temp);
  50.         }
  51. }

  52. int main(void)
  53. {
  54.         struct nb *head=NULL;
  55.        
  56.         p(&head);
  57.         sc(head);
  58.         release(&head);
  59.        
  60.         return 0;
  61. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-2 04:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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