| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
# include <stdlib.h> 
# include <malloc.h> 
# include <stdio.h> 
 
struct Node 
{ 
        int data; //数据域 
        struct Node * pNext; //指针域 
}; 
 
//函数声明 
struct Node * create_list(void); 
void traverse_list(struct Node *); 
 
int main(void) 
{ 
        struct Node * pHead = NULL; //等价于 struct Node * pHead = NULL; 
 
        pHead = create_list();  //create_list()功能:创建一个非循环单链表,并将该链表的头结点的地址付给pHead 
        traverse_list(pHead); 
         
        return 0; 
} 
 
struct Node * create_list(void) 
{ 
        int len;  //用来存放有效节点的个数 
        int i; 
        int val; //用来临时存放用户输入的结点的值 
 
        //分配了一个不存放有效数据的头结点 
        struct Node * pHead = (struct Node *)malloc(sizeof(struct Node)); 
        if (NULL == pHead) 
        { 
                printf("分配失败, 程序终止!\n"); 
                exit(-1); 
        } 
        struct Node * pTail = pHead; 
        pTail->pNext = NULL; 
 
        printf("请输入您需要生成的链表节点的个数: len = "); 
        scanf("%d", &len); 
         
        for (i=0; i<len; ++i) 
        { 
                printf("请输入第%d个节点的值: ", i+1); 
                scanf("%d", &val); 
                 
                struct Node * pNew = (struct Node *)malloc(sizeof(struct Node)); 
                if (NULL == pNew) 
                { 
                        printf("分配失败, 程序终止!\n"); 
                        exit(-1);  //终止程序 
                } 
                pNew->data = val; 
                pTail->pNext = pNew; 
                pNew->pNext = NULL; 
                pTail = pNew; 
        } 
         
        return pHead; 
} 
 
void traverse_list(struct Node * pHead) 
{ 
        struct Node * p = pHead->pNext; 
 
        while (NULL != p) 
        { 
                printf("%d  ", p->data); 
                p = p->pNext; 
        } 
        printf("\n"); 
         
        return; 
} 
 
程序在visual studio运行出现一堆错误不知如何解决,希望大家帮助: 
 
下面为错误提示 
1>------ 已启动生成: 项目: list_1, 配置: Debug Win32 ------ 
1>生成启动时间为 2015/9/28 15:23:21。 
1>InitializeBuildStatus: 
1>  正在对“Debug\list_1.unsuccessfulbuild”执行 Touch 任务。 
1>ClCompile: 
1>  list_1.c 
1>f:\c语言\list_1\list_1\list_1.c(38): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 
1>f:\c语言\list_1\list_1\list_1.c(39): error C2065: “pTail”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(39): error C2223: “->pNext”的左侧必须指向结构/联合 
1>f:\c语言\list_1\list_1\list_1.c(49): error C2143: 语法错误 : 缺少“;”(在“类型”的前面) 
1>f:\c语言\list_1\list_1\list_1.c(50): error C2065: “pNew”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(50): warning C4047: “==”:“void *”与“int”的间接级别不同 
1>f:\c语言\list_1\list_1\list_1.c(55): error C2065: “pNew”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(55): error C2223: “->data”的左侧必须指向结构/联合 
1>f:\c语言\list_1\list_1\list_1.c(56): error C2065: “pTail”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(56): error C2223: “->pNext”的左侧必须指向结构/联合 
1>f:\c语言\list_1\list_1\list_1.c(56): error C2065: “pNew”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(57): error C2065: “pNew”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(57): error C2223: “->pNext”的左侧必须指向结构/联合 
1>f:\c语言\list_1\list_1\list_1.c(58): error C2065: “pTail”: 未声明的标识符 
1>f:\c语言\list_1\list_1\list_1.c(58): error C2065: “pNew”: 未声明的标识符 
1> 
1>生成失败。 
1> 
1>已用时间 00:00:00.46 
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ========== |   
 
 
 
 |