0

プログラムがコンパイルされるポイントに到達しましたが(問題がある場合は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 column < 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で段階的に分析する方法がわかりません。問題を特定するのに役立つと思います。

4

1 に答える 1