0

私は C の本からの質問で忙しくしています。質問は単純ですが、いくつかの特定の部分があります。

配列について質問したいです。私の質問は、構造体の配列を作成する最良の方法についてです。質問はこれらすべてを望んでいます。まず、構造体の配列を作成します。次に、これらの配列を restp ポインタに接続するリンク リストを作成します。質問をサブパートに分割したいと思います。最初の部分は構造体の配列です...構造体の配列を作成するにはどうすればよいですか。私はこれについて調査を行いました。そして、ここに私の方法があります:私は構造の配列の構造を作成しています:

struct student{
    int id;
    struct courseList_node_s *restp;
};

そして、質問の残りを完了するための私のリンクされたリスト:

typedef struct courseList_node_s{
    char course[6];
    int credit,
        section;
    struct courseList_node_s *restp;
}courseList_node_t;

この学生のスケジュールを処理する関数を実装しました。私の get_studentList 関数で; 配列を次のように宣言しました。

struct student *ansp[size];

そして、メモリ割り当てを行います。

ansp[i] = malloc(sizeof(struct student));

最後に値を割り当てます。

ansp[i]->id =id;

さて、私の問題は、配列の作成中に、順序付き配列として作成できなかったことです。たとえば、ユーザーは 1111、1222、1232、1011 と入力できます。したがって、配列の最初の要素は ansp[0] = 1011、ansp[1] = 1111 です。

私は理解できませんでした。

これらを構成するアルゴリズムを教えてください(構造の順序付き配列の作成)。

最後に、私の下手な英語で申し訳ありません。文法的な間違いを犯した可能性があります...

前もって感謝します。

4

3 に答える 3

1

要素を並べ替えるには、並べ替える必要があります。ではC、おそらく を使用したいと思うでしょうqsort(C++もっと簡単な方法があります)。比較関数を定義し、それを使用して配列をstruct student *呼び出す必要があります。qsort

インスピレーションについては、この例を参照してください。あなたの配列は構造ポインタの配列であることに注意してください。例は直接構造の配列です(これはおそらくあなたが望んでいたものですか?)。

于 2012-09-12T20:31:10.100 に答える
0

これらを構成するアルゴリズムを教えてください(構造の順序付き配列の作成)。

構造の順序付けられた配列を作成したい場合は、おそらくツリーを構築する必要があります。

そのためのライブラリがありますが、学習して理解するには、「Cのバイナリツリー」などをグーグルで検索できます。

http://www.macs.hw.ac.uk/~rjp/Coursewww/Cwww/tree.html

ツリーを使用すると、ユーザーはソートされていない値を挿入し、それらをソートされた順序で取得できます (また、より迅速に検索できます)。

于 2012-09-12T20:36:39.823 に答える
0

@Keith Randall と lserni の助けを借りて問題を解決しました。バイナリ検索ツリーと構造の配列の両方を実装しました。

最初の方法、qsort で配列を並べ替える:

比較関数を作成する必要がありました:

int compare(const void *p1, const void *p2){
    return (* (struct student **) p1)->id - (* (struct student **) p2)->id;
}

そして私の他のヘルパー関数;

void get_studentList(struct student **listp,int size){
    int id,i;
    struct student *ansp[size];
    for(i=0;i<size;i++){
        printf("Enter student's id to exit enter -1> ");
        scanf("%d", &id);
        ansp[i] = malloc(sizeof(courseList_node_t));
        ansp[i]->id = id;
        ansp[i]->restp = NULL;
    }
    qsort (ansp, size, sizeof(struct student *), compare);
    for(i=0;i<size;i++){
        listp[i] = ansp[i];
    }
}


courseList_node_t * insert_studentSchedule(courseList_node_t *headp, int size){
    courseList_node_t *cur_nodep;
    if(headp == NULL){
        cur_nodep = scan_course();
        headp = cur_nodep;
    } else {
        headp->restp = insert_studentSchedule(headp->restp,size);
    }
    return (headp);
}

そして私の表示機能;

void display_schedule(struct student **headp, int size){
    courseList_node_t *cur_nodep;
    int i = 0;
    while(i< size){
        cur_nodep = headp[i]->restp;
        printf("Student id > %d\n", headp[i]->id);
        while(cur_nodep != NULL){
            printf("Course name> %s\t", cur_nodep->course);
            printf("Course credit> %d\t", cur_nodep->credit);
            printf("Course section> %d\n", cur_nodep->section);
            cur_nodep = cur_nodep->restp;
        }
        i++;
    }
}

2 番目の方法、二分探索木:

ヘッダー ファイルの typedef 部分を次のように変更しました。

typedef struct tree_node_s{
    int id;
    struct courseList_node_s *restp;
    struct tree_node_s *leftp, *rightp;
}tree_node_t;

そして、ノードの動的割り当てで標準パターンを形式化する私のマクロ:

#define TYPED_ALLOC(type) (type *)malloc(sizeof(type))

そして、二分探索木を作成する私の実装:

/*
 * Insert a new id in a binary search tree. 
 * Pre: rootp points to the root node of a binary search tree
 */
tree_node_t * get_studentTree(tree_node_t *rootp, int newId)
{
    if (rootp == NULL){
        rootp = TYPED_ALLOC(tree_node_t);
        rootp->id = newId;
        rootp->restp = NULL;
        rootp->leftp = NULL;
        rootp->rightp = NULL;
    } else if ( newId == rootp->id){
        /* */
    } else if (newId < rootp->id){
        rootp->leftp = get_studentTree(rootp->leftp, newId);
    } else {
        rootp->rightp = get_studentTree(rootp->rightp, newId);
    }
    return (rootp);
}

この部分はこの質問とは関係ありません。本当の問題の部分的な解決策を共有したいので、それらを与えました。

/*
 * Its aim to add courses to restp component of subtree
 * It may have some problems. And you can omit it. Because it not related with this question
 * Pre: elementp not empty
 */
courseList_node_t * add_course(courseList_node_t *nextp, courseList_node_t *elementp){
        if(nextp->restp == NULL){
            nextp->restp = elementp;
        } else {
            nextp->restp = add_course(nextp->restp,elementp);
        }
        return (nextp);
}

/*
 * It is not neccessary to first call get_studentTree function. It simply creates a linked list which consist of student class/lecture schedule.
 * Pre: ele and id not empty
 * Post: Tree returned includes all schedule and retains binary search tree properties.
 */
tree_node_t * insert_studentSchedule(tree_node_t *rootp,courseList_node_t *ele, int id){
    if (rootp == NULL){
        rootp = get_studentTree(rootp, id);
        rootp->restp = TYPED_ALLOC(courseList_node_t);
        strcpy(rootp->restp->course, ele->course);
        rootp->restp->credit = ele->credit;
        rootp->restp->section = ele->section;
    }
    else if(rootp->id == id){
        if ( rootp->restp == NULL ){
            rootp->restp = TYPED_ALLOC(courseList_node_t);
            strcpy(rootp->restp->course, ele->course);
            rootp->restp->credit = ele->credit;
            rootp->restp->section = ele->section;
        } else {
            rootp->restp = add_course(rootp->restp, ele);
        }

    } else if ( id < rootp->id ){
        if ( rootp->leftp != NULL )
            rootp->leftp = insert_studentSchedule(rootp->leftp, ele, id);
    } else if ( id > rootp->id ) {
        if ( rootp->rightp != NULL )
            rootp->rightp = insert_studentSchedule(rootp->rightp, ele, id);
    }
    return (rootp);
}

/*
 * Course scanning function
 */
courseList_node_t * scan_course(void){
    courseList_node_t *cur_coursep;
    char courseName[6];
    cur_coursep = (courseList_node_t *)malloc(sizeof(courseList_node_t));

    printf("Welcome to course scanning part>\n");
    printf("Enter the name of course> ");
    scanf("%s", courseName);
    strcpy(cur_coursep->course, courseName);
    printf("Enter the credit of course> ");
    scanf("%d", &cur_coursep->credit);
    printf("Enter the section of course> ");
    scanf("%d", &cur_coursep->section);
    cur_coursep->restp = NULL;

    return (cur_coursep);
}

/*
 * My way to print binary search tree with all elements
 */
void display_schedule(tree_node_t *rootp){
    courseList_node_t *cur_course;
    if(rootp == NULL)
        return;
    display_schedule(rootp->leftp);
    if (rootp->restp == NULL)
        printf("Tree with id: %d element has no member!", rootp->id);
    else {
        cur_course = rootp->restp;
        while (cur_course != NULL){
            printf("Student Id> %d\n", rootp->id);
            printf("Course name> %s\t", rootp->restp->course);
            printf("Course credit> %d\t", rootp->restp->credit);
            printf("Course section> %d\n", rootp->restp->section);
            cur_course = cur_course->restp;
        }
    }
    display_schedule(rootp->rightp);
}

本の質問の完全な解決策ではないかもしれませんが、あなたの助けを借りて、それは本質的な部分の解決策です. 間違いを見つけた場合。お気軽にコメントを追加してください。

于 2012-09-13T22:00:05.807 に答える