qq_39855168 2023-12-06 16:49 采纳率: 0%
浏览 1

简单选择排序代码错误求帮助

#报错 run: line 1: 3 Segmentation fault (core dumped) ./a.out

Exited with error status 139

#代码为

#include <stdio.h>
#include <stdlib.h>

typedef struct Node{
    int data;
    struct Node* next;
}Node;

Node* initList(){
    Node* list = (Node*)malloc(sizeof(Node));
    list -> data = 0;
    list -> next = NULL;
    return list;
}

void tailInsert(Node* list,int data){
    Node* head = list;
    Node* node = (Node*)malloc(sizeof(Node));
    node->data = data;
    node->next = NULL;
    list = list->next;
    while(list->next){
        list = list -> next;
    }
    list -> next = node;
    head -> data++;
}

void display(Node* list){
    list = list -> next;
    while(list){
        printf("%d ",list -> data);
        list = list->next;
    }
    printf("\n");
}

Node* findMin(Node* p){
    int value;
    Node* min = p;
    value = p->data;
    p = p->next;
    while(p){
        if(p->data<value){
            min = p;
            value = min->data;
        }
        p = p->next;
    }
    return min;
}

void simpleSort(Node* list){
    Node* q;
    Node* p;
    q=p=list->next;
    while(p){
        q = findMin(q);
        int temp = q->data;
        q->data = p->data;
        p->data = temp;
        p=p->next;
        q=p;
    }
}
int main()
{
    Node* list = initList();
    tailInsert(list,12);
    tailInsert(list,32);
    tailInsert(list,4);
    tailInsert(list,25);
    tailInsert(list,13);
    printf("排序前");
    display(list);
    simpleSort(list);
    printf("排序后");
    display(list);
}

  • 写回答

3条回答 默认 最新

  • ZhangChc丶 2023-12-06 17:07
    关注
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct Node {
        int data;
        struct Node* next;
    } Node;
    
    Node* initList() {
        Node* list = (Node*)malloc(sizeof(Node));
        list->next = NULL;
        return list;
    }
    
    void tailInsert(Node* list, int data) {
        Node* node = (Node*)malloc(sizeof(Node));
        node->data = data;
        node->next = NULL;
    
        Node* head = list;  // head指向头结点
        while (head->next) {
            head = head->next;
        }
    
        head->next = node;
    }
    
    void display(Node* list) {
        list = list->next;
        while (list) {
            printf("%d ", list->data);
            list = list->next;
        }
        printf("\n");
    }
    
    Node* findMin(Node* p) {
        int value;
        Node* min = p;
        value = p->data;
        p = p->next;
        while (p) {
            if (p->data < value) {
                min = p;
                value = min->data;
            }
            p = p->next;
        }
        return min;
    }
    
    void simpleSort(Node* list) {
        Node* q;
        Node* p;
        q = p = list->next;
        while (p) {
            q = findMin(q);
            int temp = q->data;
            q->data = p->data;
            p->data = temp;
            p = p->next;
            q = p;  // 修正此处,防止在下一次循环中使用无效的 q 指针
        }
    }
    
    int main() {
        Node* list = initList();
        tailInsert(list, 12);
        tailInsert(list, 32);
        tailInsert(list, 4);
        tailInsert(list, 25);
        tailInsert(list, 13);
        printf("排序前: ");
        display(list);
        simpleSort(list);
        printf("排序后: ");
        display(list);
    
        // 释放动态分配的内存
        Node* current = list->next;
        while (current) {
            Node* next = current->next;
            free(current);
            current = next;
        }
        free(list);
    
        return 0;
    }
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 12月6日

悬赏问题

  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统
  • ¥100 rtmpose姿态评估