首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

哪位高手能帮小弟我看看这个程序错哪了

2012-04-08 
谁能帮我看看这个程序哪里错了#include stdio.h#include malloc.htypedef struct node{int datastruc

谁能帮我看看这个程序哪里错了
#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;} 

热点排行