#include #include #include "studlist.h" int main() { Record r; /* リストの初期化 */ /* headノードの内容は意味のある情報でなくてよい */ r.id = 0; head = make_1node( r, NULL ); listprint(); /* 新しいノードをリストに追加する */ /* CTL+D でループを抜ける */ while (1) { printf("Insert new student data: (ID Surname Givenname GPA) -> "); if (scanf("%d %s %s %lf", &r.id, r.surname, r.givenname, &r.gpa) != 4) { printf("\n"); break; } if (insert(r) == NULL) { printf("ID %d is already on the list!\n", r.id); } listprint(); } printf("\n"); return 0; } NodePointer insert(Record r) { NodePointer newnode; if (finditem(r.id) == NULL) {    /* 重複するノードの検索 */ newnode = make_1node(r, head->next); /* headの下に新しいノードを作成 */ head->next = newnode; /* headの下に新しいノードをつなぐ */ return newnode; } else return NULL; } void listprint(void) { NodePointer n; printf("Head - \n"); for(n = head->next; n != NULL; n = n->next){    /* headの次のノードからNULLの前までたどる */ printf(" %7d %-12s %-12s %5.2f\n", n->data.id, n->data.surname, n->data.givenname, n->data.gpa); } printf("\n"); } NodePointer finditem(int sid) { NodePointer n; for (n = head; n->next != NULL; n = n->next) { /* headからNULLの一つ前までのノードをたどる */ if (n->next->data.id == sid) return n; /* sidと同じ値が見つかったら一つ手前のノードを指すポインタを返す */ } return NULL; } NodePointer make_1node(Record r, NodePointer p) { NodePointer n; if ((n = (NodePointer) malloc(sizeof(struct node)) ) == NULL) {   /* メモリ確保 */ printf("Error in memory allocation\n"); exit(8); } n->data = r; n->next = p; return n; }