c - 对链表字符串进行排序的问题

标签 c

我是 C 的新手。过去一周我一直在从事这个项目,并且已经完成。我无法开始工作的最后一部分是按到达时间对文件进行排序。我拥有我认为需要的所有代码,但仅打印第一个航类后它就崩溃了。任何人可以提供的任何帮助将不胜感激。下面是我的代码和我正在使用的该文件的示例。

AA 2415 2015 2135
AM 0045 1500 1615
DT 0123 1230 1320
FR 1440 1000 1100
SW 0013 0800 0905
JB 3626 0721 0830

代码:

struct flight
{
    char arr[4];
    char *string;
    struct flight *next;
};
typedef struct flight LIST;

void bubbleSort(struct flight *start);
void swap(struct flight *a, struct flight *b);

int main(int argc, char *argv[])
{
    struct flight arrival;
    struct flight *start;
    FILE *fp;
    char buffer[20];
    LIST *current, *head, *node;
    head = current = NULL;
     //fp=fopen("argv[1]", "r");
    while(fgets(buffer,20,fp))
    {
        node = (struct flight *)malloc(sizeof(struct flight));
        memcpy(arrival.arr, &buffer[8], 12);
        arrival.arr[4]='\0';
        printf("%s\n", arrival.arr);
        node->string = strdup(buffer);
        node->next =NULL;
        if(head == NULL)
        {
            current = head = node;
        }
        else
        {
            current = current->next = node;
        }
        bubbleSort(start);
    }
    fclose(fp);
    for(current = head; current ; current=current->next)
    {
        printf("%s", current->string);
    }
}

void bubbleSort(struct flight *start)
{
    int swapped, i;
    struct flight *ptr1;
    struct flight *lptr = NULL;
    do
    {
        swapped = 0;
        ptr1 = start;
        while (ptr1->next != lptr)
        {
            if (ptr1->arr > ptr1->next->string)
            {
                swap(ptr1, ptr1->next);
                swapped = 1;
            }
            ptr1 = ptr1->next;
        }
        lptr = ptr1;
    }
    while (swapped);
}

void swap(struct flight *a, struct flight *b);
{
    int temp = a->string;
    a->string = b->string;
    b->string = temp;
}

最佳答案

像这样修复:

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

#define OFFSET 8 //arrival time

typedef struct flight {
    //char arr[5];//4+1(NUL)//unnecessary or must be synchronized to string of member.
    char *string;
    struct flight *next;
} LIST;

void bubbleSort(LIST *start);
void swap(LIST *a, LIST *b);

int main(int argc, char *argv[]){
    FILE *fp;
    char buffer[20];
    LIST *current, *head, *node;

    head = current = NULL;
    fp=fopen(argv[1], "r");//argc > 1.
    while(fgets(buffer, sizeof buffer, fp)){
        node = malloc(sizeof(*node));//cast is not required in C.
        node->string = strdup(buffer);
        node->next = NULL;
        if(head == NULL){
            current = head = node;
        } else {
            current = current->next = node;
        }
        bubbleSort(head);//head instead of start
    }
    fclose(fp);
    for(current = head; current ; current = current->next){
        printf("%s", current->string);//Include a newline in the (all)line
    }
}

void bubbleSort(LIST *start){
    int swapped;
    LIST *lptr = NULL;//last

    do {
        LIST *ptr = start;
        swapped = 0;
        while (ptr->next != lptr){
            if(strncmp(ptr->string + OFFSET,  ptr->next->string + OFFSET, 4) > 0) {// use strncmp
                swap(ptr, ptr->next);
                swapped = 1;
            }
            ptr = ptr->next;
        }
        lptr = ptr;
    } while (swapped);
}

void swap(LIST *a, LIST *b){//remove ;
    char *temp = a->string;//type is char*
    a->string = b->string;
    b->string = temp;
}

关于c - 对链表字符串进行排序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40535878/

相关文章:

c - 如何将二维数组的每个元素递增 2

CGI 缓冲问题

c++ - 在 C 或 C++ 中以编程方式删除非空目录

c - c 程序中的双重释放或损坏 (!prev) 错误

c - 如何从 Fortran 访问 C 指针?

c - ARM交叉编译ZeroMQ zstr_rcv()给出段错误

c - 变量类型结构体是否必须是数组才能存储 100 个元素?

c - 基于字符将字符串分解为数组的有效方法,例如 C 中的%?

c - 如何在 C 中使用此代码进入 while 循环

python - 在 Python ctypes 中为结构实现 offsetof()