哪位高手能帮小弟我看看这个程序错哪了
谁能帮我看看这个程序哪里错了
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
}l;
void main()
{
l *head=(l*)malloc(sizeof(l));
l *r;
int x[10];
head->next=NULL;
r=head;
int i=0;
printf("请输入十个数字:\n");
for(i=0;i<10;i++)
scanf("%d",x[i]);
for(i=0;i<10;i++)
{
l *p=(l*)malloc(sizeof(l));
p->data=x[i];
p->next=NULL;
r->next=p;
r=r->next;
};
while(head->next!=NULL)
{
printf("%d ",head->data);
head=head->next;
};
}
以指针方式输入10个数并输出
------解决方案--------------------
两个错误
(1)scanf("%d",&x[i]); //这里要有&
(2)第一个head->data没有赋值
------解决方案--------------------
for(i=0;i<10;i++)
scanf("%d",&x[i]);
for(i=0;i<10;i++)
{
l *p=(l*)malloc(sizeof(l));
p->data=x[i];
p->next=NULL;
r->data = p->data;//加一句 r->next=p;
r=r->next;
};
------解决方案--------------------
#include <stdio.h>
#include <malloc.h>
typedef struct node
{
int data;
struct node *next;
}l;
void main()
{
l *head=(l*)malloc(sizeof(l));
l *r;
int x[10];
head->next=NULL;
r=head;
int i=0;
printf("请输入十个数字:\n");
for(i=0;i<10;i++)
scanf("%d",x[i]);
for(i=0;i<10;i++)
{
l *p=(l*)malloc(sizeof(l));
p->data=x[i];
p->next=NULL;
r->next=p;
r=r->next;
};
while(head->next!=NULL)
{
printf("%d ",head->data);
head=head->next;
};
}
以指针方式输入10个数并输出
------解决方案--------------------
两个错误
(1)scanf("%d",&x[i]); //这里要有&
(2)第一个head->data没有赋值
------解决方案--------------------
for(i=0;i<10;i++)
scanf("%d",&x[i]);
for(i=0;i<10;i++)
{
l *p=(l*)malloc(sizeof(l));
p->data=x[i];
p->next=NULL;
r->data = p->data;//加一句 r->next=p;
r=r->next;
};
------解决方案--------------------
- C/C++ code
#include <stdio.h> #include <stdlib.h> #include <malloc.h> typedef struct node { int data; struct node *next; }List; int main(int argc, char* argv[]) { List *head=(List*)malloc(sizeof(List)); List *p; int nArray[10]; head->next=NULL; p=head; int i=0; printf("请输入十个数字:\n"); for(i=0;i<10;i++) scanf("%d",&nArray[i]); for(i=0;i<10;i++) { List *q=(List*)malloc(sizeof(List)); q->data=nArray[i]; q->next=NULL; p->next=q; p=p->next; }; p = head->next; while(p) { printf("%d ",p->data); p=p->next; }; system("pause"); return 0; }