Search code examples
c++insertlinked-listtraversal

Adding a node to the end of a linked list


I've been trying to add an item to the end of a linked list. I think I have a handle on the concept but i'm having difficulty implementing the code. In particular, being able to traverse a linked list and find the tail. Here is what i have so far. I've been at this for a while trying different things. Any help would be appreciated.

##include <iostream>
using namespace std;


class node
{
public:
    int data;
    node *next;
};


class linkedList
{
private:
    node* ptrHead;
    node* ptrTail;

    int size;

public:
    linkedList();  //default constructor
    void display();
    void addFront(int);
    void removeFront();
    void addBack(int);
    void removeBack();
};

//default constructor
linkedList::linkedList(){
    size = 0;
    ptrHead = ptrTail = NULL;
}
//display linked list
void linkedList::display(){
    node* current = ptrHead;

    while (current != NULL) {
        cout << current->data << " "; //display current item
        current = current->next; //move to next item
    }
    cout << size;
}
//add item to front of linked list
void linkedList::addFront(int addData){

    node* n = new node;
    n->next = ptrHead;
    n->data = addData;
    ptrHead = n;

    size++;
}
//remove item from front of linked list
void linkedList::removeFront(){

    node* n = ptrHead;
    ptrHead = ptrHead->next;
    delete n;

    size--;
}

       void linkedList::addBack(int addData){   ////work in progress


        node* n = new node; //create new node
        n->data = addData;  //input data
        n->next = NULL;     //set node to point to NULL

        if ( ptrTail == NULL )  // or if ( ptrTail == nullptr )
        {
            ptrHead = n;
            ptrTail = n;
        }
        else
        {
            ptrTail->next = n;
            ptrTail = n;
        }

        size++;
    }



    //this is the test code from my main function
              int main()
        {
            //test code
            linkedList list;

            list.addFront(40);
            list.addFront(30);
            list.addFront(20);
            list.addFront(10);
            list.addFront(0);
            list.addBack(50);
            list.addBack(60);

            list.display(); //50 60 7  (the 7 is the count/size of the linked list)
            cout << endl;
        }

Solution

  • You did not show the definition of your linkedList.

    So I can only suppose that it has data members ptrTail and ptrHead. In this case the function will look the following way

    void linkedList::addBack(int addData)
    {
       node* n = new node; //create new node
       n->data = addData;  //input data
       n->next = NULL;     //set node to point to NULL
    
       if ( ptrTail == NULL )  // or if ( ptrTail == nullptr )
       {
          ptrHead = n;
       }
       else
       {
          ptrTail->next = n;
       }
       ptrTail = n;
    
       size++;
    }
    

    Function addFront can be defined the similar way

    void linkedList::addFront(int addData)
    {
       node* n = new node; //create new node
       n->data = addData;  //input data
    
       if ( ptrHead == NULL )  // or if ( ptrHead == nullptr )
       {
          ptrTail = n;
       }
    
       n->next = ptrHead;
       ptrHead = n;
    
       size++;
    }
    

    One more function

    void linkedList::removeFront()
    {
        if ( ptrHead != NULL )
        {
            if ( ptrTail == ptrHead ) ptrTail = NULL;
            node* n = ptrHead;
            ptrHead = ptrHead->next;
            delete n;
            size--;
        }
    }