鱼C论坛

 找回密码
 立即注册
查看: 4337|回复: 1

刚刚接触链表,出现问题,大牛帮忙看下问题出现在哪里,

[复制链接]
发表于 2013-9-19 16:56:53 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 ♂季_末♀ 于 2013-9-19 16:58 编辑

复制代码


找了许久,还是出现问题提示,请打牛帮忙,感谢。。
  1. # include <stdio.h>
  2. # include <malloc.h>
  3. # include <stdlib.h>


  4. struct Node
  5. {
  6.         int data;
  7.         struct Node * pNext;
  8. };

  9. struct Node * create_list(void);
  10. void traverse_list (struct Node * );

  11. int main (void)
  12. {
  13.         struct Node * pHead = NULL;

  14.         pHead = create_list();

  15.         traverse_list(pHead);

  16.         return 0;

  17. }

  18. struct Node * create_list(void)
  19. {
  20.         int len;  //用来存放有效节点的个数
  21.         int i;
  22.         int val; //用来临时存放用户输入的结点的值

  23.         //分配了一个不存放有效数据的头结点
  24.         struct Node * pHead = (struct Node *)malloc(sizeof(struct Node));
  25.         if (NULL == pHead)
  26.         {
  27.                 printf("分配失败, 程序终止!\n");
  28.                 exit(-1);
  29.         }
  30.         struct Node * pTail = pHead;
  31.         pTail->pNext = NULL;

  32.         printf("请输入您需要生成的链表节点的个数: len = ");
  33.         scanf("%d", &len);
  34.        
  35.         for (i=0; i<len; ++i)
  36.         {
  37.                 printf("请输入第%d个节点的值: ", i+1);
  38.                 scanf("%d", &val);
  39.                
  40.                 struct Node * pNew = (struct Node *)malloc(sizeof(struct Node));
  41.                 if (NULL == pNew)
  42.                 {
  43.                         printf("分配失败, 程序终止!\n");
  44.                         exit(-1);  //终止程序
  45.                 }
  46.                 pNew->data = val;
  47.                 pTail->pNext = pNew;
  48.                 pNew->pNext = NULL;
  49.                 pTail = pNew;
  50.         }
  51.        
  52.         return pHead;
  53. }

  54. void traverse_list (struct Node * pHead)
  55. {
  56.         struct Node * p = pHead->pNext;

  57.         while(NULL != p)
  58.         {
  59.                 printf("%d", p->data);
  60.                 p = p->pNext;
  61.         }
  62.         return ;
  63. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-9-19 16:59:06 | 显示全部楼层
找了许久,还是出现问题提示,请打牛帮忙,感谢。。


--------------------Configuration: 链表 - Win32 Debug--------------------
Compiling...
链表.c
D:\Hei\编程程序\链表.c(38) : error C2143: syntax error : missing ';' before 'type'
D:\Hei\编程程序\链表.c(39) : error C2065: 'pTail' : undeclared identifie
D:\Hei\编程程序\链表.c(39) : error C2223: left of '->pNext' must point to struct/union
D:\Hei\编程程序\链表.c(49) : error C2143: syntax error : missing ';' before 'type'
D:\Hei\编程程序\链表.c(50) : error C2065: 'pNew' : undeclared identifier
D:\Hei\编程程序\链表.c(50) : warning C4047: '==' : 'void *' differs in levels of indirection from 'int '
D:\Hei\编程程序\链表.c(55) : error C2223: left of '->data' must point to struct/union
D:\Hei\编程程序\链表.c(56) : error C2223: left of '->pNext' must point to struct/union
D:\Hei\编程程序\链表.c(57) : error C2223: left of '->pNext' must point to struct/union
执行 cl.exe 时出错.

链表.obj - 1 error(s), 0 warning(s)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-6 15:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表