struct list *add_list( int key, char *str, struct list *head );
struct list *del_list( int key, struct list *head );
(struct関数?)とありますが、
*add_listや*del_listにどうして*が付いているのかが分かりません。
教えてください。お願いします。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ADD 1
#define DEL 2
#define END 3
struct list {
int key; /* キー */
char name[20]; /* 名前 */
struct list *next; /* 次のデータへのポインタ */
};
struct list *add_list( int key, char *str, struct list *head );
struct list *del_list( int key, struct list *head );
void show_list( struct list *p );
int in_dt( int *key, char *name );
void free_list( struct list *p );
int main( void )
{
struct list *head; /* 先頭ポインタ */
char name[20];
int code, key;
head = NULL; /* 先頭ポインタにNULLを設定 */
while( 1 ) {
/* コード、キー、名前の入力 */
code = in_dt( &key, name );
if ( code == END )
/* 処理終了 */
break;
else if ( code == ADD )
/* リストにデータを登録 */
head = add_list( key, name, head );
else if ( code == DEL )
/* リストからデータを削除 */
head = del_list( key, head );
else
/* コード入力エラー */
printf( "コードの入力エラーです。再入力してください\n" );
}
/* リストの表示 */
show_list( head );
/* リストの開放 */
free_list( head );
return 0;
}
/*** リストにデータを登録 ***/
struct list *add_list( int key, char *str, struct list *head )
{
struct list *p, *new_p;
/* 新規リストにデータを登録 */
if ( ( new_p = ( struct list * )malloc( sizeof( struct list ) ) ) == NULL ) {
printf( "malloc error\n" );
exit( EXIT_FAILURE );
}
new_p->key = key;
strcpy( new_p->name, str );
/* キーが最大のとき */
if ( head == NULL || key > head->key ) {
/* ポインタのつなぎ換え */
new_p->next = head;
return new_p;
}
/* キーのサーチ */
for ( p = head; p->next != NULL; p = p->next )
if ( key > p->next->key ) break;
/* ポインタのつなぎ換え */
new_p->next = p->next;
p->next = new_p;
return head;
}
/*** リストからデータを削除 ***/
struct list *del_list( int key, struct list *head )
{
struct list *p, *old_p;
p = old_p = head;
/* キーのサーチ */
while ( p != NULL ) {
if ( key == p->key ) {
/* キーが一致したら */
if ( p == head )
head = p->next;
else
old_p->next = p->next;
free( p );
return head;
}
old_p = p;
p = p->next;
}
/* キーが見つからない */
printf( "キー%dが見つかりません\n", key );
return head;
}
/*** リストの表示 ***/
void show_list( struct list *p )
{
printf( "\nリストの表示\n" );
while ( p != NULL ) { /* 次ポインタがNULLまで処理 */
printf( "%3d %s\n", p->key, p->name );
p = p->next;
}
}
/*** コード、キー、名前の入力 ***/
int in_dt( int *key, char *name )
{
int code;
printf( "次の処理コードを選択しなさい\n" );
printf( "\t%d:リストの追加 %d:リストの削除 %d:処理終了\n", ADD, DEL, END );
scanf( "%d", &code );
if ( code == ADD ) {
printf( "KEYを入力\n" );
scanf( "%d", key );
printf( "名前を入力(MAX:19文字 )\n" );
scanf( "%19s", name );
}
else if ( code == DEL ) {
printf( "KEYを入力\n" );
scanf( "%d", key );
}
/* 処理コードの返却 */
return code;
}
/*** リストの開放 ***/
void free_list( struct list *p )
{
struct list *p2;
while ( p != NULL ) { /* 次ポインタがNULLまで処理 */
p2 = p->next;
free( p );
p = p2;
}
}