insertBack doesn't work for any but the first element added. After that, you search for the end of the list, but don't actually add the new element! (And you use the same variable name for two different things.)
If you want to be able to add and remove things from both the front and back of the list, then you should probably have a "tail" pointer in StringList that always points to the last element. That way you don't have to constantly loop through the list just to find the end.
Don't use free() in C++. delete is used to deallocate memory allocated with new.
It's best not to dump the std namespace into your code. It really doesn't gain you much. If you really want to add symbols from the std namespace into your global namespace, then pick and choose:
Code:
using std::cout;
using std::endl;
using std::string;
It looks like ListNode could use a ctor:
Code:
ListNode(std::string v, ListNode *n = nullptr)
: value(v), next(n) {}
Then insertFront could be:
Code:
void StringList::insertFront(std::string word) {
head = new ListNode(word, head);
}
Use nullptr instead of NULL (that's what it's for!).
Initialize your variables when you declare them, not in a separate statement.
I.e., instead of this:
Code:
ListNode *nodePtr;
nodePtr = head;
do this:
Code:
ListNode *nodePtr = head;