gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:54:24 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com