我正在尝试用这 3 个typedef struct
(人、统计数据和车辆)创建一个单链表。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char name[22];
char city[10];
int yearBorn;
} person;
typedef struct{
int height[8];
double weight[18];
} Stats;
typedef struct{
char car[16];
int horsepower[20];
} vehicle;
typedef struct node {
person *p;
Stats *stat;
vehicle *hp;
struct node *next;
}NODE;
typedef struct {
NODE *head, *tail;
}SLL;
int main(){
void insert(SLL *list, person p, vehicle hp, Stats stat){
a = (NODE *)malloc(sizeof(p));
b = (NODE *)malloc(sizeof(hp));
c = (NODE *)malloc(sizeof(stat));
if(list->head == NULL){
return NULL;
}
list -> head -> next = a;
list -> head -> next-> next = b;
list -> head -> next-> next -> next = c;
list -> tail = c;
}
return 0;
}
我不断收到a,b和c的错误,说错误:"a"未声明(首次在此函数中使用)。我只是想知道我是否正确执行此操作,以及此错误意味着什么。我非常感谢对我的代码的任何见解。谢谢。
问题就在这里
int main(){
void insert(SLL *list, person p, vehicle hp, Stats stat){
a = (NODE *)malloc(sizeof(p));
b = (NODE *)malloc(sizeof(hp));
c = (NODE *)malloc(sizeof(stat));
if(list->head == NULL){
return NULL;
}
list -> head -> next = a;
list -> head -> next-> next = b;
list -> head -> next-> next -> next = c;
list -> tail = c;
}
return 0;
}
如何在其他函数中定义函数?!!