Function to create objects keeps making the same object

Can I ask, is this first in last out implemented? It looks that way. Your head is going to be the top item in the list i.e the most recently inserted.

So here

      if (newPosition == 1) {
          newNodePtr->setNext(headPtr);
          headPtr = newNodePtr;
      } 

That's your base insert right. Assuming the list has an initial value when created?

So the top newnode's next is going to be the previous item. And the newnode becomes the head. So...

          Node<ItemType> *prevPtr = getNodeAt(newPosition - 1);

          newNodePtr->setNext(prevPtr->getNext());
          prevPtr->setNext(newNodePtr);

Here, you are getting the previous item, which should be headPtr now right before you've made the insertion? But then you are setting the newNode's next, to the current heads next??

Say you have a list of 5 items. You are now inserting the 6th item. And the 6th items next is now going to point at item 4...

Shouldn't you be setting newNode's next to prevPtr?

And then you are setting prevPtr's next again? prevPtr's next shouldn't be touched. It already knows it's next node.

/r/cpp_questions Thread