/* リストの先頭要素を削除する */ #include #include typedef struct node{ int data; struct node *next; } NODE, *Nodep; void displist(Nodep fp); void delhead(Nodep fp); NODE first; /* 先頭ノードを指すポインタを含むノード */ main() { /* [1, 5, 9] というリストを作成 */ first.next = (Nodep)malloc(sizeof(NODE)); first.next->data = 1; first.next->next = (Nodep)malloc(sizeof(NODE)); first.next->next->data = 5; first.next->next->next = (Nodep)malloc(sizeof(NODE)); first.next->next->next->data = 9; first.next->next->next->next = NULL; displist(&first); delhead(&first); /* [5, 9] というリストになっているはず */ displist(&first); delhead(&first); /* [9] というリストになっているはず */ displist(&first); delhead(&first); /* empty リストになっているはず */ displist(&first); delhead(&first); /* empty リストのままのはず */ displist(&first); } void displist(Nodep fp) { printf("List"); if( fp->next == NULL ) { printf(" is empty\n"); return; } for( fp = fp->next ; fp != NULL; fp = fp->next ) { printf("%5d", fp->data ); } printf("\n"); } void delhead(Nodep fp) { Nodep p; if( p = fp->next ) { fp->next = p->next; free(p); } } /* end of llist4.c */