【问题标题】:Adding node in Linked List in a specific position in C在 C 中的特定位置添加链表中的节点
【发布时间】:2013-07-19 23:51:38
【问题描述】:

我正在尝试将节点添加到链接列表。这个想法是传递指针,看看新节点将通过排序顺序到达哪里,在这种情况下是 G,然后是 D,​​然后是 M,然后是 S。

然而,当我编译和运行时,我实际上并没有生成一个链表(这已经在 main 中完成了)。我非常确定我的 addp() 函数有问题。是不是我应该传入双指针? 对不起,有点不专业和无知。我不是最强的程序员。

任何帮助都会有所帮助。

我附上了我已经经历了很多次的方法。

typedef struct node {
char fname[1024];
char lname[1024];
char pos;
int val;
int rank;
struct node * next;
} player;


    struct node* addp (player* newnode, struct node* list){
    player* templist = list;
        player* templist1;
    // if the list is non empty.
    if (list!=NULL){
        if(newnode->pos == GOALKEEPER){  //insert if G.
            newnode->next = list;
        }
        if(newnode->pos == DEFENDER){// after G bef M.
            // iterate through templist.
            while (templist->next != NULL && (templist->next)->rank < 1) {  // go to end of G.
                // when the list isn't empty next node rank is less than one, keep going
                templist = templist -> next;
            }
            // when finally rank == or > 1, then add newnode.
            templist1 = templist->next;
            templist->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == MIDFIELDER){ //after G and M but before S
            while (templist->next != NULL && (templist->next)->rank <2 && (templist->next)->rank> 2){
                templist = templist -> next;
            }
            // when stopped, then add newnode.
            templist1 = templist->next;
            templist->next = newnode;
            newnode->next = templist1;
        }
        if(newnode->pos == STRIKER){ // at the end.
            while (templist->next != NULL && (templist->next)->rank <3){
                templist = templist -> next;
            }
            templist1 = templist->next;
            templist->next = newnode;
            newnode->next = templist1;
        }
        return list;
        printf("player added");
    }
    // if list is empty
    else{
        newnode->next = list;
        return 0;
    }
}

以下是我想出的列表功能。它一直说我的链接列表是空的。可能是这个函数有问题。

int print(struct player* list){
    // create temp list so non modify origin.
    struct player* temp = list;
    if (list == NULL && temp == NULL)
        printf("linked list is empty");
    while (temp != NULL){
        printf("%s \n", temp->lname);
        printf("%s \n", temp->fname);
        printf("%c \n", temp->pos);
        printf("d \n", temp->val);
        temp = temp->next;
    }
return 0;
}

【问题讨论】:

  • 编辑:我已经包含了 typedef。
  • 编辑:我还包括了显示功能,它列出了各个节点中的数据。

标签: c linked-list trace


【解决方案1】:

在不知道播放器类型定义的情况下,这很难分析,但我认为您可以通过将函数的签名简化为仅使用播放器来使其更容易。

void addp(player* newnode, player* firstnode)

返回是不必要的,因为您只是返回调用者已经拥有的第二个参数。第二个参数应该是指向播放器节点的指针,它是链表中的第一个元素。如果您可以在编译器不抱怨隐式转换指针的情况下调用该函数,那么我认为您的算法没有任何问题,尽管它当然可以简化。

【讨论】:

  • 我明白了。因此,取消返回类型绝对是明智的。行。我被告知传入player** newnodeplayer** firstnode 而不是我当前的单指针。这也会大大简化我的算法吗?顺便感谢您的帮助。
  • 我认为您不想传递双指针。这对你没有帮助。主要是确保您传入列表中第一个玩家的地址。因此,当您调用该函数时,如果“newnode”和“firstnode”是指针,则像这样传递它们:addp(newnode, firstnode),但如果它们是本地变量,则像这样传递它们:addp(&amp;newnode, &amp;firstnode)。同样,如果你把它搞砸了,编译器应该会抱怨。
  • 好吧,一切似乎都运行良好。只是当我运行我的链表列表时,它显示链表中没有任何内容。我会发布它,希望有人可能会在列表函数中发现错误。
  • 哦,你是完全用这个函数填充你的链表吗?如果是这样,那就解释了问题。此函数无法更新列表的开头。我建议只是将调用函数更改为手动执行此操作,因为它更容易,但如果您想使用 addp 执行此操作,那么您 应该 实际上为列表传递一个双指针。然后在将其分配给 templist 并将函数的结尾更改为 else { list = newnode } 时取消引用它
  • 我已经按照这些步骤传递了一个用于列表的双指针。现在编译器会抛出 error: request for member 'lname' in something not a structure or union 和相关错误。很抱歉让您如此困惑,并感谢您的帮助,但我应该如何调用这些来编辑它们?
【解决方案2】:

好吧,我的理解是你的播放器结构包含一个变量 pos,它将指示在列表中的哪个位置插入播放器。我对吗 ?

在这种情况下,您可以做的最好的事情是按排名变量对列表进行排序。然后修改您的 pos 变量(在玩家结构中)以匹配您列表的排名变量。

然后您只需使用经典的“添加到排序列表”功能添加它:C++ Add to linked list in sorted orderenter link description here

【讨论】:

  • 这就是我试图在我的代码中做的事情,是的。问题是,在我运行完该方法后,它实际上并没有在一天结束时修改列表。
  • 好的,所以您只需按地址越过列表的第一个节点。正如你在其他评论中所说。但是您不必通过要按地址添加的节点。所以函数应该是这样的: void addp (player* newnode, player** list){...}
猜你喜欢
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
相关资源
最近更新 更多