单链表冒泡排序,请看看小弟我的有关问题何在
单链表冒泡排序,请看看我的问题何在?
代码如上,单独测试swap功能可用。
调试时为什么(head+j)->score和(head+j+1)->score都是-4.3160208e+008,而输出又是正常?
为什么struct student有name成员,调试时看不见?一头雾水啊
------最佳解决方案--------------------
重点是sort和get函数里的改动
void swap(struct student *head, int lIndex, int rIndex){
struct student *lNode = get(lIndex, head);
struct student *rNode = get(rIndex, head);
struct student *tmp = (struct student *)malloc(SIZE);
strcpy(tmp->name, lNode->name);
strcpy(tmp->no, lNode->no);
tmp->score = lNode->score;
strcpy(lNode->name, rNode->name);
strcpy(lNode->no, rNode->no);
lNode->score = rNode->score;
strcpy(rNode->name, tmp->name);
strcpy(rNode->no, tmp->no);
rNode->score = tmp->score;
}
void sort(struct student *head){
for(int i = 0; i < size(head); i++){
for(int j = 0; j < size(head) - i - 1; j++){
if((head+j)->score-(head+j+1)->score>0){
swap(head, j, j+1);
}
}
}
}
代码如上,单独测试swap功能可用。
调试时为什么(head+j)->score和(head+j+1)->score都是-4.3160208e+008,而输出又是正常?
为什么struct student有name成员,调试时看不见?一头雾水啊
------最佳解决方案--------------------
重点是sort和get函数里的改动
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE sizeof(struct student)
struct student
{
char name[20];
char no[8];
float score;
struct student *next;
};
struct student *create(void){
struct student *p1,*p2,*head;
int count = 0;
p1 = p2 = head = (struct student *)malloc(SIZE);
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);
while(strcmp(p1->no, "0")!=0){
if(count!=0){
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(SIZE);
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);
++count;
}
p2->next = NULL;
return count == 0?NULL:head;
}
void output(struct student *head){
struct student *p = head;
while(p!=NULL){
printf("%s %s:%3.1f\n", p->name, p->no, p->score);
p=p->next;
}
}
int search(char *no, struct student *head){
struct student *p = head;
int count = 0;