glib - 单链表

时间:2017-05-18 21:16:15

标签: c glib singly-linked-list

我试图进入glib库,所以我想我会尝试做一些简单的程序来实现glib的单链表。 一切正常,直到我尝试使用2个功能: g_slist_nth和g_slist_nth_data。 我不知道我做错了什么。也许你可以帮我看清楚事情。相关的行是:64 - 70。 我这样编译:

  

gcc singly_linked_list.c pkg-config --cflags --libs glib-2.0

谢谢。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

struct person {
    char *name;
    int age;
};

struct person *new_person(char *name, int age);
void free_person(gpointer person);
void printf_person(gpointer person, gpointer data);
void printf_int(void *integer, void *formatstring);


int main(void) {
    GSList *list_int, *list_people;

    // EXAMPLE 1
    // Create a sample list of int
    for (int i = 5; i > 0; i--) {
        list_int = g_slist_prepend(list_int, GINT_TO_POINTER(i));
    }
    // print list
    printf("===== list of integers =====\n\n");
    g_slist_foreach(list_int, printf_int, "Node %d: int = %d\n"); 
    // free list
    g_slist_free(list_int);

    printf("\n");

    // EXAMPLE 2
    // create a sample list of people
    for (int i = 1; i <= 5; i++) {
        struct person *new_p = new_person(NULL, 0);
        char *name = (char *) malloc(11 * sizeof(char)); //+1 for \0

        if (new_p && name) {
            char num[sizeof(int)]; // is this a valid hack?

            // convert int to string
            sprintf(num, "%d", i);
            // setup the name
            strcpy(name, "Person ");    
            strcat(name, num);

            // set new_person data
            new_p->name = name;
            new_p->age = i * 9; // just vary the age

            list_people = g_slist_append(list_people, (gpointer) new_p);
        } else {
            if(new_p)
                free(new_p);
            else
                free(name);
        }
    }
    // print list
    printf("\n===== list of people =====\n\n");
    g_slist_foreach(list_people, printf_person, NULL);

    // WHY DOESNT THIS WORK?
    // gpointer data = g_slist_nth_data(list_people, 1);
    // printf_person(data, NULL);

    // NEITHER DOES THIS
    // GSList *node = g_slist_nth(list_people, 0);
    // printf_person(node->data, NULL);

    // free list
    g_slist_free_full(list_people, free_person);

    return 0;
}

// Allocates space for a new struct person
struct person *new_person(char *name, int age) {
    struct person *new_person = (struct person *) malloc(sizeof(struct person));
    if (new_person) {
        new_person->name = name;
        new_person->age = age;
    }

    return new_person;
}

// frees the allocated space for struct person and its data
void free_person(gpointer person) {
    free(((struct person *) person)->name);
    free((struct person *) person);
}   

// print function for person
void printf_person(gpointer person, gpointer unused) {
    printf("Name: %s\nAge: %d\n\n", ((struct person *) person)->name, ((struct person *) person)->age);
}

// print function for int
void printf_int(gpointer integer, gpointer formatstring) {
    static int ctr = 1;

    printf((char *) formatstring, ctr++, GPOINTER_TO_INT(integer)); 
}   

0 个答案:

没有答案