このソート機能が動作しない理由を理解するのに苦労しています。ノードはソート順に配置されますが、最初のノードは常にプロセスで失われます。ここに私のコードは、(head
変数がグローバルである)である:私のソート機能に問題があります
void medianscore() {
int i, j=0;
int counter=0;
struct student *curr=head;
struct student *trail=head;
struct student *temp=NULL;
while (curr !=NULL)
{
curr=curr->next; //couting the number of items I have in my list.
counter++; //this works fine.
}
curr=head->next; // reseting the curr value for the 2nd position.
for (i=0; i<counter; i++)
{
while (curr != NULL)
{
if (trail->grade > curr->grade)
{
temp=curr->next; //bubble sort for the pointers.
curr->next=trail;
trail->next=temp;
temp=curr; //reseting trail and curr. curr gets back to be infront.
curr=trail;
trail=temp;
if (j==0) //i'm using j to determine the start of the loop so i won't loose the head pointer.
{
head=trail;
}
}
j++;
trail=curr;
curr=curr->next; //traversing thru the list. nested loop.
}
trail=head;
curr=trail->next;
curr->next=trail->next->next; //traversing thru the list. outer loop.
j=0;
}
}
私が間違って何をしているのですか?
どのノードが削除されているのが体系的ですか?たとえば、最初に最初に、または最初に最後に失われたノードが常に存在しますか? –
常に最初のノードです – pete