1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
|
#include <stdlib.h>
#include <stdio.h>
struct cell {
int value;
struct cell* next;
};
struct line {
struct cell* first; //Verweis auf erstes Element, die folgenden dann über cell->next
struct line* nextLine; //Verweis auf nächste Zeile
};
struct matrix {
struct line* firstLine; //folgende Zeilen dann über die erste Zeile
};
struct line* createLine(int columns, int val) {
struct line* l = malloc(sizeof(struct line));
struct cell* currentCell = malloc(sizeof(struct cell));
currentCell->value = val;
l->first = currentCell;
for(int i = 1; i < columns; i++) {
struct cell* c = malloc(sizeof(struct cell));
c->value = currentCell->value + 1;
currentCell->next = c;
currentCell = c;
}
return l;
}
struct matrix* grid_init(int num_columns, int num_rows) {
struct matrix* mat = malloc(sizeof(struct matrix));
struct line* currentLine = createLine(num_columns, 0);
mat->firstLine = currentLine;
for (int i = 1; i < num_rows; i++) {
struct line* l = createLine(num_columns, i*num_columns);
currentLine->nextLine = l;
currentLine = l;
}
return mat;
}
void printLine(struct line* l) {
struct cell* c = l->first;
while (c) {
printf("%d\t", c->value);
c = c->next;
}
printf("\n");
}
void printMatrix(struct matrix* mat) {
struct line* l = mat->firstLine;
while (l) {
printLine(l);
l = l->nextLine;
}
}
int main() {
struct matrix* mat = grid_init(4,3);
printMatrix(mat);
} |