无法弄清楚如何通过仅使用类的函数删除整个链接列表
Posted
技术标签:
【中文标题】无法弄清楚如何通过仅使用类的函数删除整个链接列表【英文标题】:Unable to figure out how to delete the entire Linked List through a function using classes only 【发布时间】:2015-10-29 23:18:21 【问题描述】:作为 C++ 的初学者,我能够使用类而不是模板或向量来制作链表,并且达到了可以将节点添加到末尾,从末尾删除节点的地步(按值)并打印节点。但是,我现在想知道如何能够清除完整的节点集,从而清除完整的链表,而不使用任何专门的函数或特殊的标头。 我浏览了以下参考资源,但无法设置或使我的理解与它们保持一致,因为以下资源使用不同的方法完成了:-
Can't figure out how to clear a linked list in c++?
Linked List Delete Method (是的,我尝试从 java 中获取想法,即使我不知道 java 但也尝试过)
C programming Linked Lists delete node at position N
How would I clear a linked list?(最接近让我理解但有些混乱)
书籍:Deitel 和 Deitel(出人意料地接近我的想法,只是他们使用模板或矢量展示了)
书籍:入门(我有点难以理解)
任何帮助将不胜感激。请尝试原谅我的一些不良编程习惯,例如使用命名空间 std 等,因为我的目标是让 DeleteAll 函数正常工作(清除完整列表),请查看最后完全注释的代码块LinkedList 类的函数。 然后看看案例 4,我试图在主函数中调用它。这是我第一次使用链表的概念,是的,它看起来很吓人。
#include <iostream>
#include <cstdlib>
#include "ctype.h" //for enabling the recognition of data types for different illegal user inputs for main program
using namespace std;
// Node class
class Node
int data;
Node* next;
public:
Node();
void setData(int aData)
data = aData;
void setNext(Node* aNext)
next = aNext;
int Data()
return data;
Node* Next()
return next;
;
// LinkedList Class
class LinkedList
private:
Node *head;
public:
List()
head = NULL;
// printing contents of list
void Print()
Node *tmp = head;
// but if no Nodes then following
if (tmp==NULL)
cout<<"\n \t\tcannot find any Nodes: List EMPTY\n";
return;
//if one node is found
else if (tmp->Next()==NULL)
cout<<tmp->Data();
cout<<"-->";
cout<<"NULL"<<endl;
//else more nodes then parse and print the list
else
do
cout<<tmp->Data();
cout<<"-->";
tmp = tmp->Next();
while(tmp!=NULL);
cout<<"";
cout<<"X"<<endl;
//Append or add a node to the linked list
void Append(int data)
//creates a new node;
Node *newNode = new Node();
newNode->setData(data);
newNode->setNext(NULL);
//create a temp pointer for further linking to be facilitated
Node *tmp = head;
//but before setting next link to point last node to new node
//we need to check if we are at the end of the list and if not then traverse to the end of node
if(tmp !=NULL)
while(tmp->Next() !=NULL)
tmp = tmp->Next();
tmp->setNext(newNode);
else
head = newNode;
//Delete a node from list BY VALUE
void Delete(int data)
//again create a temp pointer.
Node *tmp = head;
//again if no nodes
if (tmp==NULL)
cout<<"\n \t\tNo Nodes to delete\n";
return;
//Last node in the list which is the same as only one node left in list then following
else if(tmp->Next()==NULL)
delete tmp;
head = NULL;
//again parse through the nodes again to delete the data related node.
else
Node *prev;
do
if(tmp->Data()==data)
break;
else
prev = tmp;
tmp = tmp->Next();
while(tmp != NULL);
//once the data is found and located then readjust the linkage of previous with next and
//delete the current node
prev->setNext(tmp->Next());
delete tmp;
//
// int Deleteall()
//
// again create a temp pointer.
// Node *tmp = head;
//
//
// again if no nodes
// if (tmp==NULL)
//
// cout<<"\n \t\tNo Nodes to delete\n";
// return 0;
//
//
// Last node in the list which is the same as only one node left in list then following
// else if(tmp->Next()==NULL)
//
// delete tmp;
// head = NULL;
//
//
// again parse through the nodes again to delete the data related node.
// else
//
// Node *prev;
//
// while(tmp);
//
//
// once the data is found and located then readjust the linkage of previous with next and
// delete the current node
// prev->setNext(tmp->Next());
// delete tmp;
//
// return 0;
//
;
//Now call through the main function
int main()
//New List
LinkedList list1;
int usrdata, choice;
while(choice)
cout<<"\n\nPlease choose an action to be performed on the LinkedList\n\ninput 1 for adding/appending a node\ninput 2 for printing the list\ninput 3 for deleting the node by value\ninput 4 to exit\n\n";
cin>>choice;
switch(choice)
case 1 :
cout<<"\nEnter your desired data for adding/appending\n";
cin>>usrdata;
list1.Append(usrdata);
list1.Print();
continue;
case 2 :
cout<<"\nPrinting\n";
list1.Print();
break;
case 3 :
cout<<"\nEnter your desired data for removal/deleting\n";
cin>>usrdata;
list1.Delete(usrdata);
list1.Print();
break;
case 4 :
cout<<"\n deleting n exiting...\n";
// list1.Deleteall();
list1.Print();
goto end;
default :
cout<<"I think you should go home now you seem tired\n";
end:
cout<<"\n\nThis statement was reached because either you chose to exit or entered a wrong/illegal value or operation\n\n";
system("pause");
return 0;
如何获取最后一个函数(完全注释的行) DeleteAll 工作以清除整个列表,而不使用任何其他专门的标题或特殊函数等。
【问题讨论】:
【参考方案1】:全部删除其实是一个简单的过程。
算法如下:
current node is head
while the current node is not null
get the next node
delete the current node
current node is next node
set head to null
在删除所有内容时,您不必担心维护链接。
【讨论】:
非常感谢我理解该算法,但是当我编写相同的代码时我遇到了麻烦,在某处我让 -> 运算符无法在下一个集合中正常工作节点的事情,newaz 将尝试感谢您的响应。 @CodeMan,我不是为什么你甚至需要使用 -> 设置下一个节点。我可以发布算法的实现,但这会不利于您作为开发人员的学习。您能否发布我在上面发布的更简单算法的实现(而不是您在上面注释掉的复杂错误版本),我们可以解决您的误解吗?从长远来看,这对你会更好。 嘿,别误会我的意思,我完全同意你的看法。就像我在最后一行评论“将尝试”中所说的那样,我并没有真正要求相同的实现代码,因为即使我认为这不是一个好的做法,因为它无助于推动我进一步思考。 @DominicMcDonnell 我想尝试自己做,因为我希望能够永远记住这一点。该算法的帮助绰绰有余。现在并没有真正要求任何进一步的帮助或代码帮助。我想尝试和 V-invent-V 剩下的自己。 老实说。 :D :)。以上是关于无法弄清楚如何通过仅使用类的函数删除整个链接列表的主要内容,如果未能解决你的问题,请参考以下文章