簡単にリストの利点・欠点や考え方のおさらいをやったあと、 リストデータを生成するプログラムの実例
struct List* cons( int x , struct List*p ) { struct List* n ; n = (struct List*)malloc( sizeof( struct List ) ) ; if ( n != NULL ) { n->data = x ; n->next = p ; } return n ; } void main() { // 単純な機械的生成 struct List* top = cons( 1 , cons( 2 , cons( 3 , NULL ) ) ) ; // 先頭に挿入型 struct List* top = NULL ; int x ; while( scanf( "%d" , &x ) == 1 ) top = cons( x , top ) ; // 末尾連結型 struct List* top = NULL ; struct List** tail = &top ; while( scanf( "%d" , &x ) == 1 ) { (*tail) = cons( x , NULL ) ; tail = &( (*tail)->next ) ; } }
基礎プログラムを見せた後、時間が余ったので来週のネタの入り口として、 リスト構造による Stack の説明