简体   繁体   English

为什么我的C ++程序不能打印出LinkedList的所有项目?

[英]Why my c++ program doesn't print all the items of a LinkedList?

I am learning c++ and tried a creating a Linked-List Data structure. 我正在学习c ++,并尝试创建一个链表数据结构。 Here is the program- 这是程序-

main.cpp main.cpp

        #include <iostream>
        using namespace std;

    class LinkedList
    {
    public:
        int data;
        LinkedList *nextNode;

        void init(int value,LinkedList *root)
        {
            root->data=value;
            root->nextNode=NULL;
        }

        void add(int value,LinkedList *root)
        {
            LinkedList *temp=new LinkedList;
            if(root->nextNode==NULL)
            {
                //cout<<"IF ADD()"<<endl;
                temp->data=value;
                temp->nextNode=NULL;
                root->nextNode=temp;
            }
            else
            {
                //cout<<"else ADD()"<<endl;
                while(root->nextNode!=NULL)
                {
                    root=root->nextNode;
                }
                temp->data=value;
                temp->nextNode=NULL;
                root->nextNode=temp;
            }
        }

        void display(LinkedList *root)
        {
            if(root->nextNode==NULL)
            {
                cout<<root->data<<endl;
            }
            else
            {
                while(root->nextNode!=NULL)
                {
                    cout<<root->data<<endl;
                    root=root->nextNode;
                }

            }
        }

        void free(LinkedList *root)
        {
            if(root->nextNode==NULL)
            {
                delete root;
            }
            else
            {
                while(root->nextNode!=NULL)
                {
                    LinkedList *temp=root->nextNode;
                    delete root;
                    root=temp;
                }
            }
        }

    };

    int main()
    {
        LinkedList *root=new LinkedList;
        root->init(1,root);
        root->add(2,root);
        root->add(3,root);
        root->add(4,root);
        root->add(5,root);
        root->add(6,root);
        root->add(7,root);
        root->add(8,root);
        root->add(9,root);
        root->add(10,root);
        root->display(root);
        root->free(root);
        //root->display(root);
        //delete root;
        return 0;
    }

Output- 1 2 3 4 5 6 7 8 9 输出 -1 2 3 4 5 6 7 8 9

My question is why it doesn't print the last item ie 10. And one more question, as you can see I am commenting in the following line 我的问题是为什么它不打印最后一个项目,即10。还有一个问题,如您所见,我在下一行中进行评论

//delete root

inside my main method. 在我的主要方法中。 What if I don't call my free() method and uncomment this? 如果我不调用我的free()方法并取消注释该怎么办? Would it free the whole LinkedList? 它会释放整个LinkedList吗?

Thanks 谢谢

while(root->nextNode!=NULL)
{
    cout<<root->data<<endl;
    root=root->nextNode;
}

should be 应该

while(root!=NULL)
{
    cout<<root->data<<endl;
    root=root->nextNode;
}

Porblem is with this function in else statement it does not go to last node because last node's next will be NULL. Porblem在else语句中具有此功能,它不会到达最后一个节点,因为最后一个节点的下一个将为NULL。 One more thing is you are modifying the root in display function is should be a const function. 还有一件事是您要修改显示函数的根,应该是const函数。 It should not modify the root. 它不应修改根目录。

void display(LinkedList *root)
        {
            if(root->nextNode==NULL)
            {
                cout<<root->data<<endl;
            }
            else
            {
                while(root->nextNode!=NULL)
                {
                    cout<<root->data<<endl;
                    root=root->nextNode;
                }

            }
        }

Actual implementation should be like this. 实际的实现应该是这样的。

void display(LinkedList *root) const
{
    LinkedList * temp = root;
    while(temp!=NULL)
    {
        cout<<temp->data<<endl;
        temp=temp->nextNode;
    }  
}

Regarding your second question-> It won't clear the memory. 关于第二个问题->它不会清除内存。 But because its a small code after application close memory will be freed. 但是由于它在应用程序关闭内存后的一小段代码将被释放。 It's always best practice to free memory otherwise you will have memory leaks and if this data structure is used heavily it can take all memory and crash the application. 释放内存是最好的做法,否则会发生内存泄漏,如果大量使用此数据结构,可能会占用所有内存并使应用程序崩溃。

Also implementation of your free function is not correct. 另外,您的自由功能的实现不正确。 It should be like this 应该是这样

void free(LinkedList *root)
{
    LinkedList * temp = root;
    LinkedList * nodeToFree = root;
    while(temp!=NULL)
    {
        temp=temp->nextNode;
        delete nodeToFree;
        nodeToFree = temp;
    }
}

change Condition of display 更改显示条件

#include <iostream>
    using namespace std;

class LinkedList
{
public:
    int data;
    LinkedList *nextNode;

    void init(int value,LinkedList *root)
    {
        root->data=value;
        root->nextNode=NULL;
    }

    void add(int value,LinkedList *root)
    {
        LinkedList *temp=new LinkedList;
        if(root->nextNode==NULL)
        {
            //cout<<"IF ADD()"<<endl;
            temp->data=value;
            temp->nextNode=NULL;
            root->nextNode=temp;
        }
        else
        {
            //cout<<"else ADD()"<<endl;
            while(root->nextNode!=NULL)
            {
                root=root->nextNode;
            }
            temp->data=value;
            temp->nextNode=NULL;
            root->nextNode=temp;
        }
    }

    void display(LinkedList *root)
    {
        if(root->nextNode==NULL)
        {
            cout<<root->data<<endl;
        }
        else
        {
            while(root !=NULL) // Change this condition
            {
                cout<<root->data<<endl;
                root=root->nextNode;
            }

        }
    }

    void free(LinkedList *root)
    {
        if(root->nextNode==NULL)
        {
            delete root;
        }
        else
        {
            while(root->nextNode!=NULL)
            {
                LinkedList *temp=root->nextNode;
                delete root;
                root=temp;
            }
        }
    }

Here you can display it one time only as you are changing its root value so it would be better if you use temp variable to display as done by @sanjay in above case. 在这里,您只能在更改其根值时显示它一次,因此,在上述情况下,如果使用temp变量显示@sanjay,则更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM