kusur
kusur

Reputation: 618

C++ - Linked List not generating any output

I am trying a print a linked list. The program is as follows:

#include<iostream>
#include<vector>
using namespace std;
class Link
{
    public:
        int iData;
        double dData;
        Link* pNext;
        Link(int id,double dd)
        {
            iData = id;
            dData = dd;
        }
        void displayLink()
        {
            cout<<"iData = "<<iData<<"\n";
            cout<<"dData = "<<dData<<"\n";
        }
};
class OrderedList
{
    private:
        Link* pRoot;
    public:
        OrderedList()
        {
            pRoot = NULL;
        }
        void insert(int id,double dd)
        {
            Link* newlink = new Link(id,dd);
            Link* pCurrent = pRoot;
            Link* pPrev = pRoot;
            while(pCurrent!=NULL&&pCurrent->iData<=id)
            {
                pPrev = pCurrent;
                pCurrent = pCurrent->pNext;
            }
                newlink->pNext = pCurrent;
            if(pCurrent==NULL)
            {
                pCurrent = newlink;
            }
            else
            {
                pPrev->pNext = newlink;
            }
        }
        Link* search(int val)
        {
            Link* pCurrent = pRoot;
            while(pCurrent!=NULL&&pCurrent->iData<val)
            {
                pCurrent = pCurrent->pNext;
            }
            return pCurrent;
        }
        bool remove(int val)
        {
            Link* pCurrent = pRoot;
            Link* pPrev = pRoot;
            while(pCurrent!=NULL&&pCurrent->iData<val)
            {
                pPrev = pCurrent;
                pCurrent = pCurrent->pNext;
            }
            if(pCurrent==NULL)
                return false;
            else
            {
                Link* temp = pCurrent;
                pPrev->pNext = pCurrent->pNext;
                delete temp;
                return true;
            }
        }
        void displayList()
        {
            Link* pCurrent = pRoot;
            while(pCurrent!=NULL)
            {
                pCurrent->displayLink();
                pCurrent = pCurrent->pNext;
            }
        }
};
OrderedList ol;
    ol.insert(10,10.10);
    ol.insert(11,11.11);
    ol.insert(12,12.12);
    ol.insert(13,13.13);
    ol.insert(14,14.14);
    ol.insert(15,15.15);
    ol.insert(16,16.16);
    ol.insert(17,17.17);
    ol.insert(18,18.18);
    ol.insert(19,19.19);
    ol.insert(20,20.20);
    ol.insert(21,21.21);
    ol.insert(22,22.22);
    ol.insert(23,23.23);
    ol.insert(24,24.24);
    ol.insert(25,25.25);
    ol.insert(26,26.26);
    ol.insert(27,27.27);
    ol.insert(28,28.28);
    ol.insert(29,29.29);
    ol.displayList();

The problem that I am facing is that the code is looking correct and in running fine on paper. When I try to run it on computer, it compiles without any error. However, on executing it, no output is shown. Not even any error. The program just executes without displaying anything. I am not able pin point the problem. I think that the code is fine. Please help me with this problem.

Upvotes: 0

Views: 235

Answers (2)

Neil Kirk
Neil Kirk

Reputation: 21763

When you assign pRoot to pCurrent, and make pCurrent point to the new link, this does not update pRoot. When inserting the first node, you will have to update pRoot as well as pCurrent.

Upvotes: 0

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

OrderedList never sets pRoot to anything, other than setting it to NULL in the constructor. Therefore insert() doesn't do anything (other than create a Link and leak it), and the call to displayList() never enters the while loop. delete() does no work either, since it always sees an empty list.

Upvotes: 1

Related Questions