-1

我正在处理一个链表示例。我正在查找和删除函数,它遍历列表以查找特定元素。find_remove 函数不起作用。应用程序中的所有其他函数(head_return、tail_return 等)都可以工作

如果有人能指出我在 find_remove 上哪里出了问题,我将不胜感激。

#include <iostream>
using namespace std;

struct node_ll
{
    int payload;
    node_ll* next;//Pointer to the next node
};

void head_insert(node_ll** list, int pload)
{
    node_ll* temp = new node_ll;//Create a new node, and let temp be the address of that node.
    temp->payload = pload;//Set the payload of the struct whose address is temp to pload.
    temp->next = *list;//Set the next of the struct whose address is temp to the address of the old head of the list.
    *list = temp;//The address of the old head of the list is changed to the address of the struct temp.
};

void tail_insert(node_ll** list, int pload)
{
    if (*list == NULL)
    {
        head_insert(list, pload);
    }
    else
    {
        node_ll* temp = new node_ll;
        for (temp = *list; temp->next; temp = temp->next);
        temp->next = new node_ll;
        temp->next->payload = pload;
        temp->next->next = NULL;
    }
}

int head_return (node_ll** list)
{
    if (*list != NULL)
    {
    int temp = (*list)->payload;
      node_ll* trash = *list;
      *list = (*list)->next;
      delete trash;
      return temp;
    }
    else
    {
        return 0;
    }
}

int tail_return (node_ll** list)
{
    if (*list != NULL)
    {
      if ((*list)->next == NULL)
        {
            return head_return(list);
        }
        else
        {
      node_ll* trash;
            for (trash = *list; trash->next->next; trash = trash->next);
            int temp = trash->next->payload;
            delete trash->next;
            trash->next = NULL;
            return temp;
        }
    }
    else
    {
        return 0;
    }
}

void find_remove (node_ll** list, int pload)
{
    if (*list != NULL)
    {
        node_ll* temp;//Declared before loop for use after loop.
        for (temp = *list; temp->next; temp = temp->next)
        {
            if (temp->payload == pload)
            {
                int trash = head_return(&temp);
            }
        }
        if (temp->payload == pload)
        {
            int trash = tail_return(list);
        }
    }
}

void print_ll (node_ll** list)
{
  node_ll* temp = *list;//Let temp be the address of the node that is the head of the list.
    while(temp)// != NULL
    {
        cout << temp->payload << endl;//Print out payload of the struct whose address is temp.
        temp = temp->next;//Set the address of temp equal to the address stored in next of the struct whose address is temp.
    }
}

int main()
{
    node_ll *blist = NULL;
    tail_insert(&blist, 2);
    tail_insert(&blist, 4);
    tail_insert(&blist, 6);
    find_remove(&blist, 4);
    print_ll(&blist);
    cout << '\n';

    system("PAUSE");
    return 0;
}

编辑

这是我重写代码的尝试。

void find_remove (node_ll** list, int pload)
{
    if (*list != NULL)
    {
        while (*list && (*list)->payload == pload)
        {
            head_return(list);
        }
        if (*list != NULL)
        {
            node_ll* temp;
            for (temp = *list; temp->next->next; temp = temp->next)
            {
                if (temp->next->payload == pload)
                {
                    node_ll* trash = temp->next;
                    temp->next = temp->next->next;
                    head_return(&trash);
                }
            }
        }
    }
}

编辑2: 谢谢!如果开头有多个匹配节点、中间有多个匹配节点或结尾有多个匹配节点,则此新解决方案有效。

void find_remove (node_ll** list, int pload)
{
    if (*list != NULL)
    {
        while (*list && (*list)->payload == pload)
        {
            head_return(list);
        }
        if (*list != NULL)
        {
            node_ll* temp;
            for (temp = *list; temp->next; temp = temp->next)
            {
                while (temp->next->next != NULL && temp->next->payload == pload)
                {
                    node_ll* trash = temp->next;
                    temp->next = temp->next->next;
                    head_return(&trash);
                }
            }
            if (temp->next == NULL && temp->payload == pload)
            {
                tail_return(list);
            }
        }
    }
}
4

1 回答 1

1

崩溃是由于您的循环逻辑不正确,而且您没有跟踪前一个节点,这意味着您没有正确重置链表,这是最简单的修复:

void find_remove (node_ll** list, int pload)
{
    if (*list != NULL)
    {
        node_ll* temp, *prev=NULL;//Declared before loop for use after loop.
        for (temp = *list; temp != NULL; temp = temp->next)
        {
            if (temp->payload == pload)
            {
               if( NULL != prev )
               {
                  prev->next = temp->next ;
               }
                int trash = head_return(&temp);
            }

            prev=temp ;
         }
   }
}

虽然老实说代码有很多样式问题,并且更像 C 样式代码而不是 C++ 样式代码。您可能需要考虑查看Code Review

更新:

如果您在修改后的代码中更改它,它会起作用:

for (temp = *list; temp->next != NULL; temp = temp->next)
                   ^^^^^^^^^^^^^^^^^^

在循环的顶部,您永远不知道是否temp->next为 NULL,因此这样做temp->next->next永远无效。

于 2013-04-12T01:35:36.637 回答