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