C言語なら俺に聞け(入門篇) Part 30
■ このスレッドは過去ログ倉庫に格納されています
0703デフォルトの名無しさん
2008/06/22(日) 16:10:29struct list {
int key; /* キー */
char name[20]; /* 名前 */
struct list *next; /* 次のデータへのポインタ */
};
struct list *add_list(int key, char *name, struct list *head)
{
struct list *prev, *cur;
struct list *p = malloc(sizeof *p);
if (p == NULL) { printf("out of memory\n"); exit(1); }
p->key = key;
strcpy(p->name, name);
p->next = NULL;
if (head == NULL || strcmp(name, head->name) < 0) {
p->next = head;
return p;
}
for (prev = cur = head; (cur = cur->next) != NULL; prev = cur) {
if (strcmp(name, cur->name) < 0) {
p->next = cur;
break;
}
}
prev->next = p;
return head;
}
■ このスレッドは過去ログ倉庫に格納されています