I'm trying to get my program to insert a score into a place within an ordered linked list. It compiles fine but crashes when I launch it. Linked lists aren't exactly my speciality so I'm sure I've made an error somewhere. p[i].score and p[i].name are defined just before this piece of code and the list is initially empty.
struct highScore
{
char name[10];
int score;
struct highScore *next;
};
struct highScore *head,*currentScore,*newScore, *temp;
if(head==NULL)
{
head = currentScore = newScore;
currentScore->score = p[i].score;
strcpy(currentScore->name, p[i].name);
}
else
{
currentScore = head;
while (currentScore->next != NULL)
{
while(p[i].score < currentScore->score)
{
currentScore = currentScore->next;
if (currentScore->next->score < p[i].score)
{
newScore = (struct highScore *)malloc(sizeof(struct highScore));
newScore->score = p[i].score;
strcpy(newScore->name, p[i].name);
temp = currentScore->next;
currentScore->next = newScore;
currentScore = newScore;
currentScore->next = temp;
}
}
}
}
struct highScoredefinition?headinitialized somewhere, before it is compared toNULL? Also, how isp[i].namepopulated?