Nignig
Nignig

Reputation: 9

C++ Linked list insert back

I am trying to create a function that will insert a node to the back of the list using linked list. I am new to using linked list and I have tried many different ways of doing an insertion at the end of the list but nothing seems to be working. The main passes the values one at a time to be inserted 2 4 5 8 9, and the output is 2 0 0 0 0. I do not whats causing this problem.

.h

class Node
{
public:
    Node() : data(0), ptrToNext(NULL) {}
    Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext){}
    Node* getPtrToNext() const { return ptrToNext; }
    int getData() const { return data; }
    void setData(int theData) { data = theData; }
    void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; }
    ~Node(){}
private:
    int data;
    Node *ptrToNext;    //pointer that points to next node
};

class AnyList
{
public:
    AnyList();  
        //default constructor
    void print() const;
        //Prints all values in the list.
    void destroyList();
        //Destroys all nodes in the list.
    ~AnyList();
        //destructor
    int getNumOfItems();
    void insertBack(int b);
    void deleteFirstNode();

private:
    Node *ptrToFirst; //pointer to point to the first node in the list
    int count;        //keeps track of number of nodes in the list
};

.cpp

void AnyList::insertBack(int b)
{
    Node *temp = new Node;

    if (ptrToFirst == NULL)
    {
        temp->setData(b);
        ptrToFirst = temp;
    }
    else
    {
        Node *first = ptrToFirst;
        while (first->getPtrToNext() != NULL)
        {
            first = first->getPtrToNext();
        }
        first->setPtrToNext(temp);
    }
}

Upvotes: 0

Views: 1091

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598164

First, you really should be using the std::list or std::forward_list class instead of implementing the node handling manually:

#include <list>

class AnyList
{
public:
    void print() const;
        //Prints all values in the list.
    void destroyList();
        //Destroys all nodes in the list.
    int getNumOfItems() const;
    void insertBack(int b);
    void deleteFirstNode();

private:
    std::list<int> nodes; //nodes in the list
};

void AnyList::print() const
{
    for (std::list<int>::const_iterator iter = nodes.begin(); iter != nodes.end(); ++iter)
    {
        int value = *iter;
        // print value as needed...
    }
}

void AnyList::destroyList()
{
    nodes.clear();
}

void AnyList::getNumOfItems() const
{
    return nodes.size();
}

void AnyList::insertBack(int b)
{
    nodes.push_back(b);
}

void AnyList::deleteFirstNode()
{
    if (!nodes.empty())
        nodes.pop_front();
}

That being said, your manual implementation fails because you are likely not managing the nodes correctly (but you did not show everything you are doing). It should look something like this:

class Node
{
public:
    Node() : data(0), ptrToNext(NULL) {}
    Node(int theData, Node *newPtrToNext) : data(theData), ptrToNext(newPtrToNext) {}
    ~Node() {}
    Node* getPtrToNext() const { return ptrToNext; }
    int getData() const { return data; }
    void setData(int theData) { data = theData; }
    void setPtrToNext(Node *newPtrToNext) { ptrToNext = newPtrToNext; }
private:
    int data;
    Node *ptrToNext;    //pointer that points to next node
};

class AnyList
{
public:
    AnyList();  
        //default constructor
    ~AnyList();
        //destructor
    void print() const;
        //Prints all values in the list.
    void destroyList();
        //Destroys all nodes in the list.
    int getNumOfItems() const;
    void insertBack(int b);
    void deleteFirstNode();

private:
    Node *ptrToFirst; //pointer to point to the first node in the list
    Node *ptrToLast;  //pointer to point to the last node in the list
    int count;        //keeps track of number of nodes in the list
};

AnyList:AnyList()
    : ptrToFirst(NULL), ptrToLast(NULL), count(0)
{
}

void AnyList::print() const
{
    for (Node *temp = ptrToFirst; temp != NULL; temp = temp->getPtrToNext())
    {
        int value = temp->getData();
        // print value as needed...
    }
}

AnyList::~AnyList()
{
    destroyList();
}

void AnyList::destroyList()
{
    Node *temp = ptrToFirst;
    ptrToFirst = ptrToLast = NULL;
    count = 0;

    while (temp != NULL)
    {
        Node *next = temp->getPtrToNext();
        delete temp;
        temp = next;
    }
}

int AnyList::getNumOfItems() const
{
    return count;
}

void AnyList::insertBack(int b)
{
    Node *temp = new Node(b, NULL);

    if (ptrToFirst == NULL)
        ptrToFirst = temp;

    if (ptrToLast != NULL)
        ptrToLast->setPtrToNext(temp);

    ptrToLast = temp;
    ++count;
}

void AnyList::deleteFirstNode()
{
    if (ptrToFirst == NULL)
        return;

    Node *temp = ptrToFirst;
    ptrToFirst = temp->getPtrToNext();

    if (ptrToLast == temp)
        ptrToLast = NULL;

    delete temp;
    --count;
}

Upvotes: 1

Related Questions