Why does not my C ++ program print all the elements of a linked list?

advertisements

I am learning c++ and tried a creating a Linked-List Data structure. Here is the program-

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

My question is why it doesn't print the last item i.e 10. And one more question, as you can see I am commenting in the following line

//delete root

inside my main method. What if I don't call my free() method and uncomment this? Would it free the whole LinkedList?

Thanks


Porblem is with this function in else statement it does not go to last node because last node's next will be NULL. One more thing is you are modifying the root in display function is should be a const function. 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;
    }
}