Error printing doubly linked list backward

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$


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include<limits>
using namespace std;


struct listrec
{
	listrec *prev;
	listrec *next;
	float value;
};

listrec *head, *tail, *current, *temp;
float val;
int size;


int listSize (struct listrec* item, string direction)
{
	struct listrec* cur = item;
	int size = 0;
	if(direction == "forward"){

		while (cur != NULL)
		{
			++size;
			cur = cur->next;
		}
	}
	else {
		while (cur != NULL)
		{
			++size;
			cur = cur->prev;
		}

	}
	return size;
}

void printForwardElem() {
	std::cout << "Print Forward: " << std::endl;
	// Going forwards.
	listrec *iter;
	iter = head;
	while (1)
	{
		std::cout << iter->value+1 << std::endl;
		iter = iter->next;
		if (iter ==NULL)
			break;
	}
	size = listSize(head, "forward");
	cout << " total modules "<<size<< endl;

}


void printBackwardElem(){
	std::cout << "Print in reverse order: " << std::endl;
	// Reverse
	listrec *iter;
	iter = tail;
	while (1)
	{
		std::cout << iter->value+1 << std::endl;
		iter = iter->prev;
		if (iter == NULL)
			break;
	}
	size = listSize(tail, "backward");
	cout << " total modules "<<size<< endl;
}


int main()
{
	int userin;
	std::cout << "How many nodes do you want to create?: ";
	//	std::cin >> userin;
	while(!(cin >> userin) || userin < 0)
	{
		cin.clear();
		cin.ignore(numeric_limits<streamsize>::max(), '\n');
		cout << "Invalid input, the input must be zero or greater and not a letter, Please enter again:" << endl;
	}

	head   = NULL;
	tail    = NULL;
	current = NULL;
	temp    = NULL;

	if ( userin == 0){
		cout << " You created zero nodes, nothing to print " << endl;
		return 0;	
	}

	for (int i = 0; i < userin; ++i)
	{
		//  std::cout << "Enter " << i << "th value: ";
		//  std::cin >> val;      
		val = i;
		temp        = new listrec;
		temp->prev    = current;
		temp->next    = NULL;
		temp->value = val;

		if (current != NULL)
			current->next = temp;      
		current    = temp;

		// It's the first iteration.
		if (head == NULL && current->prev == NULL)
			head = temp;
		// Last Iteration.
		else if (i == userin - 1)
			tail = temp;
	}

	printForwardElem();
	printBackwardElem();   

	return 0;
}
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
}

Topic archived. No new replies allowed.