I'm working on a project to create my own linked list and I'm having some trouble with the print function. Specifically, how to iterate through the pointers to items in the list. Each node has pointers to the previous and next items in the list. I also have a head and tail pointer.
string printList() {
stringstream ss;
int nodeCounter = 0;
ss << "print" << endl;
for (Node* i = head; i != tail; i = i->next) {
ss << "node " << nodeCounter << ": " << *i << endl;
nodeCounter++;
}
return ss.str();
}
But (obviously) it's not working. I think my problem is with, "i = i->next."
I want to start at the "head" pointer for the list and then jump to the "next" pointer for each node.
Any ideas or suggestions are very welcome, thanks for any help!
Maybe you can create a new pointer to Node and assign it the head. Then you do a while-loop that iterate as long as the next item doesn't point to NULL.
while (newPointer != NULL){
cout<< newPointer->item;
newPointer = newPointer->next;
}
EDIT: Had written something wrong on the while-line. Corrected now :)