简体   繁体   English

删除循环链接列表中的节点c ++?

[英]Deleting a node in a circular linked list c++?

EDITED: using c++ to code. 编辑:使用c ++进行编码。

void circularList::deleteNode(int x)
{
    node *current;
    node *temp;
    current = this->start;

    while(current->next != this->start)
    {
        if(current->next->value == x)
        {
            temp = current->next;
            current->next = current->next->next;
            delete current->next;
        }
    else{ 
            current = current->next;
             }
    }
}

Added the else i'm sorry i kinda forgot to copy that part of the code and yes it is for learning purposes. 添加了其他,对不起,我有点忘记复制代码的那部分,是的,这是出于学习目的。 I'm new to coding with c++ and probably come off as a noob sorry about that. 我是C ++编码的新手,可能对此感到抱歉。

Also as for this line of code 同样关于这行代码

this->start->value == x this-> start-> value == x

im not sure what you mean by it or where you think it goes, yes there are nodes in the linked list and assume that it will always have at lease 1 node all the time. 我不确定您的意思是什么,或者您认为它去向何方,是的,链接列表中有节点,并假定它始终始终至少有1个节点。

Think about this two lines: 考虑以下两行:

current->next = current->next->next;

delete current->next;

Try to determine what you are actually deleting (no its not current->next; , at least not the one you want to delete). 尝试确定您实际上要删除的内容(没有当前不是- current->next; ,至少不是您要删除的内容)。

You never move to the next node in your while loop. 您永远不会移至while循环中的下一个节点。 After your if, you should have: 在if之后,您应该具有:

else
    current = current->next;

Also, you might want to consider returning from the function after you've found the node (unless you suspect that two nodes have the same value). 另外,您可能要在找到节点之后考虑从函数返回(除非您怀疑两个节点具有相同的值)。

In addition to Justin's and Let_Me-Be's answers, consider what you might need to take care of when 除了贾斯汀(Justin)Let_Me-Be的答案外,请考虑一下您可能需要注意的事项

this->start->value == x

If you don't handle that right, you'll lose your whole list (or crash trying to get to it)... 如果您处理不正确,则会丢失整个列表(或崩溃而无法访问)...

Do you have only a singularly linked list? 您只有一个单链表吗?

Have you considered the STL, perhaps a deque ? 您是否考虑过STL,也许是deque

Or, if you must have a single linked list then why not take something that it 90% STL (;-) and look at the Boost libraries? 或者,如果您必须具有单个链接列表,那么为什么不采用90%STL(;-)的名称并查看Boost库呢?

You do seem to be reinventing the wheel here. 您似乎确实是在重新发明轮子。 Why not take some existing - and tested - code and use that? 为什么不采用一些现有的并经过测试的代码并使用呢?

Is "deleting a node from linked list" the issue or "deleting a node from a circular linked list" the issue? 是“从链表中删除节点”还是“从循环链表中删除节点”问题?

The difference from non-circular to circular is the line: 从非圆形到圆形的区别在于:

while(current->next != this->start)

If the list were non circular it would have been 如果清单是非圆形的,那本来是

while(current->next != NULL )

Apart from that, there are issues with the code snippet, which others have already pointed out. 除此之外,还有其他人已经指出的代码片段问题。

Another thing: Do you check that you have at least one valid node in your list before you call deleteNode? 另一件事:在调用deleteNode之前,是否检查列表中是否至少有一个有效节点?

I ask this as your function does not check for a NULL-value of the start element. 我问这是因为您的函数不会检查start元素的NULL值。

The solution would be to add the line 解决方案是添加行

if(start == NULL) return if(start == NULL)返回

as first line of your method to avoid this. 作为避免这种情况的方法的第一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM