我有一个问题与代码在C。我试图打开一个txt文件,有电影名称,年,演员。。。
我把电影放在一个链表里问题是在订购列表中的电影时。我尝试使用冒泡排序方法,但是由于列表太大,当在Dev中运行时,程序无限地运行函数,而不执行主要的其他操作(由于排序效率低下)。
有人能给我一个提示或帮助,让我申请代码,并设法排序列表吗?
代码如下:

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

#define N 10000

typedef struct Lista {
    char data[N];
    struct Lista *next;
} Filmes;

typedef struct ListaDupla {
    char pessoa[N];
    struct ListaDupla *prox;
    struct ListaDupla *ant;
} DuplaLista;

struct Lista *Insert(struct Lista *head, char data[N]) {
    char aux3[N];
    struct Lista *tmp = ((struct Lista *)malloc(sizeof(struct Lista)));
    int aux5;
    strcpy(tmp->data, data);
    tmp->next = NULL;
    if (head == NULL) {
        head = tmp;
        return head;
    } else {
        struct Lista *aux = head;
        struct Lista *aux2 = head;
        while (aux->next != NULL) {
            aux = aux->next;
        }

        aux->next = tmp;

        while (aux != NULL) {
            aux2 = aux2->next;
            while (aux2 != NULL) {
                aux5 = strcmp(aux->data, aux2->data);
                if (aux5 > 0) {
                    strcpy(aux3, aux->data);
                    strcpy(aux->data, aux2->data);
                    strcpy(aux2->data, aux3);
                }
            }
            aux = aux->next;
        }
        return head;
    }

    // Complete this method
}

int main() {
    struct Lista *filmes = ((struct Lista *)malloc(sizeof(struct Lista)));
    int opcao;
    char aux2[N];
    FILE *arq;
    arq = fopen("nomes.txt", "rt");
    int i, a = 0, b, aux;
    char linha[600], nome[100];

    if (arq == NULL) {
        printf("Ocorreu um erro!");
        return 1;
    }
    while (fgets(linha, 700, arq)) {
        char *p = strtok(linha, ",");
        filmes = Insert(filmes, p);
        while (filmes->next != NULL) {
            printf(" \n Nome:%s", filmes->data);
            filmes = filmes->next;
        }
    }
    fclose(arq);
}

最佳答案

代码中有许多问题:
列表项应该有一个指向已分配字符串的指针,而不是一个大大小(char字节)的10000数组.
您应该使用插入排序将新节点插入正确的点,而不是在尝试气泡排序失败时修改列表元素。
您在main()开始时与struct Lista *filmes = ((struct Lista *)malloc(sizeof(struct Lista)));一起分配一个初始项,没有任何用途。您应该使用struct Lista *filmes = NULL;将列表初始化为空
linha将行读入fgets(linha, 700, arq),但linha的大小只有600字节。使用fgets(linha, sizeof linha, arq)来避免这样的不一致。
您可以插入一个带有filmes = Insert(filmes, p);的新条目,这很好,但是您可以使用相同的指针在下面的循环中遍历列表。因此filmes将指向循环末尾的最后一项,下一个元素将不会插入到下一行的头部列表中。您应该使用不同的指针在列表上迭代。
以下是修改版本:

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

typedef struct Lista {
    char *data;
    struct Lista *next;
} Lista;

struct Lista *Insert(struct Lista *head, const char *data) {
    struct Lista *newp;
    struct Lista *tmp;
    char *new_data;

    /* allocate a new list item */
    newp = malloc(sizeof(struct Lista));
    new_data = strdup(data);
    if (newp == NULL || new_data == NULL) {
        fprintf(stderr, "out of memory");
        return NULL;
    }
    newp->data = new_data;
    newp->next = NULL;

    /* check if element should be inserted at the head */
    if (head == NULL || strcmp(new_data, head->data) < 0) {
        newp->next = head;
        head = newp;
    } else {
        /* otherwise find the point of insertion */
        tmp = head;
        while (tmp->next && strcmp(new_data, tmp->next->data) >= 0) {
            tmp = tmp->next;
        }
        newp->next = tmp->next;
        tmp->next = newp;
    }
    return head;
}

int main() {
    struct Lista *filmes;
    struct Lista *film;
    FILE *arq;
    char linha[600];

    /* open the file */
    arq = fopen("nomes.txt", "r");
    if (arq == NULL) {
        printf("Ocorreu um erro!");
        return 1;
    }

    /* insert the items */
    filmes = NULL;
    while (fgets(linha, sizeof linha, arq)) {
        char *p = strtok(linha, ",");
        filmes = Insert(filmes, p);
    }
    fclose(arq);

    /* print the sorted list */
    for (film = filmes; film != NULL; film = film->next) {
        printf("Nome: %s\n", film->data);
    }

    /* free the list */
    while (filmes != NULL) {
        struct Lista *next = filmes->next;
        free(filmes->data);
        free(filmes);
        filmes = next;
    }
    return 0;
}

注意,可以修改插入功能,以避免在头部插入特殊外壳:
struct Lista *Insert(struct Lista *head, const char *data) {
    struct Lista *newp;
    struct Lista **linkp;
    char *new_data;

    /* allocate a new list item */
    newp = malloc(sizeof(struct Lista));
    new_data = strdup(data);
    if (newp == NULL || new_data == NULL) {
        fprintf(stderr, "out of memory");
        return NULL;
    }
    newp->data = new_data;
    newp->next = NULL;

    /* use a double pointer to locate the point of insertion in a single pass */
    linkp = &head;
    while (*linkp && strcmp(new_data, (*linkp)->data) >= 0) {
        linkp = &(*linkp)->next;
    }
    newp->next = *linkp;
    *linkp = newp;
    return head;
}

关于c - 链表的无限气泡排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50538670/

10-11 22:59
查看更多