使用head_return和tail_return查找链接列表并从中删除

时间:2013-04-12 01:16:40

标签: c++ linked-list

我正在研究一个链表示例。我在查找和删除函数,遍历列表以查找特定元素。 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);
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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永远无效。