Ordered Linked List

For my assignment i'm supposed to implement the 4 cases of inserting a node in a linked list. I finished the code but when i execute it, it stops working but I have no idea what I did wrong. So the assignment is to insert a node into a linked list, making it an ordered 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
#include <iostream>
using namespace std;

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

void insert(nodeType *&first, nodeType *&last, int newItem);
void displayList(nodeType *&first);

int main()
{
	nodeType *first, *last;

	insert(first, last, 37); //C1
	insert(first, last, 14); //C2
	insert(first, last, 68); //C3A
	insert(first, last, 47); //C3B
	displayList(first);

	return 0;
}

void insert(nodeType *&first, nodeType *&last, int newItem)
{
	nodeType *newNode = NULL, *trail = NULL, *current = NULL;
	bool found;

	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;
		}
	}
	
}
void displayList(nodeType *&first)
{
	nodeType *current;
	current = first;
	while (current != NULL)
	{
		cout << current->info << endl;
		current = current->link;
	}
}

The reason for the crash is that on line 15 you do not initialize first/last with NULL.
ok, i fixed it and it doesn't crash. However, now when it runs it only outputs 47.
I cannot reproduce that. The output in the C++Shell is

14
37
47
68
Topic archived. No new replies allowed.