mpg
mpg

Reputation: 13

Head of linked list always null after adding new node

This is a homework question. I'm struggling with writing my linked list data structure. I think my problem is in my add function. It looks like I'm not managing to reassign the head pointer properly, but I'm not sure what I'm doing wrong.

Add():

template<class listType>
void LinkedList<listType>::Add(int i, string n, listType h, listType r)
{
    Node<listType>* newnode = new Node<listType>(i, n, h, r);
    head = newnode;

    newnode->SetNext(head);

But when call the display function (which passes the head to print) from main, the print function always displays the list is empty

Print Function:

template<class listType>
 void LinkedList<listType>::Display()
{
    Print(head);
}

template<class listType>
void LinkedList<listType>::Print(Node<listType>* temp)
{
    if (temp == NULL)
    {
        cout << "\nEnd of list.\n";
        return;
    }
    else 
    {
        cout << temp->GetName() << "\n";
        Print(temp->GetNext());
    }
}

I've tried passing the head by reference as well but to no avail.

Full LinkedList source:

template <class listType>
class LinkedList {

private:
    Node<listType>* head;
public:
    LinkedList();
    ~LinkedList();
    void Add(int i, string n, listType h, listType r);
    void Display();
    void Print(Node<listType>*);
};

template<class listType>
 bool LinkedList<listType>::IsEmpty() const
{
    return head == NULL;
}

template<class listType>
 LinkedList<listType>::LinkedList()

{
    head = NULL;
}

template<class listType>
 LinkedList<listType>::~LinkedList()
{
}

template<class listType>
void LinkedList<listType>::Add(int i, string n, int h, int r)
{
    Node<listType>* newnode = new Node<listType>(i, n, h, r);
    head = newnode;

    newnode->SetNext(head);

    cout << newnode->GetId() << "\t" << newnode->GetName() << "\t";
    cout << newnode->GetHours() << "\t" << newnode->GetRate() << "\n";
}

template<class listType>
 void LinkedList<listType>::Display()
{
    Print(head);
}

template<class listType>
void LinkedList<listType>::Print(Node<listType>* temp)
{
    if (temp == NULL)
    {
        cout << "\nEnd of list.\n";
        return;
    }
    else 
    {
        cout << temp->GetName() << "\n";
        Print(temp->GetNext());
    }
}

Edit: Adding the main function, as well as a data sample:

Main:

int main()
{
    LinkedList<int> integerList;

    fstream fin("data1.txt");
    LoadData<int>(integerList, fin, "data1.txt");
    cout << "\n testing Display()\n\n";

    integerList.Display();

    return 0;
}

A function to populate the list from a text file:

template<class type>
void LoadData(LinkedList<type> list, fstream& fin, string fileName)
{
    int id;
    string nameFirst, nameLast, nameFull;
    type hours, rate;

    if (fin.is_open())
    {
        cout << "Loading: " << fileName << '\n';
        while (!fin.eof())
        {
            fin >> id >> nameLast >> nameFirst >> hours >> rate;
            nameFull = nameFirst + " " + nameLast + "\b \b";
            list.Add(id, nameFull, hours, rate);

        }
        cout << fileName << " loaded succesfully.\n";
    }
    else {
        cout << "File not found, exiting.";

    }
    fin.close();
}

Text file example:

21169
Ahmed, Marco
40 10
24085
ATamimi, Trevone
30 15
28139
Choudhury, Jacob 
45 12

Upvotes: 1

Views: 456

Answers (1)

MFisherKDX
MFisherKDX

Reputation: 2866

Your first problem is that you are passing your list by value to LoadData from main. This means that the LoadData function actually takes a copy of the list from main and adds to the copy. The original list is unaffected.

So when you call integerList.Display(), it actually never changed and is still empty.

Instead you should pass your list by reference to LoadData as so:

void LoadData(LinkedList<type>& list, fstream& fin, string fileName)

Your next problem is in your function void LinkedList<listType>::Add(int i, string n, listType h, listType r). The following code creates a cycle of length 1.

head = newnode;
newnode->SetNext(head);

What you want to do is add newnode to the front of the list before head, and update head.

newnode->SetNext(head);
head = newnode;

Upvotes: 4

Related Questions