English 中文(简体)
没有适当更新 Coubly 链接的 Cdobly 列表变量
原标题:C Doubly linked list variable not being properly updated

我对双重关联名单有问题 所以我有两个问题

首先,描述。

我以这种方式做了一个 struct :

typedef struct team{
    char *name;
    char *teamPlace;
}Team;

并以这种方式创造了我的列表:

typedef struct nodeTeam{
    int numberOfTeams;
    Team team;
    struct nodeTeam *next;
    struct nodeTeam *prev;
}NodeTeam;

因此,我的列表将包含一个 head tail 。 当我在列表中添加一些 team 时, 我的 head head ; 将包含我列表中的团队数量。 tail 将包含我列表中的最后一个元素和 intnumberofteams; head 之后将包含 team ID。

我的列表将以此方式创建 :

int createsListOfTeams(NodeTeam **head, NodeTeam **tail);
int addNodeTeamsSorted(NodeTeam *head, NodeTeam **tail, Team team);
void printListOfTeams(NodeTeam *listofTeams);

int main()
{
    NodeTeam *headTeams,*tailTeams;
    Team eq;
    /*Creates the doubly linked list*/
    if(createsListOfTeams(&headTeams,&tailTeams)){ /*See below this part of the code*/
        printf("
Error
");
        return 0;
    }

    /*Teams are on a .txt file. The code for reading from a file is missing. It´s working ok so I believe it s not needed.
    After reading one line after another it will do this

    addNodeEquipasSorted(headTeams,tailTeams,eq);

    where eq is a `struct` with the team data.
    */

    /*Will print all the teams*/
    printListOfTeams(headTeams);

    return 0;
}

这是创建列表的代码 :

/*Add the teams to the doubly linked list. At the end, all teams will be sorted by name*/
int createsListOfTeams(NodeTeam **head, NodeTeam **tail){
    (*head) = (NodeTeam *)malloc(sizeof(NodeTeam));

    if ((*head) == NULL){
        return -1;
    }
    (*head)->numberOfTeams = 0;
    strcpy((*head)->team.teamPlace,"");
    strcpy((*head)->team.name,"");
    (*head)->next = NULL;
    (*head)->prev = NULL;

    *tail = *head;
    return 0;
}

在我的列表中添加 < code> Team 的代码( 以团队名称排序) 是:

/*Creates the doubly linked list*/
int addNodeTeamsSorted(NodeTeam  *head, NodeTeam  **tail, Team team){
    NodeTeam  *no, *aux;

    /*Memory alloc for a new node*/
    no = (NodeTeam*) malloc(sizeof(NodeTeam));
    if (no == NULL){
        return -1;
    }

    /*Updates the number of element of the list*/
    head->numberOfTeams++;

    /*Creates a copy of tail*/
    aux = (*tail);

    /*Puts team data on node*/
    no->team = team;

    /*to see if the list it s empty(no it s the first element of my list) or the last node as a name "smaler" then node*/
    if(head == *tail || strcmp((*tail)->team.name,no->team.name) <= 0)
    {
        if (head == *tail){
            no->numberOfTeams = 1;
        }
        else{
            no->numberOfTeams = head->numberOfTeams;
            (*tail)->numberOfTeams = no->numberOfTeams - 1;
        }
        no->next = (*tail)->next;
        no->prev = *tail;
        (no->prev)->next = no;
        (*tail) = no;
        aux = (*tail);
    }
    else{ /*If not the first element*/
        head = head->next; /*To advance to the first item after my head*/
        while(strcmp(head->team.name,no->team.name) < 0 && strcmp((*tail)->team.name,no->team.name) > 0 && head != *(tail)){
            head = head->next;
            (*tail) = (*tail)->prev;
        }
        if(strcmp(head->team.name,no->team.name) >= 0){
            no->next = head;
            no->prev = head->prev;
            head->prev = no;
            (no->prev)->next = no;
            no->numberOfTeams = (no->next)->numberOfTeams;
            (no->next)->numberOfTeams = no->numberOfTeams + 1;
            if((no->prev)->prev != NULL){
                (no->prev)->numberOfTeams = no->numberOfTeams - 1;
            }
        }
        else{
            no->next = (*tail)->next;
            no->prev = (*tail);
            no->numberOfTeams = (no->prev)->numberOfTeams + 1;
            (no->prev)->next = no;
            (no->next)->prev = no;
        }
    }

    /*Puts `tail` pointing to the right position*/
    if (aux != (*tail)){
        (*tail) = aux;
    }

    return 0;
}

在我的. txt 文件上 我有这个数据:

E team;E team place
J team;J team place
G team;G team place
F team;F team place
L team;L team place
A team;A team place
H team;H team place
O team;O team place
K team;K team place
P team;P team place
N team;N team place
B team;B team place
C team;C team place
M team;M team place
D team;D team place
I team;I team place

这就是产出。

---------------------------------------------------------
|                     List of Teams                     |
---------------------------------------------------------
|        Number of Teams       |                     16 | no 00740ff0 | prev 00000000 | next 00741240 |
--------------------------------------------------------
|  ID  |       Team Name       |        Team Place      |
--------------------------------------------------------
|    1 | A team                | A team place           | no 00741240 | prev 00740ff0 | next 00741450 |
|    2 | B team                | B team place           | no 00741450 | prev 00741240 | next 007436b0 |
|    3 | C team                | C team place           | no 007436b0 | prev 00741450 | next 00743760 |
|    4 | D team                | D team place           | no 00743760 | prev 007436b0 | next 00741088 |
|    5 | E team                | E team place           | no 00741088 | prev 00743760 | next 00741190 |
|    2 | F team                | F team place           | no 00741190 | prev 00741088 | next 00741138 |
|    3 | G team                | G team place           | no 00741138 | prev 00741190 | next 00741298 |
|    4 | H team                | H team place           | no 00741298 | prev 00741138 | next 007437b8 |
|    5 | I team                | I team place           | no 007437b8 | prev 00741298 | next 007410e0 |
|    4 | J team                | J team place           | no 007410e0 | prev 007437b8 | next 00741348 |
|    5 | K team                | K team place           | no 00741348 | prev 007410e0 | next 007411e8 |
|    7 | L team                | L team place           | no 007411e8 | prev 00741348 | next 00743708 |
|    8 | M team                | M team place           | no 00743708 | prev 007411e8 | next 007413f8 |
|    8 | N team                | N team place           | no 007413f8 | prev 00743708 | next 007412f0 |
|    9 | O team                | O team place           | no 007412f0 | prev 007413f8 | next 007413a0 |
|   10 | P team                | P team place           | no 007413a0 | prev 007412f0 | next 00000000 |
--------------------------------------------------------

通过此输出,我可以看到我的团队正在被添加到列表中按名称排序。 我打印存储地址的调试显示一切正常。 问题在于团队 ID 。 它的编号为 int numberfeeteams;

因此,最后,在所有这些案文之后,这是我的问题:

问号 1 < programé question 1 问题1 :在将新的 < code> Team 加入我的名单后,我还能做些什么来解答我的团队 ID??? ID会被更新到正确的 ID 。

问题2 < problease 2 Altough my addNode TeamsSorted 正在对 ID 输出工作, 我相信代号是 " chopy" 。 您可以推荐一些优化吗?

谢谢 谢谢

最佳回答

我能做些什么来解答我的小组的身份识别名, 即,在将新小组插入我的名单之后, 身份将更新到正确的身份识别号上。

这是非法的记忆书写:

(*head) = (NodeTeam *)malloc(sizeof(NodeTeam));

if ((*head) == NULL){
    return -1;
}
(*head)->numberOfTeams = 0;
strcpy((*head)->team.teamPlace,"");   <<------- here, and
strcpy((*head)->team.name,"");        <<------- here

teamPlace name 被合并为 char%/code >, 因此 strcpy () 将写入不应写入的文字。 您需要为这些内容分配内存或将其定义为固定大小的阵列( 如果可能的话) 。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...