105 用 list_head 实现学生链表

#include <stdio.h>
#include "linux_list.h"

LIST_HEAD(students);

typedef struct tagIDCard {
    long serialNo;    // 6位数的序列号
    char name[32];    // 名称
} IDCard;

typedef struct tagStudent {
    char gender;    // F or M
    unsigned short age;
    IDCard id; 
    struct list_head node;
} Student;

void print_student(Student *ps)
{
    if (ps) {
        if (ps->id.serialNo < 200000)
            ps->id.serialNo += 200000;
        printf("%15s: %c\n"
                "%15s: %hd\n"
                "%15s: %ld\n"
                "%15s: %s\n",
                "Gender", ps->gender,
                "Age", ps->age,
                "ID SerialNo.", ps->id.serialNo,
                "ID name", ps->id.name);
        printf("-------------------------\n");
    }   
}

int main(int argc, char *argv[])
{
    Student s1 = { 'F', 20, { 200101, "x1" } };
    Student s2 = { 'M', 21, { 200102, "x2" } };
    Student s3 = { 'F', 22, { 200103, "x3" } };
    list_add_tail(&s1.node, &students);
    list_add_tail(&s2.node, &students);
    list_add_tail(&s3.node, &students);
    Student *ps, *n; 
    list_for_each_entry_safe(ps, n, &students, node)
        print_student(ps);
    return 0;
}