#include<stdio.h> #include<stdlib.h>求大神看看,哪里错了,怎么运行不了

#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct node)

struct node
{
int date;
struct node *next;
};
struct node * initialize(int num)
{
struct node *head;
struct *p1, *p2;
p1=p2=(struct node*)malloc(SIZE);

printf("\nPlease input the valve of the node:\n");
scanf("%d",&p1->data);
head=p1;
while(num>1)
{
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(SIZE);
scanf("%d",&p1->data);
num--;
}
p2->next=p1;
p2=p1;
p2->next=NULL;
return head;
}
void traverse(struct node *head)
{
struct node *p;
printf("\nThe records are:\n");
p=head;
if(head!=NULL)
{
do
{
printf("%d",p->date);
p=p->next;
}while(p!=NULL);
}
printf("\n");
}
int main()
{
printf("Please input the number of nodes in list:");
int numOfNodes=0;
scanf("%d",&numOfNodes);
struct node *head=initialize(numOfNodes);
traverse(head);

return 0;}

第1个回答  2012-10-17
这个编译都不通过你没发现吗?其中scanf("%d",&p1->data);都改为scanf("%d",&p1->date);还有struct *p1, *p2;改为struct node *p1, *p2;就没问题了追问

改了这两个,编译仍然不能通过

追答

#include
#include
#define SIZE sizeof(struct node)

struct node
{
int date;
struct node *next;
};
struct node * initialize(int num)
{
struct node *head;
struct node *p1, *p2;
p1=p2=(struct node*)malloc(SIZE);

printf("\nPlease input the valve of the node:\n");
scanf("%d",&p1->date);
head=p1;
while(num>1)
{
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(SIZE);
scanf("%d",&p1->date);
num--;
}
p2->next=p1;
p2=p1;
p2->next=NULL;
return head;
}
void traverse(struct node *head)
{
struct node *p;
printf("\nThe records are:\n");
p=head;
if(head!=NULL)
{
do
{
printf("%d",p->date);
p=p->next;
}while(p!=NULL);
}
printf("\n");
}
int main()
{
printf("Please input the number of nodes in list:");
int numOfNodes=0;
scanf("%d",&numOfNodes);
struct node *head=initialize(numOfNodes);
traverse(head);

return 0;}
你复制过去看看,我用VS2008没有问题了

第2个回答  2012-10-17
scanf("%d",&p1->data);

有的地方是date,有的是data拼写不一致

遇到错误应该把错误信息贴出来,仅仅说运行不了,别人很难帮你
第3个回答  2012-10-17
以你的代码帮你该了一下
#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct node)

struct node
{
int date;
struct node *next;
};
struct node * initialize(int num)
{
struct node *head;
struct node *p1,*p2;
p1=p2=(struct node*)malloc(SIZE);

printf("\nPlease input the valve of the node:\n");
scanf("%d",&p1->date);
head=p1;
while(num>1)
{
p2->next=p1;
p2=p1;
p1=(struct node *)malloc(SIZE);
scanf("%d",&p1->date);
num--;
}
p2->next=p1;
p2=p1;
p2->next=NULL;
return head;
}
void traverse(struct node *head)
{
struct node *p;
printf("\nThe records are:\n");
p=head;
if(head!=NULL)
{
do
{
printf("%d",p->date);
p=p->next;
}while(p!=NULL);
}
printf("\n");
}
int main()
{
printf("Please input the number of nodes in list:");
int numOfNodes=0;
scanf("%d",&numOfNodes);
struct node *head=initialize(numOfNodes);
traverse(head);

return 0;}追问

(21) : error C2039: 'data' : is not a member of 'node'(8) : see declaration of 'node'(54) : error C2143: syntax error : missing ';' before 'type'(57) : warning C4047: 'function' : 'struct node *' differs in levels of indirection from 'int '(57) : warning C4024: 'traverse' : different types for formal and actual parameter 1Error executing cl.exe.121.obj - 5 error(s), 2 warning(s)运行不通过

本回答被提问者和网友采纳
相似回答