連結リスト

いろいろ手抜きな連結リスト

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

typedef struct person {
	int score;
	struct person *next;
} Person;

typedef struct {
	Person *head;
	Person *current;
} List;

void insert_front(List *list, int score) {
	Person *tmp = list->head;
	list->head = list->current = ((Person *) calloc(1, sizeof(Person)));
	list->head->next = tmp;
	list->head->score = score;
}

void remove_front(List *list) {
	if (list->head != NULL) {
		Person *tmp = (list->head)->next;
		free(list->head);
		list->head = list->current = tmp;
	}
}

void remove_current(List *list) {
	if (list->head != NULL) {
		if (list->current == list->head)
			remove_front(list);
		else {
			Person *tmp = list->head;

			while (tmp->next != list->current)
				tmp = tmp->next;
			tmp->next = list->current->next;
			free(list->current);
			list->current = tmp;
		}
	}
}

void remove_under_50(List *list) {
	list->current = list->head;

	while (list->current != NULL) {
		if (list->current->score < 50) {
			remove_current(list);
		}
		list->current = list->current->next;
	}

}

void show(List *list) {
	if (list->head == NULL) {
		puts("リストは空です。");
	} else {
		Person *tmp = list->head;

		while (tmp != NULL) {
			printf("score = %d\n", tmp->score);
			tmp = tmp->next;
		}

	}
}

int main(void) {
	List list;
	list.head = NULL;
	list.current = NULL;

	insert_front(&list, 32);
	insert_front(&list, 87);
	insert_front(&list, 50);
	insert_front(&list, 32);
	insert_front(&list, 92);
	show(&list);
	printf("\n");
	remove_under_50(&list);

	show(&list);

	return 0;
}