#include <stdio.h>
#include <stdlib.h>
/*
* https://ja.wikipedia.org/wiki/連結リスト#.E9.80.A3.E7.B5.90.E3.83.AA.E3.82.B9.E3.83.88.E3.81.A8.E9.85.8D.E5.88.97
*/
typedef struct node {
int key;
struct node *next;
} Node;
Node *head;
void insert(int);
void print_keys();
int find(int);
void free_list();
int main(){
int i;
head = malloc(sizeof (struct node));
head->key = -1;
head->next = NULL;
for(i=0;i<10;i++){
insert(i);
}
printf("==find==\n");
printf("5: %d\n", find(5));
printf("15: %d\n", find(15));
printf("\n");
print_keys();
free_list();
return 0;
}
void insert(int key){
Node *new_node = malloc(sizeof (struct node));
new_node->key = key;
new_node->next = head->next;
head->next = new_node;
}
void print_keys(){
Node *p;
for(p=head->next; p->next != NULL; p=p->next){
printf("%d ", p->key);
}
printf("\n");
}
int find(int key){
Node *p;
for(p=head->next; p->next != NULL; p=p->next){
if(p->key == key){
return 1;
}
}
return 0;
}
void free_list(){
Node *p, *tmp;
p = head;
while(p != NULL){
tmp = p->next;
free(p);
p = tmp;
}
}