首页 新闻 会员 周边

菜鸟求助:数据结构(创建链表时遇到的问题)

0
[已关闭问题] 关闭于 2012-05-28 19:06
 1 #include<stdio.h>
 2 #include<malloc.h>
 3 #include<stdlib.h>
 4 
 5 typedef struct Node
 6 {
 7     int data;
 8     struct Node * pNext;
 9 }NODE, *PNODE;
10 
11 PNODE create_list();
12 void traverse_list(PNODE);
13 
14 void main()
15 {
16     PNODE pHead = create_list();
17     traverse_list(pHead);
18 }
19 
20 PNODE create_list()
21 {
22     int length;
23     int val;
24 
25     PNODE pHead = (PNODE)malloc(sizeof(NODE));
26     if(NULL == pHead)
27     {
28         printf("动态分配内存失败!\n");
29         exit(-1);
30     }
31     else
32     {
33         printf("分配内存成功!\n");
34     }
35     PNODE pTail = pHead;
36     pTail->pNext = NUll;
37 
38     printf("请输入你要创建的链表的长度:");
39     scanf("%d",&length);
40     for(int i=0;i<length;i++)
41     {
42         printf("请输入第%d个节点的值",i+1);
43         scanf("%d",&val);
44 
45         PNODE pNew = (PNODE)malloc(sizeof(NODE));
46         if(NULL == pNew)
47         {
48             printf("动态分配内存失败!\n");
49             exit(-1);
50         }
51         else
52         {
53             printf("分配内存成功!\n");
54         }
55 
56         pNew->data = val;
57         pTail->pNext = pNew;
58         pNew->pNext = NULL;
59         pTail = pNew;
60     }
61 
62     return pHead;
63 }
64 
65 void traverse_list(PNODE pHead)
66 {
67     PNODE pTem = pHead->pNext;
68     while(NULL != pTem)
69     {
70         printf("%d",pTem->data);
71     }
72     printf("\n");
73 }

--------------------Configuration: 链表 - Win32 Debug--------------------
Compiling...
test.c
D:\数据结构-练习\链表\test.c(35) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\数据结构-练习\链表\test.c(9) : see declaration of 'PNODE'
D:\数据结构-练习\链表\test.c(35) : error C2146: syntax error : missing ';' before identifier 'pTail'
D:\数据结构-练习\链表\test.c(35) : error C2065: 'pTail' : undeclared identifier
D:\数据结构-练习\链表\test.c(35) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct Node *'
D:\数据结构-练习\链表\test.c(36) : error C2223: left of '->pNext' must point to struct/union
D:\数据结构-练习\链表\test.c(36) : error C2065: 'NUll' : undeclared identifier
D:\数据结构-练习\链表\test.c(40) : error C2143: syntax error : missing ';' before 'type'
D:\数据结构-练习\链表\test.c(40) : error C2143: syntax error : missing ';' before 'type'
D:\数据结构-练习\链表\test.c(40) : error C2143: syntax error : missing ')' before 'type'
D:\数据结构-练习\链表\test.c(40) : error C2143: syntax error : missing ';' before 'type'
D:\数据结构-练习\链表\test.c(40) : error C2065: 'i' : undeclared identifier
D:\数据结构-练习\链表\test.c(40) : warning C4552: '<' : operator has no effect; expected operator with side-effect
D:\数据结构-练习\链表\test.c(40) : error C2059: syntax error : ')'
D:\数据结构-练习\链表\test.c(41) : error C2143: syntax error : missing ';' before '{'
D:\数据结构-练习\链表\test.c(45) : error C2275: 'PNODE' : illegal use of this type as an expression
D:\数据结构-练习\链表\test.c(9) : see declaration of 'PNODE'
D:\数据结构-练习\链表\test.c(45) : error C2146: syntax error : missing ';' before identifier 'pNew'
D:\数据结构-练习\链表\test.c(45) : error C2065: 'pNew' : undeclared identifier
D:\数据结构-练习\链表\test.c(45) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct Node *'
D:\数据结构-练习\链表\test.c(46) : warning C4047: '==' : 'void *' differs in levels of indirection from 'int '
D:\数据结构-练习\链表\test.c(56) : error C2223: left of '->data' must point to struct/union
D:\数据结构-练习\链表\test.c(57) : error C2223: left of '->pNext' must point to struct/union
D:\数据结构-练习\链表\test.c(58) : error C2223: left of '->pNext' must point to struct/union
Error executing cl.exe.

test.obj - 18 error(s), 4 warning(s)

 

问题:

这是一个创建链表和遍历输出的程序。

用VC++6.0编译后报错,看错误提示,似乎是定义的PNODE这个结构体指针类型有问题,导致pTail和pNew这两个变量都赋值失败,但如果是这样的话,为什么:

1 前面的pHead就赋值成功了,它也是PNODE类型呀

2 我也实在看不出来PNODE这个类型定义哪里有问题

 

求各位大大帮我~~~

一个还没入门的菜菜。。。。。。

王海舟的主页 王海舟 | 菜鸟二级 | 园豆:206
提问于:2012-05-28 19:02
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册