すでに整数で満たされた 2D 配列があり、行に分割して処理する準備ができており、各行 (1D 配列) をリンク リストのノードに渡す必要があります。各ノードは次のようになります。
struct node {
int *val;
struct node *next;
};
新しいノードが追加され、次のようにリンクされます。
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);
memcpy(tnode->val, val, sizeof(int) * columns);
tnode->val = val;
tnode->next = next;
};
return tnode;
}
各ノードを満たすプログラムのフラグメントは、おおよそ次のようになります。
int table[rows][columns], i, j;
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[j];
printf("%d ",head->val[j]);
};
puts("\n");
};
指定された場所での進め方がわかりません:
- これはノード全体の malloc ですが、malloc for はどうすればよい
val
ですか? 各ノードにある必要があるテーブルの長さを知っています。それcolumns
は、メイン関数で取得されます。メモリをどこに割り当てる必要がありますか? columns
この行の上は、1 行の整数に対して十分な ( ) メモリをmalloc する場所です。それは良い選択ですか?- これは、前のループを使用すると、現在
head->val[j]
の 2D 配列の適切な i 行を埋める必要table
がありますが、あまりにも良いように見えます。そのままにしていいですか?
編集:いくつかの場所で修正しましたが、並べ替えを試みた後、ゴミが返されます。ここにほとんどのコードをダンプします。
#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 columns = atoi(argv[1]); //until sorting works, I'll keep it at 0
int rows = 0;
head = NULL;
int column = 0; //temporary until I find the way to send one argument during executing it under linux like so 'name_of_program columns < test.txt'
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); //I have heard conflicting opinions on that, but in this case it works, and in the end it's a school project, not commercial code
int table[rows][columns], i, j;
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");
};
head = mergesort(head, column);
for(current = head; current != NULL; current = current->next){
for (j = 0; j < columns; j++){
printf("%d ", current->val[j]);
};
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);
memcpy(tnode->val, val, sizeof(int) * columns);
tnode->val = val;
tnode->next = next;
};
return tnode;
}
struct node *mergesort(struct node *head, int column)
{
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)
{
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;
}
次のように Unix で実行します。
name_of_program < test.txt
この構造を持つtest.txtを使用http://pastebin.com/WL5brutf