### 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:

 ``1234567891011`` ``````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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111`` `````` #include "node1.h" #include #include 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.