【发布时间】:2020-10-19 02:57:41
【问题描述】:
对于我的数据结构类中的一个项目,我们被指示制作一个创建链接列表的程序。
这里是赋值指令:你的任务是定义和实现一个 List ADT 作为内存中的有序双链表。您的数据将来自名为 ThingsToDo.txt 的输入文本文件。 List 类将连同构造函数和析构函数一起定义列表的核心功能:
- 将项目(字符串)插入升序列表中
- 从升序列表中删除一个项目(字符串)
- 在升序列表中编辑项目(字符串)
- 打印有序列表
我已经创建了以下头文件:
using namespace std;
//create a node with data and link fields
struct Node
{
string info;
Node * next;
Node * prev;
};
//the List ADT definition
class List
{
public:
// construct a List object
List();
// Destroy a list object
~List();
// Insert a string into its correct alpha location in the list and return true if the function worked and false otherwise
bool Insert(string);
// Delete a string from its location in the list and return true if the function worked and false otherwise (no string found)
bool Delete(string);
// Edit a string from its location in the list and return true if the edit worked and false otherwise.
bool Edit(string, string);
// Print all of the nodes info from the beginning to end.
void Print() const;
private:
Node * Head;
};
我的问题是我不知道如何在我的 c++ 文件中创建一个好的删除函数。
我已经制作了以下插入和编辑功能,但如果有人可以帮助我创建一个好的删除功能,将不胜感激。
bool List::Edit(string target, string replace)
{
bool Success = false;
// first delete the target node calling the Delete function
if (Delete(target))
{
// if deleted, then insert the replace node
if (Insert(replace))
{
Success = true;
}
}
// will only return true if Delete and add both worked , otherwise false
return Success;
}
/*Function description: insert data into the doubly linked list by placing
* it into its correct ascending alpha location
*/
bool List::Insert(string data)
{
bool wasSuccessful = false;
// Allocate memory for a node and store the address in p. If new returns
// false, then memory could not be allocated and the function will exit
// with failure.
Node* p = new Node;
if (p != NULL)
{
//store the KEY data in the node
p->info = data;
// Logic that handles the insert empty case (CASE 1)
if (Head == NULL)
{
p->next = NULL;
p->prev = NULL;
Head = p;
wasSuccessful = true;
}
else
{
//Logic if the List is not empty, cur is the traversal pointer, p points
//to the node to be inserted (CASES 2, 3 AND 4), set traversal
//pointer cur
Node* cur = Head;
//Loop until the node was successfully inserted, the loop
//looks for the correct insertion point which could be front
//middle, or last
while (!wasSuccessful)
{
//insert node (p) still greater than current node (cur)
if (p->info > cur->info)
{
//are we at the last node? condition for last insertion (CASE 2)
if (cur->next == NULL)
{
//insert node at end of list
p->next = NULL;
p->prev = cur;
cur->next = p;
wasSuccessful = true;
}
else
{
//move the current pointer to the next node
cur = cur->next;
}
}
else
{
//we have found an insertion point, the node to be inserted
//is lesser than the current node in the list
//Is it an insert front condition? (CASE 3)
if (Head == cur)
{
p->next = Head;
p->prev = NULL;
Head->prev = p;
Head = p;
}
else
{
//Logically, this has to be an insert middle case (CASE 4)
p->next = cur;
p->prev = cur->prev;
cur->prev->next = p;
cur->prev = p;
}
wasSuccessful = true;
}
}
}
}
return wasSuccessful;
}
我为 Delete 函数编写的唯一内容是:
bool List::Delete(string data)
{
}
有没有人可以为我编写删除功能的代码?
【问题讨论】:
-
第 1 步:创建一个 find 函数,该函数接受要找到的字符串并返回找到的节点(如果未找到,则返回 null)。第 2 步:编写一个删除函数,该函数接受一个指向节点的指针,从列表中删除该节点并
deletes。第三步:编写删除函数。使用 find 函数查找要删除的节点,如果找到,请使用 remove 函数将其删除。一般经验法则:当一个问题太大而您的大脑无法吞下时,将其分解为较小的问题。 -
那我该怎么写呢?对不起,我是一个非常年轻的学生,我一直在与链接列表作斗争。我也处于时间紧缩状态(它的到期时间为 1 小时 45 分钟),而且我对按时完成感到非常压力
-
这不是您问题的答案,但您可以查看此视频youtu.be/JfmTagWcqoE,了解如何编写无泄漏算法。演讲者是 C++ 社区中非常杰出的人物。我认为你可以学到一些东西来应用到你的项目中。
-
找到要删除的节点应该很容易。从头开始循环遍历列表中的所有项目,直到找到保存字符串的节点(返回节点)或用完节点(返回
nullptr)。删除节点...变得棘手,因为你需要将之前的节点附加到之后的节点,反之亦然,而不会在您仍然需要它时意外地使任何点无效。这就是通过画图检查逻辑真正派上用场的地方。 -
最后一点:因为所有功能都建立在彼此的基础上,所以在你使用的时候,对每个功能进行详尽的测试。如果您在插入中犯了错误,则无法获得有效的删除,如果您浪费时间为插入中的错误调试删除,那是浪费了您本可以花在其他家庭作业或在酒吧喝啤酒的时间。或者在我的情况下运行冠军赛。想想看,我有时间玩 RPG 的主要原因是我很早就被介绍给调试器。学习使用开发工具附带的任何调试器。为您节省数年的调试时间。
标签: c++ linked-list