小小小草 发表于 2015-5-9 22:49:39

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

#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));head->next=a;
        a=(Node*)malloc(sizeof(Node));a->data=1;a->next=b;
        b=(Node*)malloc(sizeof(Node));b->data=2;b->next=c;
        c=(Node*)malloc(sizeof(Node));c->data=3;c->next=d;
        d=(Node*)malloc(sizeof(Node));d->data=4;d->next=e;
        e=(Node*)malloc(sizeof(Node));e->data=5;e->next=f;
        f=(Node*)malloc(sizeof(Node));f->data=6;f->next=NULL;       
        for(p=head->next;p!=NULL;p=p->next)
          printf("%d",p->data);
        return 0 ;
}啊啊啊啊啊啊啊啊啊啊啊啊,求助,在线等

~风介~ 发表于 2015-5-9 23:52:45

#include <stdio.h>
#include <stdlib.h>
typedef int datatype;
typedef struct node
{
      datatype data;
      struct node *next;
}Node;

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

迷雾少年 发表于 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]
查看完整版本: 求助...奇怪了,为什么没有输出呢?用6个指针数abcdef模拟链表,然后为什么没输出