鱼C论坛

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

[已解决]求助...奇怪了,为什么没有输出呢?用6个指针数abcdef模拟链表,然后为什么没输出

[复制链接]
发表于 2015-5-9 22:49:39 | 显示全部楼层 |阅读模式

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

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

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef int datatype;
  4. typedef struct node
  5. {
  6.         datatype data;
  7.         struct node *next;
  8. }Node;
  9. Node *p,*head,*a,*b,*c,*d,*e,*f;
  10. int main()
  11. {
  12.         head=(Node*)malloc(sizeof(Node));head->next=a;
  13.         a=(Node*)malloc(sizeof(Node));a->data=1;a->next=b;
  14.         b=(Node*)malloc(sizeof(Node));b->data=2;b->next=c;
  15.         c=(Node*)malloc(sizeof(Node));c->data=3;c->next=d;
  16.         d=(Node*)malloc(sizeof(Node));d->data=4;d->next=e;
  17.         e=(Node*)malloc(sizeof(Node));e->data=5;e->next=f;
  18.         f=(Node*)malloc(sizeof(Node));f->data=6;f->next=NULL;       
  19.         for(p=head->next;p!=NULL;p=p->next)
  20.             printf("%d",p->data);
  21.         return 0 ;
  22. }
复制代码
啊啊啊啊啊啊啊啊啊啊啊啊,求助,在线等

最佳答案
2015-5-10 07:29:46
head=a;的时候  a 你还没分配
同理 a b c d e都是 你让他指定下一个 但他们还没分配
代码如下

#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
        datatype data;
        struct node *next;
}Node;
Node *p,*head,*a,*b,*c,*d,*e,*f;
int main()
{
        head=(Node*)malloc(sizeof(Node));

        f=(Node*)malloc(sizeof(Node));f->data=6;f->next=NULL;     
    e=(Node*)malloc(sizeof(Node));e->data=5;e->next=f;
    d=(Node*)malloc(sizeof(Node));d->data=4;d->next=e;
        c=(Node*)malloc(sizeof(Node));c->data=3;c->next=d;
    b=(Node*)malloc(sizeof(Node));b->data=2;b->next=c;
    a=(Node*)malloc(sizeof(Node));a->data=1;a->next=b;
  
        head->next=a;
        for(p=head->next;p!=NULL;p=p->next)
                printf("%d",p->data);
        return 0 ;
}
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2015-5-9 23:52:45 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. typedef int datatype;
  4. typedef struct node
  5. {
  6.         datatype data;
  7.         struct node *next;
  8. }Node;

  9. int main()
  10. {
  11.                 Node *p,*head,*a,*b,*c,*d,*e,*f;
  12.         head=(Node*)malloc(sizeof(Node));
  13.         a=(Node*)malloc(sizeof(Node));
  14.         b=(Node*)malloc(sizeof(Node));
  15.         c=(Node*)malloc(sizeof(Node));
  16.         d=(Node*)malloc(sizeof(Node));
  17.         e=(Node*)malloc(sizeof(Node));
  18.         f=(Node*)malloc(sizeof(Node));   
  19.                 head->next=a;   
  20.                 a->data=1;a->next=b;
  21.                 b->data=2;b->next=c;
  22.                 c->data=3;c->next=d;
  23.                 d->data=4;d->next=e;
  24.                 e->data=5;e->next=f;
  25.                 f->data=6;f->next=NULL;  
  26.         for(p=head->next;p!=NULL;p=p->next)
  27.             printf("%d\n",p->data);
  28.         return 0 ;
  29. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2015-5-10 07:29:46 | 显示全部楼层    本楼为最佳答案   
head=a;的时候  a 你还没分配
同理 a b c d e都是 你让他指定下一个 但他们还没分配
代码如下

#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
        datatype data;
        struct node *next;
}Node;
Node *p,*head,*a,*b,*c,*d,*e,*f;
int main()
{
        head=(Node*)malloc(sizeof(Node));

        f=(Node*)malloc(sizeof(Node));f->data=6;f->next=NULL;     
    e=(Node*)malloc(sizeof(Node));e->data=5;e->next=f;
    d=(Node*)malloc(sizeof(Node));d->data=4;d->next=e;
        c=(Node*)malloc(sizeof(Node));c->data=3;c->next=d;
    b=(Node*)malloc(sizeof(Node));b->data=2;b->next=c;
    a=(Node*)malloc(sizeof(Node));a->data=1;a->next=b;
  
        head->next=a;
        for(p=head->next;p!=NULL;p=p->next)
                printf("%d",p->data);
        return 0 ;
}

评分

参与人数 1荣誉 +5 鱼币 +5 收起 理由
~风介~ + 5 + 5 感谢楼主无私奉献!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-9 16:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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