除去具有一定值的所有节点中的链接列表 [英] Removing all nodes with a certain value in a linked list

查看:107
本文介绍了除去具有一定值的所有节点中的链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题是pretty言自明。下面是我为这个目的编写的功能:

 无效wipeLoneCells()
{
    细胞* tmp目录;    尾=头;
    而(1)
    {
        如果(头==尾放大器;&安培;!tail->旗)
        {
            头=流浆>接下来,
            免费(尾);
            尾=头;
            继续;
        }        TMP = tail->接下来,/ *** / IF(tmp->接下来== NULL和放大器;&安培;!tmp->旗)
        {
            tail->接着= NULL;
            免费(TMP);
            打破;
        }
        否则,如果(tmp->!标记)
        {
            tail->接下来= tmp->接下来,
            免费(TMP);
            继续;
        }        尾= tail->接下来,
    }
}

该列表的头和尾是全球性的,而列表是通过这个函数被调用头指向第一个节点和尾指向最后一个(其接下来就是NULL)的时间建成。我几乎可以肯定,我的链表是正确建立,我可以没有任何错误打印。有时,这种功能完美的作品,有时它在标有星线导致访问冲突。我知道这是不是因为我得到我想要的时候它不会产生错误的结果完全错误的,但我得到了错误频繁所以,必须有我忽视的东西。预先感谢您的任何帮助。

编辑:这里是固定的code:

 无效wipeLoneCells()
{
    细胞* tmp目录;    尾=头;
    而(1)
    {
        如果(头==尾放大器;&安培;!tail->旗)
        {
            头=流浆>接下来,
            免费(尾);
            尾=头;
            继续;
        }        TMP = tail->接下来,        如果(tmp->接下来== NULL和放大器;&安培;!tmp->旗)
        {
            tail->接着= NULL;
            免费(TMP);
            打破;
        }
        否则如果(tmp->接着== NULL)
        {
            尾= tmp目录;
            打破;
        }
        否则,如果(tmp->!标记)
        {
            tail->接下来= tmp->接下来,
            免费(TMP);
            继续;
        }        尾= tail->接下来,
    }
}


解决方案

如果

  TMP = tail->接下来,

NULL ?下一行试图取消引用 NULL 指针,从而导致的未定义行为 - 可能导致崩溃

您应该检查这种情况,并采取适当的行动。

The title is pretty self explanatory. Here's the function I've written for this purpose:

void wipeLoneCells()
{
    cell *tmp;

    tail = head;
    while (1)
    {
        if (head == tail && !tail->flag)
        {
            head = head->next;
            free(tail);
            tail = head;
            continue;
        }

        tmp = tail->next;

/***/   if (tmp->next == NULL && !tmp->flag)
        {
            tail->next = NULL;
            free(tmp);
            break;
        }
        else if (!tmp->flag)
        {
            tail->next = tmp->next;
            free(tmp);
            continue;
        }

        tail = tail->next;      
    }
}

The list's head and tail are global, and the list is built by the time this function gets called with head pointing to the first node and tail pointing to the last (whose next is NULL). I'm almost certain that my linked list is built correctly as I can print them with no errors. Sometimes this function works perfectly and sometimes it results in an access violation at the line marked with stars. I know it's not completely wrong as I do get the result I want when it doesn't produce an error, although I do get the error frequently so there must be something I'm overlooking. Thank you in advance for any help.

EDIT: Here's the fixed code:

void wipeLoneCells()
{
    cell *tmp;

    tail = head;
    while (1)
    {
        if (head == tail && !tail->flag)
        {
            head = head->next;
            free(tail);
            tail = head;
            continue;
        }

        tmp = tail->next;

        if (tmp->next == NULL && !tmp->flag)
        {
            tail->next = NULL;
            free(tmp);
            break;
        }
        else if (tmp->next == NULL)
        {
            tail = tmp;
            break;
        }
        else if (!tmp->flag)
        {
            tail->next = tmp->next;
            free(tmp);
            continue;
        }

        tail = tail->next;      
    }
}

解决方案

What if

tmp = tail->next; 

is NULL? The next line attempts to dereference a NULL pointer, which results in undefined behavior - possibly leading to a crash.

You should check for this condition and take appropriate action.

这篇关于除去具有一定值的所有节点中的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆