主题:请帮忙看一下有什么问题
请帮忙看一下有什么问题
照着书上敲的也编译不过[em10]
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *pnext;
}NODE, *PNODE;
PNODE create_list (void);
void traverse_list (PNODE phead);
int main (void)
{
PNODE phead = NULL;
phead = create_list();
traverse_list (phead);
return 0;
}
PNODE create_list (void)
{
int i;
int len;
int val;
PNODE phead = (PNODE) malloc (sizeof(NODE));
if (phead == NULL)
{
printf ("分配内存失败\n");
exit (-1);
}
PNODE ptail = phead;
ptail->pnext = NULL;
printf ("请输入节点的个数 len = ");
scanf ("%d", &len);
for (i = 0; i < len; ++i)
{
printf ("请输入%d个节点值 val = ", i+1);
scanf ("%d ", &val);
PNODE pnew = (PNODE) malloc (sizeof(NODE));
if (pnew == NULL)
{
printf ("分配内存失败\n");
exit (-1);
}
pnew->data = val;
ptail->pnext = pnew;
pnew->pnext = NULL;
ptail = pnew;
}
return phead;
}
void traverse_list (PNODE phead)
{
PNODE p = phead->pnext;
while (p != NULL)
{
printf ("%d ", p->data);
p = p->pnext;
}
return;
}
下面是编译报错
--------------------Configuration: 31 - Win32 Debug--------------------
Compiling...
31.c
D:\c1\31.c(35) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\c1\31.c(8) : see declaration of 'PNODE'
D:\c1\31.c(35) : error C2146: syntax error : missing ';' before identifier 'ptail'
D:\c1\31.c(35) : error C2065: 'ptail' : undeclared identifier
D:\c1\31.c(35) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\c1\31.c(36) : error C2223: left of '->pnext' must point to struct/union
D:\c1\31.c(43) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\c1\31.c(8) : see declaration of 'PNODE'
D:\c1\31.c(43) : error C2146: syntax error : missing ';' before identifier 'pnew'
D:\c1\31.c(43) : error C2065: 'pnew' : undeclared identifier
D:\c1\31.c(43) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\c1\31.c(44) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
D:\c1\31.c(49) : error C2223: left of '->data' must point to struct/union
D:\c1\31.c(50) : error C2223: left of '->pnext' must point to struct/union
D:\c1\31.c(51) : error C2223: left of '->pnext' must point to struct/union
Error executing cl.exe.
31.exe - 10 error(s), 3 warning(s)
照着书上敲的也编译不过[em10]
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *pnext;
}NODE, *PNODE;
PNODE create_list (void);
void traverse_list (PNODE phead);
int main (void)
{
PNODE phead = NULL;
phead = create_list();
traverse_list (phead);
return 0;
}
PNODE create_list (void)
{
int i;
int len;
int val;
PNODE phead = (PNODE) malloc (sizeof(NODE));
if (phead == NULL)
{
printf ("分配内存失败\n");
exit (-1);
}
PNODE ptail = phead;
ptail->pnext = NULL;
printf ("请输入节点的个数 len = ");
scanf ("%d", &len);
for (i = 0; i < len; ++i)
{
printf ("请输入%d个节点值 val = ", i+1);
scanf ("%d ", &val);
PNODE pnew = (PNODE) malloc (sizeof(NODE));
if (pnew == NULL)
{
printf ("分配内存失败\n");
exit (-1);
}
pnew->data = val;
ptail->pnext = pnew;
pnew->pnext = NULL;
ptail = pnew;
}
return phead;
}
void traverse_list (PNODE phead)
{
PNODE p = phead->pnext;
while (p != NULL)
{
printf ("%d ", p->data);
p = p->pnext;
}
return;
}
下面是编译报错
--------------------Configuration: 31 - Win32 Debug--------------------
Compiling...
31.c
D:\c1\31.c(35) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\c1\31.c(8) : see declaration of 'PNODE'
D:\c1\31.c(35) : error C2146: syntax error : missing ';' before identifier 'ptail'
D:\c1\31.c(35) : error C2065: 'ptail' : undeclared identifier
D:\c1\31.c(35) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\c1\31.c(36) : error C2223: left of '->pnext' must point to struct/union
D:\c1\31.c(43) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\c1\31.c(8) : see declaration of 'PNODE'
D:\c1\31.c(43) : error C2146: syntax error : missing ';' before identifier 'pnew'
D:\c1\31.c(43) : error C2065: 'pnew' : undeclared identifier
D:\c1\31.c(43) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct node *'
D:\c1\31.c(44) : warning C4047: '==' : 'int ' differs in levels of indirection from 'void *'
D:\c1\31.c(49) : error C2223: left of '->data' must point to struct/union
D:\c1\31.c(50) : error C2223: left of '->pnext' must point to struct/union
D:\c1\31.c(51) : error C2223: left of '->pnext' must point to struct/union
Error executing cl.exe.
31.exe - 10 error(s), 3 warning(s)