我有下一个链表:
#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
//Node class
class Node
{
public:
string name;
string age;
Node *next;
Node *prev;
Node(string name, string age)
{
this->name = name;
this->age = age;
this->next = NULL;
this->prev = NULL;
}
};
//Doubly circular Linked list class
class List
{
private:
Node *first;
Node *last;
public:
List()
{
this->first = NULL;
this->last = NULL;
}
void deleteNode(string name)
{
Node *start = this->first;
if (start == NULL)
{
return;
}
Node *curr = start, *prev_1 = NULL;
while (curr->name != name)
{
if (curr->next == start)
{
printf("nList doesn't have node with value = %d", name);
return;
}
prev_1 = curr;
curr = curr->next;
}
if (curr->next == start && prev_1 == NULL)
{
(start) = NULL;
free(curr);
return;
}
if (curr == start)
{
prev_1 = (start)->prev;
start = (start)->next;
prev_1->next = start;
(start)->prev = prev_1;
free(curr);
}
else if (curr->next == start)
{
prev_1->next = start;
(start)->prev = prev_1;
free(curr);
}
else
{
Node *temp = curr->next;
prev_1->next = temp;
temp->prev = prev_1;
free(curr);
}
}
void add(string name, string age)
{
Node *node = new Node(name, age);
if (this->first == NULL)
{
this->first = node;
this->first->next = node;
this->first->prev = node;
this->last = node;
}
else
{
this->last->next = node;
node->next = this->first;
node->prev = this->last;
this->last = node;
this->first->prev = this->last;
}
}
void print()
{
Node *aux;
aux = this->first;
while (aux != NULL)
{
cout << aux->name << ", " << aux->age<<endl;
aux = aux->next;
if (aux == this->first)
{
break;
}
}
}
};
int main(int argc, char const *argv[])
{
List list;
list.add("David", "25");
list.add("James", "45");
list.add("Charles", "78");
list.add("Katty", "96");
cout<<"List without delete any node"<<endl;
list.print();
//trying to delete a certain node
list.deleteNode("David");
cout<<"printing list without David because I deleted him above"<<endl;
list.print();
return 0;
}
在我的函数deleteNode
中,我试图发送一个名称将其从链表中删除,但当运行我的代码时,它不起作用,它只显示下一个:
List without delete any node
David, 25
James, 45
Charles, 78
Katty, 96
我的预期输出是:
List without delete any node
David, 25
James, 45
Charles, 78
Katty, 96
printing list without David because I deleted him above
James, 45
Charles, 78
Katty, 96
我不知道还能做什么,我希望你能帮助我,谢谢。
对于初学者来说,free
是释放用new
分配的指针的错误方法。请改用delete
。此外,通常最好将字符串作为常量引用传递。
deleteNode函数看起来非常复杂。让我为你简化一下。告诉我你对此的看法:
void deleteNode(const string& name)
{
Node *start = this->first;
while ((start != nullptr) && (start->name != name))
{
start = start->next;
}
if (start == nullptr)
{
return;
}
Node* prev = start->prev;
Node* next = start->next;
if (prev != nullptr)
{
prev->next = next;
}
else
{
first = next;
}
if (next != nullptr)
{
next->prev = prev;
}
else
{
last = prev;
}
delete start;
}
同样,add
可以简化为:
void add(const string& name, const string& age)
{
Node *node = new Node(name, age);
if (this->last == NULL) // first insert
{
first = node;
last = node;
}
else
{
last->next = node;
node->prev = last;
last = node;
}
}