|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #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 ;
- }
复制代码 啊啊啊啊啊啊啊啊啊啊啊啊,求助,在线等
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 ;
}
|
|