I am having one issue with the follow code, and that is if I tell it to generate 1 node it throws a seg fault error on the print backward function. If I tell it zero or any number greater than one it appears to work just fine. Can anyone see whats causing it to not print correctly with only one generated node?
Here is the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
wxxx@xxxxx:~/xxxx/xxxx$ ./a.out
How many nodes do you want to create?: 1
Print Forward:
1
total modules 1
Print in reverse order:
Segmentation fault
wxxx@xxxxx:~/xxxx/xxxxxx$ ./a.out
How many nodes do you want to create?: 2
Print Forward:
1
2
total modules 2
Print in reverse order:
2
1
total modules 2
wxxx@xxxxx:~/xxxx/xxxx$
Here's a general outline of a reverse print method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void printReverse()const
{
if(!head)return; //empty list
Node* temp = head;//assign head to temp node
while (temp)//i.e. temp has not reached the end
{
temp = temp -> next;//iterate through the list
}//temp is now at the last node
do
{
std::cout << temp -> data << "\n";//print data of the node
temp = temp -> prev;// move backwards ...
} while (temp != head);// ... until reaching back to head
}