|
struct list {
char name[12];//名前
int num; //番号
struct test{
int kadai; //課題番号
int seigo[10];//正誤データ
}
struct list *next;//次のデータ
};
というような入れ子の構造体を作りたいと考えているのですが、
testの構造体に複数のデータを入れる(自己参照型にする)方法はありますか?
それとも二つの構造体を別々に扱わなければいけないのでしょうか?
説明ページの解説も読んだのですが、
自分では解決できなかったので助言をいただければと思います。
入れ子の構造体test無しの入力は下のソースを使っています。
メイン関数
int main( void )
{
struct list *head= NULL;
int code;
while( 1 ) {
printf( "機能→入力:%d 表示:%d 終了:%d\n" ,ADD,PRINT,END);
printf( " 入力>>" );
scanf( "%d", &code );
if ( code == 9 ) break;
else if ( code == 1 ){ head = write(code, head);
}else if ( code == 5 ){ dispall(head);
}else{ fflush(stdin);
}
}
free_list( head ); /* リストの開放 */
return 0;
}
外部関数
struct list *write(int code, struct list *head)
{
char name[10];
int Num;
printf( "Name:" ); scanf( "%s", name );
printf( "Number:" );scanf("%d",&Num);
head = asort( name, Num, head );
return(head);
}
void dispall( struct list *p )
{
while ( p != NULL) {
printf( "%s %d\n", p->name, p->Num );
p = p->next;
}
}
void free_list( struct list *p )
{
struct list *p2;
while ( p != NULL ) {
p2 = p->next;
free( p );
p = p2;
}
}
struct list *sort( char *str, int Num, struct list *head )
{
struct list *p, *new_p;
if ( ( new_p = ( struct list * )malloc( sizeof( struct list ) ) ) == NULL ) {
printf( "malloc error\n" ); exit( 1 );
}
strcpy( new_p->name, str );
new_p->Num = Num;
if ( head == NULL || Num > head->Num ) {
new_p->next = head;
return new_p;
}
for ( p = head; p->next != NULL; p = p->next )
if ( Num > p->next->Num ) break;
new_p->next = p->next;
p->next = new_p;
return head;
}
|