C - 我在链表上的mergesort有什么问题?

时间:2013-06-05 17:22:38

标签: c merge printf mergesort

我到了我的程序编译点(通过vim,如果这很重要),但返回垃圾。这是代码:

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

struct node {
    int *val;
    struct node *next;
};

struct node *addnode(int *val, struct node *next, int columns);
struct node *mergesort(struct node *head, int column);
struct node *merge(struct node *head_one, struct node *head_two, int column);

int main(int argc, char *argv[])
{

    struct node *head;
    struct node *current;
    struct node *next;
    int symbol = 0;
    int columns = 0;
    int rows = 0;
    head = NULL;
    int column = atoi(argv[1])-1;
    int lastSpace = 0;

    do {
        symbol = fgetc(stdin);

        if (rows == 0 && (lastSpace == 0 && (isspace(symbol) || feof(stdin)))) {
            columns++;
            lastSpace = 1;
        } else if (!isspace(symbol)) {
            lastSpace = 0;
        }
        if (symbol == '\n' || feof(stdin)) {
            rows++;
        };
    } while (symbol != EOF);

    if (ferror(stdin)) {
        printf("Error on reading from file.\n");
    } else {
        printf("The file contains %d row(s) and %d column(s).\n", rows, columns);
    };

    rewind(stdin); //ignore it - it works in this case

    int table[rows][columns], i, j;
    puts("Original file:\n");
    for (i = 0; i < rows; i++) {
        head = addnode(*table, head, columns);
        for (j = 0; j < columns; j++) {
            scanf("%d",&table[i][j]);
            head->val[j] = table[i][j];
            printf("%d ",head->val[j]);
        };
        puts("\n");
    };

    /* Up to this point everything works */
    head = mergesort(head, column);

    printf("Sorted by %d column:\n", column+1);
    for(current = head; current != NULL; current = current->next) {
        for (j = 0; j < columns; j++) {
            printf("%d ", current->val[j]); //suspect number 1 - printing loop prints only last node, instead node-by-node
        };//I removed a }; in this area
        puts("\n");
    };

    for(current = head; current != NULL; current = next)
        next = current->next, free(current);
    return 0;
};


struct node *addnode(int *val, struct node *next, int columns)
{
    struct node *tnode;
    tnode = (struct node*)malloc(sizeof(*tnode));
    if(tnode != NULL) {
        tnode->val = malloc(sizeof(int) * columns); //suspect number 2 - I'm not sure I allocated memory correctly, it should allocate array of integers
        memcpy(tnode->val, val, sizeof(int) * columns);
        tnode->val = val;
        tnode->next = next;
    };
    return tnode;
}

struct node *mergesort(struct node *head, int column) //suspect number 3 - my modified mergesort duplicates last node
{
    struct node *head_one;
    struct node *head_two;
    if((head == NULL) || (head->next == NULL))
        return head;
    head_one = head;
    head_two = head->next;
    while((head_two != NULL) && (head_two->next != NULL)) {
        head = head->next;
        head_two = head->next->next;
    };
    head_two = head->next;
    head->next = NULL;
    return merge(mergesort(head_one, column), mergesort(head_two, column), column);
}

struct node *merge(struct node *head_one, struct node *head_two, int column) //suspect number 4 - instead of merging, it duplicates nodes
{
    struct node *head_combined;
    if(head_one == NULL)
        return head_two;
    if(head_two == NULL)
        return head_one;
    if(head_one->val[column] < head_two->val[column]) {
        head_combined = head_one;
        head_combined->next = merge(head_one->next, head_two, column);
    } else {
        head_combined = head_two;
        head_combined->next = merge(head_one, head_two->next, column);
    };
    return head_combined;
}

我从命令行运行它,如下所示:
    name_of_program列&lt;的test.txt

test.txt看起来像这样

1   4  100  34   12
12  2  45   6     7
5   8   1   87  101
5   4  10   12   33

而不是按column列排序的列表,我从test.txt的最后一个节点获取数组4次。我不知道如何在vim中逐步分析它,我认为这有助于我找出问题所在。

1 个答案:

答案 0 :(得分:0)

我不明白为什么要将table传递给addNode。你调用它的方式,你想要添加到你正在添加的节点的行还没有从文件中读取。

这两条线需要走,但你打算用第二条线做什么? 到那时,您已经为tnode->val分配了空间,并将数据从val复制到了它。

memcpy(tnode->val, val, sizeof(int) * columns);
tnode->val = val;   /// What's this for ???

我想这个

head_two = head->next->next;

应该是

head_two = head_two->next->next;

好像你正试图在这里找到中点,所以head_two需要走的速度是head的两倍。