Linked Lists Program

So, I'm trying to write a program where the user inputs 5 integers, and then the program uses linked lists to sort the integers as they are added to the list. Unfortunately, I've seemed to have confused myself, and now the program crashes after I enter 3 integers. I really need some help figuring this out. Thank you so much.
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
#include <iostream>
using namespace std;

void getdata(int& ident);
const int nil = 0;
class node_type
{
public:
	int id;
	node_type *next;
};

void main(){
	node_type *first, *p, *q, *newnode;
	int i, ident = 0;
	first = new node_type;
	p = first;
	getdata(ident);
	(*first).id = ident;
	(*first).next = nil;
	for (i = 0; i <= 4; i++){
		newnode = new node_type;
		getdata(ident);
		(*newnode).id = ident;
		q = new node_type;
		getdata(ident);
		(*q).id = ident;
		(*q).next = nil;
		(*p).next = q;
		if (((*first).id) > ((*newnode).id)){
			(*newnode).next = first;
			first = newnode;
		}
		else if ((*first).next == nil){
			(*first).next = newnode;
		}
		else{
			q = first;
			p = (*q).next;
			while (((*newnode).id) > (((*p).id) && ((*p).next) != nil)){
				q = p;
				p = (*p).next;
				if (((*p).id) > ((*newnode).id)){
					(*q).next = newnode;
					(*newnode).next = p;
				}
				else{
					(*p).next = newnode;
				}
			}
		}
		p = q;
	}
	first = p;
	p = first;
	while ((*p).next != nil){
		cout << (*p).id << " ";
		p = (*p).next;
	}

	cout << endl;
}
void getdata(int& ident)
{
	cout << "Enter an integer: ";
	cin >> ident;
}
Topic archived. No new replies allowed.