Linked List Infinite Loop Help

So I'm working with a linked list and trying to sort it. I put random numbers into the linked list. First I find the lowest value and put it as the head and every following number builds a chain off the head The functions at the top were for debugging purposes and I think I narrowed it down to somewhere in:

1
2
3
4
5
6
7
8
9
10
11
if (beforeMin != NULL) beforeMin->set_link(min->link());
    d(4);
    if (marker == NULL) {
       if (min != head) {
           min->set_link(head);
           head = min;
       }
    } 
    else {
        min->set_link(marker->link());
    }


The full code is
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
     #include "node1.h"
     #include <cstdlib>
     #include <iostream>

     using namespace std;
 
     using namespace main_savitch_5;

     void d(int val) {
   	cout << val << endl;
     }

     void print(node* head) {
	node* current = head;
	int count = 0;
	
	while (current != NULL)
	{
		cout << current->data() << " ";
		current = current->link();
		
		if (count++ > 10) break;
	}
	cout << endl;
      }

      int main()
      {
	size_t n;
	cout << "Please enter the number of values you want in the linked list: ";
	cin >> n;

	node* head = NULL;
	node* current = NULL;

	node* marker = NULL;
	node* beforeMin = NULL;
	node* min = NULL;

	node* previous = NULL;

	node* temp = NULL;

	//srand(time(NULL));
	if (n == 0)
		cout << "Invalid" << endl;
	else
	{
		
		list_head_insert(head, rand() % 1000 + 1);
		current = head;
		
		while (n-- > 1) 
		{
			list_insert(current, rand() % 1000);
			current = current->link();
		}
		
		current = head;
		while (current != NULL)
		{
			cout << current->data() << " ";
			current = current->link();
		}
		cout << endl;
		
		while ((marker == NULL || marker->link() != NULL) && head->link() != NULL)
		{
			d(1);
			current = (marker != NULL) ? marker->link() : head;
			min = current;
			d(2);
			print(head);
			current = current->link();
			while (current->link() != NULL) {
				if (min->data() > current->data()) {
					min = current;
					beforeMin = previous;
				}
				
				previous = current;
				current = current->link();
			}
			d(3);
			if (beforeMin != NULL) beforeMin->set_link(min->link());
			d(4);
			if (marker == NULL) {
				if (min != head) {
					min->set_link(head);
					head = min;
				}
			} 
                        else {
				min->set_link(marker->link());
			}
			d(5);
			if (marker != NULL) marker->set_link(min);
			marker = min;
			d(6);
		}
		
		current = head;
		while (current != NULL)
		{
			cout << current->data() << " ";
			current = current->link();
		}
		cout << endl;
	}
	return 0;
      }

When I run the program I get

Please enter the number of values you want in the linked list: 5
384 886 777 915 793
1
2
384 886 777 915 793
3
4
5
6
1
2
384 886 777 915 793
3
4
5
6
1
2
384 777 886 777 886 777 886 777 886 777 886 777...

So it gives me my five random numbers if I put 5 as n, it sorts for the first 3, then it starts repeating the 2nd and 3rd lowest forever.
Any help with this would be appreciated I've been staring at the code at I'm not getting any further at this point.
Last edited on
Topic archived. No new replies allowed.