ポインタについて

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
もも

ポインタについて

#1

投稿記事 by もも » 11年前

今、構造体を勉強中なのですが下のプログラムで、

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;
        }
}

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: ポインタについて

#2

投稿記事 by みけCAT » 11年前

もも さんが書きました:今、構造体を勉強中なのですが下のプログラムで、

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にどうして*が付いているのかが分かりません。
教えてください。お願いします。
struct list型のデータを指すポインタを返す関数だからです。
これらの2関数では、更新後のリストの先頭のポインタを返し、呼び出し元のデータを更新できるようにしているようです。

コード:

struct list* add_list( int key, char* str, struct list* head );
struct list* del_list( int key, struct list* head );
と書いても同じ意味になります。
オフトピック
ついでに、C++なら変数/関数の戻り値/関数の仮引数のstructを外して

コード:

list* add_list( int key, char* str, list* head );
list* del_list( int key, list* head );
のように書けます。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

閉鎖

“C言語何でも質問掲示板” へ戻る