链接结构列表 - 如何编辑数据

时间:2018-01-25 07:43:56

标签: c linked-list

这是我的结构和我添加的节点。我的插入功能没问题,但是当我尝试稍后添加Course_Student数据时,通过搜索学生的uId并将数据添加到该特定节点;我的列表被删除,它什么都没显示。然后当我添加另一个节点时,Course_Student数据以奇怪的方式显示。 如何在以后将My Course_Student数据添加到一个特定节点?

typedef struct stdnt Student ;
typedef struct crs_std Course_Student;

struct crs_std{  
    int crs_code;
    char name[20];
    float grade;
    int unt;
};

struct stdnt { 
       char firstname[20];
       char lastname[20];
       Course_Student a[10];
       unsigned long long uID;
       int year;
       float avg;
};

struct Linked_list_Student{

    Student s;
    Node_s *next_s ;

这是我在插入数据后用于将数据添加到链接列表的功能。

void Select_Course(NodePtr_s *startPtr_s, Course_Student cs[10], int id){
    int i;
    NodePtr_s prevPtr_s;
    NodePtr_s currPtr_s;
    for(i=0;i<n;i++){

        if((*startPtr_s)->s.uID == id){

            (*startPtr_s)->s.a[i].crs_code = cs[i].crs_code;
            strcpy((*startPtr_s)->s.a[i].name,cs[i].name);
            (*startPtr_s)->s.a[i].unt = cs[i].unt;

        }

        prevPtr_s = (*startPtr_s);
        currPtr_s = (*startPtr_s)->next_s;

        while(currPtr_s != NULL){

            if(currPtr_s->s.uID == id){

                currPtr_s->s.a[i].crs_code = cs[i].crs_code;
                strcpy(currPtr_s->s.a[i].name,cs[i].name);
                currPtr_s->s.a[i].unt = cs[i].unt;

        }

        prevPtr_s = currPtr_s;
        currPtr_s = currPtr_s->next_s;
        }


    }

}

2 个答案:

答案 0 :(得分:1)

在你的for循环中:

for(i=0;i<n;i++){

......什么是n?看起来n可能是一个全局设置为10,用于循环通过cs?如果是这样,那么你的方法并不是最好的。看来你是:

foreach element in cs
    find the correct student record
    add that element from cs to the student record

但听起来cs中的所有元素都应该添加到相同的学生记录中?如果是这样,你应该这样做:

find the correct student record
foreach element in cs
    add that element to the student record

请确认您正在尝试实现的目标,然后我或此处的其他人可以帮助您使用正确的算法。

此外,请确保您的缩进正确,以便人们可以轻松阅读您的代码:

    while(currPtr_s != NULL){

        if(currPtr_s->s.uID == id){

            currPtr_s->s.a[i].crs_code = cs[i].crs_code;
            strcpy(currPtr_s->s.a[i].name,cs[i].name);
            currPtr_s->s.a[i].unt = cs[i].unt;

    }

    prevPtr_s = currPtr_s;
    currPtr_s = currPtr_s->next_s;
    }

应该是

    while(currPtr_s != NULL){

        if(currPtr_s->s.uID == id){

            currPtr_s->s.a[i].crs_code = cs[i].crs_code;
            strcpy(currPtr_s->s.a[i].name,cs[i].name);
            currPtr_s->s.a[i].unt = cs[i].unt;

        }

        prevPtr_s = currPtr_s;
        currPtr_s = currPtr_s->next_s;
    }

答案 1 :(得分:0)

您应该使用startPtr_s初始化循环并使用Node_s作为临时变量:

void Select_Course(NodePtr_s *startPtr_s, Course_Student cs[10], int id)
{
  Node_s *current = NULL;
  int i;

  /* iterate on each node in your list */
  for (current = *startPtr_s; current != NULL; current = current->next_s) {
    if (current->s.uID != id) continue;

    /* On each node update your data */
    for (i = 0; i < n; i++) {
      current->s.a[i].crs_code = cs[i].crs_code;
      strcpy(current->s.a[i].name,cs[i].name);
      current->s.a[i].unt = cs[i].unt;
    }
  }
}