AustinPavlas AustinPavlas - 24 days ago 7
C Question

Problems Sorting A linked list string

I am a newbie to C. I have been working on this project for the past week, and have finished. The last part that I can not get to work, in the sorting of the file, by arrival time. I have all of the code I think that I need, but it crashes after printing only the first flight. Any help anyone can provide would be much appreciated. Below is my code and a sample of that file I am using.

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


Code:

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

Answer

fix like this:

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