首页 新闻 赞助 找找看

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

0
悬赏园豆:5 [已解决问题] 解决于 2013-01-27 14:09

源代码如下:

 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 }

 

使用VC++6.0编译后,报错如下:

--------------------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这个类型定义哪里有问题

 

求各位大大帮我~~~

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

问题补充:

在这里先感谢两位的热心帮助。。。。。。

按照Edward_jie的提示,我将36行NULL这一错误改正过后,系统报错提示为:

--------------------Configuration: test - 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(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 - 17 error(s), 4 warning(s)

和改正之前区别只是少了NULL该处的错误提示。

 

按照kunkka_的提示,我看了遍traversr_list函数的代码,

貌似我少写了一行 pTem = pTem->pNext

不过加上之后,系统的报错还是没变。。。。。。

各种纠结啊,麻烦两位再帮帮我~~~

王海舟的主页 王海舟 | 菜鸟二级 | 园豆:206
提问于:2012-05-28 19:13
< >
分享
最佳答案
0

36行的NULL你后面的两个LL写成小写的了,其他地方好像还有不妥,你先把这里改了运行一下吧

收获园豆:5
Edward_诺 | 菜鸟二级 |园豆:209 | 2012-05-28 22:13

我按照您的提示改正之后仍然不能运行,报错提示和之前相同,您能帮我再看看么,多谢多谢。。。

王海舟 | 园豆:206 (菜鸟二级) | 2012-05-30 23:43

@Winterwill: 你把改完之后的代码和错误再贴一遍,那里是个很明显的NULL写成NUll的错误,应该会有变化的

Edward_诺 | 园豆:209 (菜鸟二级) | 2012-05-31 18:24

@Edward_jie: 我将改完后的错误提示贴出来了,代码就是将36行一处的NUll改成了NULL,您再帮我看看~~~

王海舟 | 园豆:206 (菜鸟二级) | 2012-06-03 10:24

@Winterwill: 我这里没有vc6.0。不过你可以单步调试下看看,看看那儿报错。然后设个断点,重新单步调试。查看调用堆栈的情况。或者你感觉哪里不确定是否正确,就先注释掉试试。就是把整个程序拆开。一个个验证

kunkka_ | 园豆:273 (菜鸟二级) | 2012-06-03 10:30
其他回答(3)
0

我拷贝到自己电脑上运行了下,就是楼上所说的错误。改过来之后就能正常运行了。楼主代码写的很规范呀,继续努力,加油!

kunkka_ | 园豆:273 (菜鸟二级) | 2012-05-28 22:53

您确定在您的电脑上能运行么?为什么我运行不了呢。。。。。。

支持(0) 反对(0) 王海舟 | 园豆:206 (菜鸟二级) | 2012-05-30 23:44

@Winterwill: 我是vs2008上,确实能运行。但是,你的程序写的不对。稍微修改下就行了。

支持(0) 反对(0) kunkka_ | 园豆:273 (菜鸟二级) | 2012-05-31 21:52

vs2008能运行。vc6.0没试过,应该没问题。但是你的Traverse函数写的不对,少了一行。

支持(0) 反对(0) kunkka_ | 园豆:273 (菜鸟二级) | 2012-05-31 21:59

@kunkka_: 

 1 void traverse_list(PNODE pHead)
 2 {
 3       PNODE pTem = pHead->pNext;
 4       while(NULL != pTem)
 5       {
 6           printf("%d",pTem->data);
 7           pTem = pTem->pNext;
 8       } 
 9       printf("\n");
10 }
11 
12 貌似是少了这一行pTem = pTem->pNext;

但加上之后系统报错还是木有变呀,您再帮着看看~~~

支持(0) 反对(0) 王海舟 | 园豆:206 (菜鸟二级) | 2012-06-03 10:27
0

应该先申请结点空间,判断内存是否分配成功,再输入VAL。修改完后,在VC上运行正确~

jionian0211 | 园豆:202 (菜鸟二级) | 2015-01-24 16:51
0

可以试一下,把定义放函数内容前面,有些软件版本是这样的

Wx120 | 园豆:244 (菜鸟二级) | 2021-09-22 09:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册