程序莫名终止,求解答

#include<stdio.h>
#include<stdlib.h>
struct student
{
	int num;
	float score;

	struct student* next;
};
void print(struct student* head);
struct student* creatsort();
int main()
{
	struct student* head;
	head = creatsort();
	print(head);
	return 0;
}
struct student* creatsort()
{
	struct student* head = NULL;
	struct student* pnew, * back, * front;
	int num;
	float score;
	printf("请输入成绩:\n");
	scanf_s("%d,%f", &num, &score);
	if (num < 0)
	{
		return head;
	}
	while (num >= 0)
	{
		pnew = (struct student*)malloc(sizeof(struct student));
		pnew->num = num;
		pnew->score = score;
		if (head == NULL)
		{
			head = pnew;
			pnew->next = NULL;
		}
		else
		{
			back = front = head;
			if (head->score < score)
			{
				pnew->next = head;
				head = pnew;
			}
			else
			{
				front = back = head;
				while (front != NULL && front->score > score)
				{
					back = front;
					front = front->next;
				}
				if (front == NULL && front->score > score)
				{
					pnew->next = back->next;
					front->next = pnew;
				}
				else
				{
					pnew->next = front->next;
					front->next = pnew;
				}
			}
		}
		printf("新排名如下:\n");
		print(head);
		printf("请输入新成绩:\n");
		scanf_s("%d,%f", &num, &score);
	}

	return head;
}
void print(struct student* head)
{
	struct student* p;
	p = head;
	if (p == NULL)
	{
		printf("Empty!\n");
		return;
	}
	while (p != NULL)
	{
		printf("%d,%.2f\n", p->num, p->score);
		p = p->next;
	}
}

如上图,程序在第三次输入后就不再输出,莫名奇妙的就终止了。

修改如下,供参考:

#include<stdio.h>
#include<stdlib.h>
struct student
{
	int    num;
	float  score;
	struct student* next;
};

void   print(struct student* head);
struct student* creatsort();

int main()
{
	struct student* head;
	head = creatsort();
	print(head);
    
	return 0;
}
struct student* creatsort()
{
	struct student* head = NULL;
	struct student* pnew, * back, * front;
	int num;
	float score;
	printf("请输入成绩:\n");
	scanf_s("%d,%f", &num, &score);
	if (num < 0)
	{
		return head;
	}
	while (num >= 0)
	{
		pnew = (struct student*)malloc(sizeof(struct student));
		pnew->num = num;
		pnew->score = score;
                pnew->next = NULL;  //移到这里
		if (head == NULL)
		{
                        printf("1\n");
			head = pnew;
			head->next = NULL; //pnew->next = NULL;
		}
		else
		{
                        //back = front = head;
			if (head->score < score)
			{
				pnew->next = head;
				head = pnew;
			}
			else
			{
                                back  = head;
                                front = head->next;
				while (front != NULL && front->score > score)
				{
					back  = front;
					front = front->next;
				}
				if (front == NULL)//if (front == NULL && front->score > score)
				{
                                        printf("5\n");
					pnew->next = back->next;
					back->next = pnew;//front->next = pnew;
				}
				else
				{
					pnew->next = front; //pnew->next = front->next;
					back->next = pnew;  //front->next = pnew;
				}
			}
		}
		printf("新排名如下:\n");
		print(head);
		printf("请输入新成绩:\n");
		scanf_s("%d,%f", &num, &score);
	}
	return head;
}
void print(struct student* head)
{
	struct student* p;
	p = head;
	if (p == NULL)
	{
		printf("Empty!\n");
		return;
	}
	while (p != NULL)
	{
		printf("%d,%.2f\n", p->num, p->score);
		p = p->next;
	}
}

 

你试试输入4,,98呢

if (front == NULL && front->score > score)

 

{

 

pnew->next = back->next;

 

front->next = pnew;  //////////这里是不是应该back->next = pnew呢

 

}

上面第64行 printf("5\n"); 多余,删除了。