首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

单链表冒泡排序,请看看小弟我的有关问题何在

2012-12-19 
单链表冒泡排序,请看看我的问题何在?void swap(struct student *head, int lIndex, int rIndex){struct st

单链表冒泡排序,请看看我的问题何在?

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;


    while(p!=NULL){
        if(strcmp(p->no, no)==0){
            break;
        }
        p=p->next;
        ++count;
    }
    return count;
}
 
void removeAtIndex(int index, struct student *head){
    struct student *p = head;
    for(int i = 0; i < index-1; i++){
        p = p->next;
    }
    p->next = p->next->next;
}
 
struct student *get(int index, struct student *head){
    struct student *p = head;
    // return p+index;
    while (index--)
        p = p->next;
    return p;
}
 
int menu(void){
    printf("\t\t\t*********************************\n");
    printf("\t\t\t*            1.Input            *\n");
    printf("\t\t\t*            2.Output           *\n");
    printf("\t\t\t*            3.Sort             *\n");
    printf("\t\t\t*            4.Insert           *\n");
    printf("\t\t\t*            5.Delete           *\n");
    printf("\t\t\t*            6.Modify           *\n");
    printf("\t\t\t*            7.Quit             *\n");
    printf("\t\t\t*********************************\n");
    int choice;
    scanf("%d",&choice);
    return choice;
}
 
int size(struct student *head){
    struct student *p = head;
    int count = 0;
    while(p!=NULL){
        ++count;
        p = p->next;
    }
    return count;
}
 
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){
            if(get(j, head)->score-get(j+1, head)->score>0){
                swap(head, j, j+1);
            }
        }
    }
}
 
int main(void)
{
    struct student *head;
    int choice;
    while(1){
        switch(choice=menu()){
        case 1:
            head = create();
            break;
        case 2:
            output(head);
            break;
        case 3:
            sort(head);
            break;
        case 4:
            break;
        case 5:
            break;
        case 6:
            break;
        case 7:
            return 0;
            break;
        default:
            printf("invilid input!\n");
            break;
        }
         
    }
    return 0;
}


[其他解释]
这个调试的问题 难道要远程?
[其他解释]
你用什么调试的
[其他解释]
看来人帮我看看啊,自己搞不定了啊。
------其他解决方案--------------------


等的我菊花都快谢了。还是先换个思路把,我去试试交换指针
[其他解释]
再顶一记。先去干干工作,一会回来看。不要让LZ失望啊
[其他解释]

引用:
这个调试的问题 难道要远程?

#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(){
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);
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);
++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;
while(p!=NULL){
if(strcmp(p->no, no)==0){
break;
}
p=p->next;
++count;
}
return count;
}

void removeAtIndex(int index, struct student *head){
struct student *p = head;
for(int i = 0; i < index-1; i++){
p = p->next;
}
p->next = p->next->next;
}

struct student *get(int index, struct student *head){
struct student *p = head;
return p+index;
}

int menu(){
printf("\t\t\t*********************************\n");
printf("\t\t\t*            1.Input            *\n");
printf("\t\t\t*            2.Output           *\n");
printf("\t\t\t*            3.Sort             *\n");
printf("\t\t\t*            4.Insert           *\n");
printf("\t\t\t*            5.Delete           *\n");
printf("\t\t\t*            6.Modify           *\n");
printf("\t\t\t*            7.Quit             *\n");
printf("\t\t\t*********************************\n");
int choice;
scanf("%d",&choice);
return choice;
}

int size(struct student *head){


struct student *p = head;
int count = 0;
while(p!=NULL){
++count;
p = p->next;
}
return count;
}

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);
}
}
}
}

void main()
{
struct student *head;
int choice;
while(true){
switch(choice=menu()){
case 1:
head = create();
break;
case 2:
output(head);
break;
case 3:
sort(head);
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
return;
break;
default:
printf("invilid input!\n");
break;
}

}
}


完整代码。本来不想贴出来丢人的
[其他解释]
引用:
你用什么调试的

vs2010
[其他解释]
楼主用的是链表, get和sort却用的是数组方式啊。
[其他解释]
引用:
楼主用的是链表, get和sort却用的是数组方式啊。

没思路啊,求大神提供思路。sort倒是有个思路,就是更改节点的位置,但是比这个复杂,涉及到表头和表尾,我就先这么写了。
[其他解释]
代码如上,单独测试swap功能可用。
调试时为什么(head+j)->score和(head+j+1)->score都是-4.3160208e+008,而输出又是正常?
为什么struct student有name成员,调试时看不见?一头雾水啊


scanf("%s%s%f", &p1->name, &p1->no,&p1->score); 不能区分输入的字串是p1->name or p1->no的,得该
[其他解释]
总的来说楼主的程序思路还是比较清晰的!
[其他解释]
引用:
总的来说楼主的程序思路还是比较清晰的!


多谢多谢。我知道问题所在了,链表的内存空间不是连续的,所以数组方式就错了。昨天就犯过一次,今天又忘了
[其他解释]
(head+j)
(head+j)->score
要放到watch窗口察看。
[其他解释]
引用:
重点是sort和get函数里的改动
C/C++ code?1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283……

版主大人,小的还有问题要问。
// scanf("%s%s%f", &p1->name, &p1->no,&p1->score); 


        scanf("%s%s%f", p1->name, p1->no,&p1->score);


为什么这两句效果一样啊?用&p1->name为什么输入内容还是保存到了name里面呢
[其他解释]
scanf("%s%s%f", &p1->name, &p1->no,&p1->score);
scanf("%s%s%f", p1->name, p1->no,&p1->score);
第二个的才正确,%s 需要 char *参数

p1->name 类型是 pointer to char
&p1->name 类型是 pointer to array of 20 chars
当然在数值上两者是相同的。


原因在scanf的说明里有
     s     Matches a sequence of non-white-space characters; the next pointer
           must be a pointer to char, and the array must be large enough to
           accept all the sequence and the terminating NUL character.  The
           input string stops at white space or at the maximum field width,
           whichever occurs first.

热点排行