Here is the matrix I want to represent in the link-list form
The idea is that it's a 2-D matrix. The font in red is the regular [i][j], and blue is the extra information I want to store in a link-list.
In this matrix, I need to have several informations to be stored.
- int row
- int colm
- int label [as shown in blue]
- bool value (the value to be displayed on the screen)
- *** right
- *** left
- *** up
- *** down
the problem is i am going to get 4 link-lists, if I create 4 pointers of array [for 2-D matrix]? how do I even get the directional poin开发者_高级运维ters???
If you are curious, I am working on solving a Karnaugh Map. link text
Any help is appreciated. Thanks!
Don't use a linked list, use a 2-d array. Quick & dirty (and incomplete):
struct Matrix {
struct Entry {
int label;
bool value;
};
Entry *array;
size_t rowlen;
Matrix(int n) : rowlen(n) { array = new Entry[n*n]; }
~Matrix() { delete[] array; }
Entry &at(int i, int j) { return array[i * rowlen + j]; }
// etc.
};
No need for directional pointers, just use some smart addition and subtraction logic.
精彩评论