我终于弄清楚如何制作和存储链表了。可以打印,但是只能汇总打印。我想要做的是从列表中提取一组特定值,就像提取数组一样。
这是到目前为止我的代码。
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct car car_t;
struct car {
char make[21], model[21];
int year;
float vmax, mass, seats;
};
typedef struct car_node cnode_t;
struct car_node {
car_t data;
cnode_t *next;
};
typedef struct {
cnode_t *head;
cnode_t *foot;
} list_t;
void Print_Car(car_t A_Car) {
static int count = 1;
fprintf(stdout,"%d) Make: %s - Model: %s - First Manufactured: %d - Mass: - %f Max Speed: %f - Seats: %f \n", count++, A_Car.make, A_Car.model,A_Car.year,A_Car.mass,A_Car.vmax,A_Car.seats);
}
void Print_Car_to_File(car_t A_Car, FILE *AnotherFP) {
fprintf(AnotherFP,"%s %s %d %f %f %f", A_Car.make, &A_Car.model, &A_Car.year, &A_Car.mass, &A_Car.vmax, &A_Car.seats);}
int Read_Car_from_File(car_t *A_Car, FILE *Reading) {
int result;
result = fscanf(Reading, "%s %s %d %f %f %f", &A_Car->make, &A_Car->model, &A_Car->year, &A_Car->mass, &A_Car->vmax, &A_Car->seats);
return result;
}
list_t* make_empty_list(void) {
list_t *list;
list = (list_t*)malloc(sizeof(*list));
assert(list != NULL);
list->head = list->foot = NULL;
return list;
}
list_t* insert_at_foot(list_t *list, car_t value) {
cnode_t *new;
new = (cnode_t*)malloc(sizeof(*new));
assert(list != NULL && new != NULL);
new->data = value;
new->next = NULL;
if(list->foot==NULL)
list->head = list->foot = new;
else {
list->foot->next = new;
list->foot = new;
}
return list;
}
void print_list(list_t *list) {
cnode_t *car_to_print;
assert(list != NULL);
if(list->head == NULL) {
printf("List is empty!!\n");
} else {
car_to_print = list->head;
while(car_to_print != NULL) {
Print_Car(car_to_print->data);
car_to_print = car_to_print->next;
}
}
}
int main() {
list_t *list_of_cars;
car_t Car_Buffer;
FILE *fp;
char filename[] = "vehicle.crash";
list_of_cars = make_empty_list();
if((fp = fopen(filename,"r")) != NULL) {
while(Read_Car_from_File(&Car_Buffer, fp) != EOF) {
insert_at_foot(list_of_cars, Car_Buffer);
}
fclose(fp);
print_list(list_of_cars);
}
else
printf("Failed to open %s for writing\n",filename);
return 0;
}
这是它从中提取的文本文件
Toyota Camry 1991 1100 200 5
Bugatti Veyron 2005 1888 415 2
Enviro400 Bus 2005 18000 129 90
Honda NSX 1992 1350 315 2
运行时我得到这个
1) Make: Toyota - Model: Camry - First Manufactured: 1991 - Mass: - 1100.000000
Max Speed: 200.000000 - Seats: 5.000000
2) Make: Bugatti - Model: Veyron - First Manufactured: 2005 - Mass: - 1888.00000
0 Max Speed: 415.000000 - Seats: 2.000000
3) Make: Enviro400 - Model: Bus - First Manufactured: 2005 - Mass: - 18000.00000
0 Max Speed: 129.000000 - Seats: 90.000000
4) Make: Honda - Model: NSX - First Manufactured: 1992 - Mass: - 1350.000000 Max
Speed: 315.000000 - Seats: 2.000000
然后用户出现并选择 4,假装这是一个数组,我想将变量定义为 charManufacturer[] = A_car[3]
.make 来调用之后,我还想定义 char model[] = A_car[3]
.model、intyear[] = A_car[3]
.year 等等向前。
我该怎么做?我尝试查找示例,但似乎找不到有助于实现此目的的函数,并且这可能只是因为我没有完全获取链接列表。
最佳答案
void Print_Car_to_File(car_t A_Car, FILE *AnotherFP) {
fprintf(AnotherFP,"%s %s %d %f %f %f", A_Car.make, A_Car.model, A_Car.year, A_Car.mass, A_Car.vmax, A_Car.seats);
}
int main() {
list_t *list_of_cars;
car_t Car_Buffer;
FILE *fp;
char filename[] = "vehicle.crash";
int num_car = 0;//number of cars
list_of_cars = make_empty_list();
if((fp = fopen(filename,"r")) != NULL) {
while(Read_Car_from_File(&Car_Buffer, fp) != EOF) {
insert_at_foot(list_of_cars, Car_Buffer);
++num_car;//count car
}
fclose(fp);
print_list(list_of_cars);
//make array from list
car_t *cars = malloc(num_car * sizeof(*cars));
cnode_t *nodep = list_of_cars->head;
for(int i = 0; i < num_car; ++i){
cars[i] = nodep->data;
nodep = nodep->next;
}
car_t veyron = cars[1];
puts("");
Print_Car_to_File(veyron, stdout);
free(cars);
}
else
printf("Failed to open %s for writing\n",filename);
return 0;
}
关于c - 如何调用链接列表中的特定条目? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37363408/