This is just wrong!

Whenever I try to print out my linked list after generating a bunch of random values, it never displays right. It only shows 1 or 2 values in the list, when it should be displaying 10-1000 values.

This is my Generate() function:

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
void SortedLinkedList::Generate(const int numValue)
{
	//variables
	int i;
	int j;
	int ranchar;
	//Timer started
	double start,end;
	start = GetTickCount();
	//string *astring;
	//astring = new string[numValue]; //creates an array of strings equal to numValue
	for(i = 0; i < numValue; i++) {
		string *astring;
		astring = new string[numValue];
		for(j = 0; j < 5; j++) { //creates strings of random characters
			ranchar = rand()%26; //code from iLab doc
			astring[i] += ('a'+ranchar); //astring is the actual generated string
		}
		length++; 
		if(Empty()) { //this is basically a repeat of the code in PutItem() function
			ptrNode* temp = new ptrNode;
			head = temp;
			tail = temp;
			temp->prev = NULL;
			temp->next = NULL;
			temp->name = astring[i]; //i represents which array value of astring
		}
		else {
			currentPos = head;
			while(astring[i] > currentPos->name && currentPos->next != tail->next) {
				currentPos = currentPos->next;
			}
			if(currentPos == head) { //places value after head
				ptrNode* temp = new ptrNode;
				temp->name = astring[i];
				temp->prev = currentPos;
				temp->next = NULL;
				head->next = temp;
				tail = temp;
			}
			else {
				if(currentPos == tail && astring[i] > tail->name) { //places value after tail
					tail->next = new ptrNode;
					(tail->next)->prev = tail;
					tail = tail->next;
					tail->next = NULL;
					tail->name = astring[i];
				}
				else { //places value before currentPos
					ptrNode* temp = new ptrNode;
					temp->name = astring[i];
					temp->next = currentPos;
					(currentPos->prev)->next = temp;
					temp->prev = currentPos->prev;
					currentPos->prev = temp;
				}
			}
		}
		
	}
	//Timer ends
	end = GetTickCount();
	double timetaken = (end - start);
	cout << "This took " << timetaken << " milliseconds too complete\n";
}


And this is my Print() function:
1
2
3
4
5
6
7
8
9
10
11
12
void SortedLinkedList::Print()
{
	if(Empty()) {
		cout << "There are no values in the list to Print!\n";
		return;
	}
	else {
		for(currentPos = head; currentPos != tail->next; currentPos = currentPos->next) {
			cout << currentPos->name << " ";
		}
	}
}


When I step through it, it looks like the problem has something to do with the pointers not pointing to the correct location in the Generate() function. I've been up working on this for the last 20 hours. Can someone throw me a bone?
Last edited on
Topic archived. No new replies allowed.