问题排序链表字符串

时间:2016-11-10 19:51:25

标签: 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;
}

1 个答案:

答案 0 :(得分:0)

像这样解决:

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