navigate 2D doubly linked lists

Hi i would like to know how i can navigate through a 2D doubly linked list.

i have created a linked list of a linked list which are contained in classes Row and Columna

Row
list <char> X

Column
list <Row> Y;

List X contains
a b c d e f g h i j k

Column Contains
rows of list x

output at the end is as follows. Each row may contain varied length.

1 a b c d e f g h i j
2 k l m n o p q r s t a b c d e
3 u v w k y z
4 a b c d e f g h i

i would like to know how to navigate somewhere like 2, P, and do some tests from that point.

Could someone shed some light on this, to get me in the right direction.
Why don't you use vectors? It will be easier for you since you can use the [] operator. Declare it like:
vector <vector<char> > my_2D_vector;

Then add data to it and use it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
my_2D_vector.resize(4); //since you want 4 rows

//now fill first row with data
my_2D_vector[0].push_back('1'); //I'm not sure if you want '1' to actually be part of the data
my_2D_vector[0].push_back('a');
my_2D_vector[0].push_back('b');
//etc...

//then fill second row with data
my_2D_vector[1].push_back('2'); //same here for '2'. If you don't want it just ignore this line
my_2D_vector[1].push_back('k');
my_2D_vector[1].push_back('l');
//etc...

//...
//and after you fill it you can use it like:
int i,j;
cout << "enter i j: ";
cin >> i >> j;
cout << "element in row i, column j is: ";
cout << my_2D_vector[i][j] << endl;


EDIT:
If the row length is fixed you can simply use an array of vectors. If the length is known at compilation time you can do it like:
1
2
const int length=10; //or anything you like
vector<char> my_static_varray[length];

And if the length is entered by the user during runtime you can do it like this:
1
2
3
4
5
6
7
8
9
int length;
cout << "enter length: ";
cin >> length;
vector<char> * my_dynamic_varray=new vector<char>[length];
//...
//use it here any way you want
//...
//and delete it when you 're done with it
delete[] my_dynamic_varray;
Last edited on
Topic archived. No new replies allowed.