热门标签 | 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.

推荐阅读
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 题目Link题目学习link1题目学习link2题目学习link3%%%受益匪浅!-----&# ... [详细]
  • 本文详细探讨了VxWorks操作系统中双向链表和环形缓冲区的实现原理及使用方法,通过具体示例代码加深理解。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细探讨了KMP算法中next数组的构建及其应用,重点分析了未改良和改良后的next数组在字符串匹配中的作用。通过具体实例和代码实现,帮助读者更好地理解KMP算法的核心原理。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本实验主要探讨了二叉排序树(BST)的基本操作,包括创建、查找和删除节点。通过具体实例和代码实现,详细介绍了如何使用递归和非递归方法进行关键字查找,并展示了删除特定节点后的树结构变化。 ... [详细]
  • 本文详细介绍了C语言中链表的两种动态创建方法——头插法和尾插法,包括具体的实现代码和运行示例。通过这些内容,读者可以更好地理解和掌握链表的基本操作。 ... [详细]
  • Codeforces Round #566 (Div. 2) A~F个人题解
    Dashboard-CodeforcesRound#566(Div.2)-CodeforcesA.FillingShapes题意:给你一个的表格,你 ... [详细]
  • 本题通过将每个矩形视为一个节点,根据其相对位置构建拓扑图,并利用深度优先搜索(DFS)或状态压缩动态规划(DP)求解最小涂色次数。本文详细解析了该问题的建模思路与算法实现。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
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社区 版权所有