链表程序错误求解
下面在指定位置插入元素的链表程序,VS提示错误,请问什么原因?大神求指导#include<stdio.h>
#include<stdlib.h>
struct list
{
int data;
struct list *next;
};
struct list *create_list() //建立一个节点
{
return (struct list *)calloc(1,sizeof(struct list));
};
void traverse(struct list *ls)//循环遍历链表
{
struct list *p = ls;
while(p)
{
printf("%d\n",p->data);
p = p->next;
}
};
struct list *insert_list(struct list *ls, int n, int data)//在指定位置插入元素
{
struct list *p = ls;
while(p && n--)
{
p->next;
}
if(p == NULL)
{
return NULL;
}
struct list *node = create_list();
node->data = data;
node->next = p->next;
p->next = node;
return node;
};
int main()
{
struct list *first = create_list(); //堆中创建一个节点
struct list *second = create_list();
struct list *third = create_list();
first->next = second;
second->next = third;
third->next = NULL;
first->data = 1;
second->data = 2;
third->data = 3;
insert_list(first,1,30);
traverse(first);
return 0;
}
结果:
1> main.c
1>c:\users\zhang\desktop\项目\链表\链表\main.c(38): error C2143: 语法错误 : 缺少“;”(在“类型”的前面)
1>c:\users\zhang\desktop\项目\链表\链表\main.c(39): error C2065: “node”: 未声明的标识符
1>c:\users\zhang\desktop\项目\链表\链表\main.c(39): error C2223: “->data”的左侧必须指向结构/联合
1>c:\users\zhang\desktop\项目\链表\链表\main.c(40): error C2065: “node”: 未声明的标识符
1>c:\users\zhang\desktop\项目\链表\链表\main.c(40): error C2223: “->next”的左侧必须指向结构/联合
1>c:\users\zhang\desktop\项目\链表\链表\main.c(41): error C2065: “node”: 未声明的标识符
1>c:\users\zhang\desktop\项目\链表\链表\main.c(41): warning C4047: “=”:“list *”与“int”的间接级别不同
1>c:\users\zhang\desktop\项目\链表\链表\main.c(42): error C2065: “node”: 未声明的标识符
1>c:\users\zhang\desktop\项目\链表\链表\main.c(42): warning C4047: “return”:“list *”与“int”的间接级别不同