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



作为C++的初学者,我能够使用类而不是模板或向量来创建链表,并且可以将节点添加到末尾,从末尾删除节点(按值)并打印节点。然而,我现在想知道如何在不使用任何专用函数或特殊标头的情况下清除完整的节点集,从而清除完整的linkedlist
我浏览了以下参考资料,但无法设置或调整我的理解,因为以下资料使用了不同的方法:-

可以';我不知道如何在c++中清除链表吗?

链表删除方法(是的,我试着从java中获得想法,尽管我不知道java,但我也试过了)

C编程链表删除位置N 的节点

如何清除链接列表?(最接近让我理解,但有些困惑)

书:Deitel和Deitel(令人惊讶地接近我的想法,只是他们使用模板或矢量显示)

书籍:初级读本(对我来说有点难以理解)

如有任何帮助,我们将不胜感激。请尝试并原谅我的一些糟糕的编程习惯,比如使用命名空间std和etc等,因为我的目标是让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 ttcannot find any Nodes: List EMPTYn";
                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 ttNo Nodes to deleten";
                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 ttNo Nodes to deleten";
//              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<<"nnPlease choose an action to be performed on the LinkedListnninput 1 for adding/appending a nodeninput 2 for printing the listninput 3 for deleting the node by valueninput 4 to exitnn";
cin>>choice;
switch(choice)
{
    case 1 :
        cout<<"nEnter your desired data for adding/appendingn";
        cin>>usrdata;
        list1.Append(usrdata);
        list1.Print();
        continue;
    case 2 :
        cout<<"nPrintingn";
        list1.Print();
        break;
    case 3 :
        cout<<"nEnter your desired data for removal/deletingn";
        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 tiredn";    
}
}
end:
cout<<"nnThis statement was reached because either you chose to exit or entered a wrong/illegal value or operationnn";
system("pause");        
return 0;
}

如何获得最后一个函数(完全注释行)DeleteAll正在清除整个列表,而不使用任何其他专用标题或特殊函数等。

Delete all实际上是一个简单的过程。

以下是算法:

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 

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

相关内容

  • 没有找到相关文章

最新更新