practice with linked list

I'm just doing some practice with linked list as to study and ran into a problem.
So far I have 4 functions: build a list forward, moving a node from front to back and deleting front, sorting an unsorted list, and a display. The first 2 functions work just fine, but when I execute the program the function to sort the list doesnt work and outputs the same thing as my function to move a node to the back. I don't know if it has something to do with the functions or if I just can't build an unordered LL in the same program as an ordered linked list.

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
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
using namespace std;

ifstream infile;

struct nodeType
{
	int info;
	nodeType *link;
};

void buildListFor(nodeType *&first, nodeType *&last);
void display(nodeType *first);
void front2back(nodeType *&first, nodeType *&last);
void sortList(nodeType *&first, nodeType *&last);

int main()
{
	infile.open("LLrac3info.txt");
	nodeType *first = NULL, *last = NULL;
	buildListFor(first, last);
	display(first);
	front2back(first, last);
	display(first);


	sortList(first, last);
	display(first);
}

void buildListFor(nodeType *&first, nodeType *&last)
{
	int digit;
	int count = 0;
	nodeType *newNode;
	infile >> digit;
	cout << "The list is: " << endl;
	while (!infile.eof())
	{
		newNode = new nodeType;
		newNode->info = digit;
		newNode->link = NULL;

		if (first == NULL)
		{
			first = newNode;
			last = newNode;
			count++;
		}
		else
		{
			last->link = newNode;
			last = newNode;
			count++;
		}
		infile >> digit;
	}
}
void display(nodeType *first)
{
	nodeType *current;
	current = first;
	while (current != NULL)
	{
		cout << current->info << endl;
		current = current->link;
	}
}
void front2back(nodeType *&first, nodeType *&last)
{
	cout << "The list after moving front to back and deleting front is: " << endl;
	nodeType *newNode;
	nodeType *temp = first;

	newNode = new nodeType;
	newNode->info = first->info;
	newNode->link = NULL;

	last->link = newNode;
	last = newNode;

	first = first->link;
	delete temp;
}
void sortList(nodeType *&first, nodeType *&last)
{
	cout << "The list aftr being sorted is: " << endl;
	nodeType *newNode;
	nodeType *current;
	nodeType *trail = NULL;
	bool found;
	int newItem;

	infile >> newItem;
	while (!infile.eof())
	{
		newNode = new nodeType;
		newNode->info = newItem;
		newNode->link = NULL;

		if (first == NULL)
		{
			first = newNode;
			last = newNode;
		}
		else
		{
			current = first;
			found = false;

			while (current != NULL && !found)
				if (current->info >= newItem)
					found = true;
				else
				{
					trail = current;
					current = current->link;
				}
			if (current == first)
			{
				newNode->link = first;
				first = newNode;
			}
			else
			{
				trail->link = newNode;
				newNode->link = current;

				if (current == NULL)
					last = newNode;
			}
		}
	}
}
All sortList does currently is leak memory. The only way first and last are assigned a value is if they were null when the function was entered, and they are not.

Logically a function called sortList should sort a list that already exists. It should not be reading from a file (and, in fact, it probably doesn't since infile is a global stream that has already reached end-of-file.)
Last edited on
Topic archived. No new replies allowed.