我到了我的程序编译的地步(通过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 中逐步分析它,我认为这将有助于我查明问题。