| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 830 人关注过本帖
标题:请帮忙看一下有什么问题
取消只看楼主 加入收藏
zheng031024
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
请帮忙看一下有什么问题
#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)
搜索更多相关主题的帖子: void 内存 include return 有心人 
2012-07-04 21:56
zheng031024
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
收藏
得分:0 
请高人给看一下吧!急啊
2012-07-04 23:13
zheng031024
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-4
收藏
得分:0 
以下是引用随风飘荡在2012-7-5 00:12:39的发言:

变量定义都放在函数头就可以了

PNODE create_list (void)
{
    int i;
    int len;
    int val;
    PNODE phead = (PNODE) malloc (sizeof(NODE));
    PNODE ptail=phead;
    PNODE pnew;
    ptail->pnext = NULL;
    /*……………………………………*/
}
谢谢! 真的可以
但这是为什么呢??????????
2012-07-05 04:49
快速回复:请帮忙看一下有什么问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.037244 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved