Program freezing issue (beginnner)

So I have a project for my CSCI class, it involves using linked lists. We have a class Node, Class List and class student.

I am having the problem in a member function called copy_sorted that takes list as a parameter and creates a new list that is sorted. The function freezes when more than 3 objects are in the list. Here is that function=
template <typename T>
void List<T*>::copy_sorted(const List<T*> &rhs)
{
Node<T*> *cur = rhs.head;

if(head != NULL)
{
clean();
}
if(rhs.head==NULL)
{
return;
}
int i=0;
while(cur != NULL)
{
if(i==0)
{
insert(*(cur->value));
}
else
{
insert_sorted(*(cur->value));
}
cur = cur -> next;
i++;
}
}

insert function:
template <typename T>
void List<T*>::insert(T st){
Node<T*> *ptr_to_node = new Node<T*>( new T(st), head);
head = ptr_to_node;

ptr_to_node = NULL;

length++;
}//insert

insert sorted function:
template <typename T>
void List<T*>::insert_sorted(T st)
{
Node<T*> *ptr = head;
Node<T*> *prev = ptr;
if(length == 0 || st < *(head -> value))
{
insert(st);
}
while(ptr != NULL)
{
if((ptr->value->get_name()) < st.get_name())
{
Node<T*> *temp = new Node<T*>(new T(st), ptr);
prev->next = temp;
length++;
return;
}
prev=ptr;
ptr=ptr->next;
}
}

main:
#include<iostream>
#include<string>
#include<ctime>
#include<algorithm>
using namespace std;
#include "list.cpp"
#include "student.h"

int main(){
List<Student* > alist;
Student st;
while(cin >> st){
alist.insert(st);
}
alist.print();
cout << "\nTesting copy-constructor:" << endl;
List<Student* > second(alist);
second.print();

cout << "\nTestsing operator=" << endl;
List<Student* > another = alist;
another.print();

cout << "\nTesting operator= on the list with items:" << endl;
another = alist;
another.print();

cout << "\nTesting copy_sorted that uses insert_sorted:" << endl;
another.copy_sorted(alist);
another.print();

Student at("Jasper", 1.85);
bool is_removed = another.find_remove(at);
if(!is_removed){
cerr << "ERROR: was unable to remove " << at << endl;
}
else{
cout << "\nTesting find_remove:" << endl;
another.print();
}

Student bt("Prat", 3.89);
second.insert_back(bt);
cout << "\nTesting insert_back:" << endl;
second.print();

is_removed = another.remove_back();
if(!is_removed){
cerr << "ERROR: was unable to remove back " << at << endl;
}
else{
cout << "\nTesting remove_back:" << endl;
another.print();
}

cout << "\nTesting insert_after at the back:" << endl;
second.insert_after(bt, at);
second.print();

cout << "\nTesting insert_after:" << endl;
Student ct("Jo", 2.0);
another.insert_after(ct, bt);
another.print();

cout << "\nTesting print_greater:" << endl;
second.print_greater(ct);

return 0;
}//main()

Run it under a debugger. When it freezes, use the debugger to interrupt and see what it's doing. It almost certainly has not frozen; it's just in some infinite (or very long lasting) loop.
Topic archived. No new replies allowed.