int StringList::remove(string value)
{
if ((head == NULL) || (value > tail->data) || (value < head->data))
{
return false;
}
if ((head == tail) && (head->data == value))
{
delete head;
head = tail = NULL;
return true;
}
if ((head->data) == value)
{
head = head->next;
delete head->previous;
head->previous = NULL;
return true;
}
if ((tail->data) == value)
{
tail = tail->previous;
delete tail->next;
tail->next = NULL;
return true;
}
Node *nodeToDelete = head;
while ((nodeToDelete->data) < value)
{
nodeToDelete = nodeToDelete->next;
}
if ((nodeToDelete->data) == value)
{
nodeToDelete->previous->next = nodeToDelete->next;
nodeToDelete->next->previous = nodeToDelete->previous;
delete nodeToDelete;
return true;
}
else
{
return false;
}
}
I have a doubly linked list here and im trying to remove all occurrences of a given string. Currently the function will only remove one occurrence, which is good but not quite working. I believe I need to somehow implement a counter variable that will count how many times the given string has been removed but im not quite sure how to go about it.
我在这里有一个双向链表,我试图删除所有出现的给定字符串。目前该功能只会删除一个事件,这很好但不太有效。我相信我需要以某种方式实现一个计数器变量,它将计算已删除给定字符串的次数,但我不太清楚如何去做。
full code: https://pastebin.com/3bRfJzJT
完整代码:https://pastebin.com/3bRfJzJT
2 个解决方案
#1
0
int StringList::remove(string value)
{
Node* curr = head;
while (curr != NULL)
{
if ((curr->data) == value)
{
if (curr == head)
{
head = curr->next;
if (head->prev != NULL)
head->prev = NULL;
else
tail = NULL;
delete curr;
curr = head;
}
else if (curr == tail)
{
tail = curr->prev;
tail->next = NULL;
delete curr;
curr = NULL;
}
else
{
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
Node* next = curr->next;
delete curr;
curr = next;
}
}
else
curr = curr->next;
}
}
#2
0
int StringList::remove(string value)
{
int numDeleted = 0;
Node *node = head;
while (node)
{
Node *nextNode = node->next;
if (node->data == value)
{
if (head == node)
head = head->next;
if (tail == node)
tail = tail->previous;
if (node->previous)
node->previous->next = node->next;
if (node->next)
node->next->previous = node->previous;
delete node;
++numDeleted;
}
node = nextNode;
}
return numDeleted;
}
That being said, you really should use std::list
instead of a manual list implementation. Let it handle the nodes for you, including removing values, eg:
话虽这么说,你真的应该使用std :: list而不是手动列表实现。让它为您处理节点,包括删除值,例如:
#include <list>
class StringList
{
private:
std::list<std::string> data;
public:
...
void remove(string value);
};
void StringList::remove(string value)
{
data.remove(value);
}
#1
0
int StringList::remove(string value)
{
Node* curr = head;
while (curr != NULL)
{
if ((curr->data) == value)
{
if (curr == head)
{
head = curr->next;
if (head->prev != NULL)
head->prev = NULL;
else
tail = NULL;
delete curr;
curr = head;
}
else if (curr == tail)
{
tail = curr->prev;
tail->next = NULL;
delete curr;
curr = NULL;
}
else
{
curr->prev->next = curr->next;
curr->next->prev = curr->prev;
Node* next = curr->next;
delete curr;
curr = next;
}
}
else
curr = curr->next;
}
}
#2
0
int StringList::remove(string value)
{
int numDeleted = 0;
Node *node = head;
while (node)
{
Node *nextNode = node->next;
if (node->data == value)
{
if (head == node)
head = head->next;
if (tail == node)
tail = tail->previous;
if (node->previous)
node->previous->next = node->next;
if (node->next)
node->next->previous = node->previous;
delete node;
++numDeleted;
}
node = nextNode;
}
return numDeleted;
}
That being said, you really should use std::list
instead of a manual list implementation. Let it handle the nodes for you, including removing values, eg:
话虽这么说,你真的应该使用std :: list而不是手动列表实现。让它为您处理节点,包括删除值,例如:
#include <list>
class StringList
{
private:
std::list<std::string> data;
public:
...
void remove(string value);
};
void StringList::remove(string value)
{
data.remove(value);
}