创建结构数组

时间:2012-09-12 20:23:37

标签: c arrays data-structures linked-list

我一直忙着写一本关于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。

我无法弄明白。

你能给我一个算法吗(创建一个有序的结构数组)。

最后,抱歉我的英语不好,我可能会犯一些语法错误......

提前致谢。

3 个答案:

答案 0 :(得分:1)

要订购元素,您需要对它们进行排序。在C中,您可能希望使用qsort(在C++中有更简单的方法)。您需要在struct student *上定义一个比较函数,然后使用它调用数组上的qsort

请参阅this example获取灵感。请注意,您的数组是一个结构指针的数组,示例是一个直接结构数组(这可能是您想要的吗?)。

答案 1 :(得分:0)

  

你能给我一个算法吗(创建一个有序的结构数组)。

如果要创建一个有序的结构数组,可能需要构建一个

有这样的图书馆,但要学习和理解,你可以谷歌'C'中的二进制树或类似的东西,例如:

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

树将允许您的用户插入未排序的值并按排序顺序检索它们(也可以更快地搜索它们)。

答案 2 :(得分: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++;
    }
}

第二种方式,二进制搜索树:

我更改了头文件的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);
}

可能不完全解决书籍问题但是有了你的帮助,是必要部分的解决方案。如果你发现了一个错误。随意添加评论。

相关问题