簡體   English   中英

如何調用鏈表中的特定條目? C

[英]How do I call specific entries in a linked list? c

我終於想出了如何制作和存儲鏈表的方法。 我可以打印,但是只能匯總打印。 我想要做的是像從數組中那樣從列表中提取一組特定值。

到目前為止,這是我的代碼。

#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,假裝這是一秒鍾的數組,我想將變量定義為char manufacturer[] = A_car[3] .make,稍后再調用,我也想定義char model[] = A_car[3] .model, int year[] = 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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM