热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

voidswap(structstudent*head,intlIndex,intrIndex){structstudent*lNodeget(lIndex,head);
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成员,调试时看不见?一头雾水啊

16 个解决方案

#1


看来人帮我看看啊,自己搞不定了啊。

#2


等的我菊花都快谢了。还是先换个思路把,我去试试交换指针

#3


再顶一记。先去干干工作,一会回来看。不要让LZ失望啊

#4


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

#5


引用 4 楼 zhuankeshumo 的回复:
这个调试的问题 难道要远程?

#include 
#include 
#include 

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

}
}

完整代码。本来不想贴出来丢人的

#6


你用什么调试的

#7


引用 6 楼 buyong 的回复:
你用什么调试的

vs2010

#8


楼主用的是链表, get和sort却用的是数组方式啊。

#9


引用 8 楼 mymtom 的回复:
楼主用的是链表, get和sort却用的是数组方式啊。

没思路啊,求大神提供思路。sort倒是有个思路,就是更改节点的位置,但是比这个复杂,涉及到表头和表尾,我就先这么写了。

#10


代码如上,单独测试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的,得该

#11


重点是sort和get函数里的改动

#include 
#include 
#include 
 
#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;
}

#12


总的来说楼主的程序思路还是比较清晰的!

#13


引用 12 楼 mymtom 的回复:
总的来说楼主的程序思路还是比较清晰的!


多谢多谢。我知道问题所在了,链表的内存空间不是连续的,所以数组方式就错了。昨天就犯过一次,今天又忘了

#14


(head+j)
(head+j)->score
要放到watch窗口察看。

#15


引用 11 楼 mymtom 的回复:
重点是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里面呢

#16


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.

推荐阅读
author-avatar
oooooo1995_395
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有