2012-09-12 135 views
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.

我无法确定数字出。

你可以给我一个包含这些的算法(创建一个有序的结构数组)。

最后,对不起,我的英语不好,我可能会做一些语法错误...

在此先感谢。

+0

'malloc(sizeof(struct student));'应该是'sizeof coursList_node_t'。 'student'是一个指向'courseList_node_t'的指针数组,不是'courseList_node_t'。 –

+0

我想,应该在用户操作后创建一个有序的数组,然后找到最小的 - >放入数组[0],找到最小的剩余 - >放入数组[1],....所以你可能需要学生结构中的另一个参数(例如,bool归档)来表示它是否已经在数组中。 – benbai123

回答

0

我已经解决了这个问题,帮助@Keith Randall和 lserni 我已经实现了二叉搜索树和结构数组。

第一种方式,与快速排序排序数组:

我不得不创建一个比较功能:

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)) 

和m y执行创建二叉搜索树:

/* 
* 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); 
} 

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

1

要排列元素,您需要对它们进行排序。在C中,您可能想要使用qsort(在C++中有更简单的方法)。您需要在struct student *上定义比较功能,并在您的阵列上使用它调用qsort

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

+0

感谢您的建议。它帮助我解决了我的问题。我也实现了二叉搜索树。但你的方式是我的问题的真正解决方案...谢谢, 我会尽快分享我的解决方案。 – mustafaSarialp

0

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

如果你想创建结构的orderd数组,你可能想建立一个

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

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

树木将允许用户插入非排序值并按照顺序检索它们(也更快速搜寻)。