計算機科学のブログ

ほしい物リスト

C - データ構造と動的メモリ - 架け橋を築く - リスト, NULL

Head First C ―頭とからだで覚えるCの基本David Griffiths(著)、 Dawn Griffiths(著)、 中田 秀基(監修)、 木下 哲也(翻訳)、 O’Reilly Media)の6章(データ構造と動的メモリ - 架け橋を築く)、p.273(コードマグネット)の解答を求めてみる。

Makefile

all: main.c
	cc main.c && ./a.out

コード

main.c

#include <stdio.h>

typedef struct island
{
    char *name;
    char *opens;
    char *closes;
    struct island *next;

} island;

void display(island *start)
{
    for (island *i = start; i != NULL; i = i->next)
    {
        printf("名前:%s 営業時間:%s-%s\n", i->name, i->opens, i->closes);
    }
}
int main()
{
    island isla_nublar = {"イスラヌブラル",
                          "09:00",
                          "17:00",
                          NULL};
    island skull = {"スカル",
                    "09:00",
                    "17:00",
                    NULL};
    island shutter = {"シャッター",
                      "09:00",
                      "17:00",
                      NULL};
    isla_nublar.next = &skull;
    skull.next = &shutter;
    display(&isla_nublar);
}

入出力結果(Terminal, Zsh)

% make
cc main.c && ./a.out
名前:イスラヌブラル 営業時間:09:00-17:00
名前:スカル 営業時間:09:00-17:00
名前:シャッター 営業時間:09:00-17:00
%