【问题标题】:C Doubly linked list variable not being properly updatedC 双向链表变量未正确更新
【发布时间】:2012-05-24 21:07:12
【问题描述】:

我在使用双向链表时遇到问题,所以我有两个问题。

首先是描述。

我用这种方式制作了一个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 int numberOfTeams; 将拥有我列表中的团队数量。 tail 将包含我列表的最后一个元素,int numberOfTeams;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("\nError\n");
        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;
}

在我的列表中添加(按团队名称排序)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 numberOfTeams;

所以,在阅读完所有这些文字之后,这些是我的问题:

问题 1如何解决我的团队 ID,即在我的列表中插入新的 Team 后,ID 会更新为正确的 ID。

问题 2 虽然我的 addNodeTeamsSorted 正在使用 ID 例外,但我相信它的算法是“断断续续”的。你能推荐一些优化吗?

谢谢

【问题讨论】:

    标签: c doubly-linked-list


    【解决方案1】:

    如何解决我的团队 ID 名称,即在我的列表中插入一个新团队后,ID 会更新为正确的 ID。

    这是一次非法的内存写入:

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

    teamPlacename 被统一化为char*,所以strcpy() 将写入它不应该写入的地方。您需要为这些分配内存或将它们定义为固定大小的数组(如果可能)。

    【讨论】:

    • @Favolas,我不确定ID 是什么?
    • 我的列表中的int numberOfTeams; head 将包含我列表中的元素数量,每个节点上将是团队的 ID 号。第一队应该是1,第二队应该是2,依此类推。看我的输出。我的 ID 列上的 ID 号不正确。它们应该是 1 到 16,没有重复的数字。所以在我的addNodeTeamsSorted int numberOfTeams; 它没有被正确分配
    • 从文件中读取时,每个团队都应该分配自己的ID,对吗?如果是这样,请将int ID 成员添加到Team 并为其分配numberOfTeams 的当前值。
    • 正确。但是有两件事。首先:假设加入了 B 队,然后加入了 A 队。B id 为 1,A id 为 2。这是不正确的。 B 为 1,但当添加团队 A 时,由于 A 先出现,因此 A 应为 ID 1,B 应更新为 2。其次。由于我的列表作为int 字段,它仅用于我的head(我列表中的团队总数)我如何使用这个空间(内存已经“用完”)成为 ID 团队
    • 好的。然后我看不出有理由将它存储在任何地方。在printListOfTeams() 中,只需使用一个局部变量来记录代表ID 的打印团队数量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多