C言語で単方向リスト
戻る
::::::::::::::
list1.c
::::::::::::::
// $Id: list-by-c.html,v 1.1 2009/06/22 16:12:16 kishi Exp kishi $
#include <stdio.h>
typedef struct list {
int value;
struct list *next;
}
LIST;
LIST * head;
void init() {
head = ( LIST* ) malloc( sizeof( LIST ) );
head->value = 0;
head->next = NULL;
}
void append( int value ) {
LIST * p = head;
while ( p->next != NULL ) {
p = p->next;
}
LIST* new = ( LIST* ) malloc( sizeof( LIST ) );
p->next = new;
new->value = value;
new->next = NULL;
}
void enumerate() {
LIST * p = head;
while ( p->next != NULL ) {
printf( "%d\n", p->value );
p = p->next;
}
printf( "%d\n", p->value );
}
int main() {
init();
int i;
for ( i = 1;i < 10;i++ ) {
append( i );
}
enumerate();
return 0;
}
■実行結果
$ ./list1.exe
0
1
2
3
4
5
6
7
8
9
戻る