C++ 删除链接列表中的节点函数
我有一个程序,可以询问用户希望删除哪个项目。然后,delete函数应成功删除该节点。我知道我需要从上一个节点获取地址,并使其指向我要删除的节点之后的节点,然后删除悬挂节点。我在理解语法方面遇到了一些困难。这是我的密码。我省略了不必要的功能C++ 删除链接列表中的节点函数,c++,linked-list,nodes,C++,Linked List,Nodes,我有一个程序,可以询问用户希望删除哪个项目。然后,delete函数应成功删除该节点。我知道我需要从上一个节点获取地址,并使其指向我要删除的节点之后的节点,然后删除悬挂节点。我在理解语法方面遇到了一些困难。这是我的密码。我省略了不必要的功能 #include <iostream> #include <cstddef> #include <string> using namespace std; struct Node { string item
#include <iostream>
#include <cstddef>
#include <string>
using namespace std;
struct Node
{
string item;
int count;
Node *link;
};
typedef Node* NodePtr;
void insert(NodePtr after_me, string an_item, int a_number);
void list_remove(NodePtr& head, string an_item);
void head_insert(NodePtr& head, string an_item, int a_number);
void show_list(NodePtr& head);
NodePtr search(NodePtr head, string target);
因此,由于您没有真正展示您将要尝试的内容,我无法真正说出哪个“语法”让您感到特别困惑。只是一个猜测 如果我理解正确,这归结为您想知道如何找到放置在您要移除的项目之前的项目,以便在必要时对其进行操作,对吗?
然后自己迭代列表,而不是使用
search()
方法:
void list_remove(NodePtr& head, string remove_item)
{
NodePtr remove_ptr; //pointer to the node that is being removed
remove_ptr = search(head, remove_item);
if(remove_ptr != NULL) // indicates the item is part of the list and
// we can remove it
{
NodePtr predecessorNode = NULL;
// Iterate the list to find the predecessor of remove_ptr
for(NodePtr curNode = head; curNode != NULL; curNode = curNode->link)
{
if(curNode->link == remove_ptr) // Indicates that curNode is the
// predecessor of remove_ptr
{
predecessorNode = curNode; // store it and ...
break; // ... exit the loop
}
}
if(predecessorNode != NULL) // We have found a predecessor
{
// Now you have the predecessor for remove_ptr,
// manipulate it as necessary
}
else // indicates the item to remove is the head of the list
{
// You might need to manipulate head, depending on how your
// linked list is organized (set to remove_ptr->link most likely)
}
}
}
要删除,还需要先删除节点,然后再删除节点。 这些步骤是:
// find the preceding_ptr
NodePtr preceding_ptr = head;
while(preceding_ptr != NULL && preceding_ptr->link != remove_ptr){
preceding_ptr = preceding_ptr->link;
}
// now preceding_ptr is set, make it skip remove_ptr
preceding_ptr->link = remove_ptr->link;
// free up memory from remove_ptr
不清楚您在问什么。我不确定应该向删除功能添加什么才能使其正常工作。语法令人困惑。请澄清哪些“语法”特别让您困惑,而不是将所有代码都扔给我们!顺便说一句:你有没有办法让它成为一个双链接列表,这样可以更容易地插入和删除?另外一点:改进代码格式!我只编辑了一些基础知识…仅供参考,这是您的列表删除方法的主体。
// Uses cstddef:
void head_insert(NodePtr& head, string an_item, int a_number)
{
NodePtr temp_ptr;
temp_ptr = new Node;
temp_ptr -> item = an_item;
temp_ptr -> count = a_number;
temp_ptr->link = head;
head = temp_ptr;
}
//Uses iostream and cstddef:
void show_list(NodePtr& head)
{
NodePtr here = head;
while (here != NULL)
{
cout << here-> item << "\t";
cout << here-> count << endl;
here = here->link;
}
}
NodePtr search(NodePtr head, string target)
{
// Point to the head node
NodePtr here = head;
// If the list is empty nothing to search
if (here == NULL)
{
return NULL;
}
// Search for the item
else
{
// while you have still items and you haven't found the target yet
while (here-> item != target && here->link != NULL)
here = here->link;
// Found the target, return the pointer at that location
if (here-> item == target)
return here;
// Search unsuccessful, return Null
else
return NULL;
}
}
void list_remove(NodePtr& head, string remove_item)
{
NodePtr remove_ptr; //pointer to the node that is being removed
remove_ptr = search(head, remove_item);
if(remove_ptr != NULL) // indicates the item is part of the list and
// we can remove it
{
NodePtr predecessorNode = NULL;
// Iterate the list to find the predecessor of remove_ptr
for(NodePtr curNode = head; curNode != NULL; curNode = curNode->link)
{
if(curNode->link == remove_ptr) // Indicates that curNode is the
// predecessor of remove_ptr
{
predecessorNode = curNode; // store it and ...
break; // ... exit the loop
}
}
if(predecessorNode != NULL) // We have found a predecessor
{
// Now you have the predecessor for remove_ptr,
// manipulate it as necessary
}
else // indicates the item to remove is the head of the list
{
// You might need to manipulate head, depending on how your
// linked list is organized (set to remove_ptr->link most likely)
}
}
}
// find the preceding_ptr
NodePtr preceding_ptr = head;
while(preceding_ptr != NULL && preceding_ptr->link != remove_ptr){
preceding_ptr = preceding_ptr->link;
}
// now preceding_ptr is set, make it skip remove_ptr
preceding_ptr->link = remove_ptr->link;
// free up memory from remove_ptr