无法弄清楚如何通过仅使用 类 的函数删除整个链表

Unable to figure out how to delete the entire Linked List through a function using classes only

作为 C++ 的初学者,我能够使用 classes 而不是模板或向量来制作链表,并且达到了可以将节点添加到末尾的程度,从末尾删除一个节点(按值)并打印该节点。但是,我现在想知道如何能够清除完整的节点集,从而清除完整的链表,而不使用任何专门的函数或特殊的 headers.
我浏览了以下参考资源,但无法设置或调整我的理解,因为以下资源使用不同的方法完成:-

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 函数正常工作(清除完整列表),查看最后完全注释的代码块class 链表的函数。 然后看看案例 4,我第一次尝试在主 function.this 中调用它,这是我第一次接触链表的概念,是的,它看起来很吓人。

#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 可以在不使用任何其他专门 headers 或特殊函数等的情况下清除整个列表

全部删除其实是一个简单的过程。

算法如下:

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 

您不必担心在删除所有内容时维护链接。