PDA

View Full Version : Array of linked lists



therealjag
7th March 2006, 19:19
hey there i have a linked list which is made up of integers...i would just like to know if there is a way to create an array of linked lists?? ive never tried it before so i dont really know the syntax of it...any help would be much appreciated thanks. Jag

high_flyer
7th March 2006, 20:35
Sure its possible.
Could you post the defintion code of your list?

dublet
8th March 2006, 11:12
Are you using a vector<int> for your linked list?

If so, if you want an array:


vector<int> array[] = {
vector<int> a,
vector<int> b
};

// Insert into a.
array[0].push_back(1);

therealjag
8th March 2006, 18:12
ok thanks for the advice - i was meant to post up my linked list but i used vectors to do it so i guess that way would work. cheers

therealjag
9th March 2006, 18:11
hey there i tried the code that was posted above for the array of linked lists but i keep gettin compilation errors from it. the error message says forbids declaration of no type and expected ; before < token. any idea's??

jacek
9th March 2006, 18:20
Add "std::" in front of "vector" and next time, please, post the exact error message.

therealjag
9th March 2006, 18:53
Ok i tried that too but it still doesnt work.im creating the vector in my header file underneath private: is that right? heres my new error message:

jacek
9th March 2006, 19:11
Could you post the code?

therealjag
9th March 2006, 19:13
private:

std::vector<int> array[] = {
vector<int> a,
vector<int> b
};

jacek
9th March 2006, 19:36
You can't initialize member variables like this.

Try:
typedef std::vector<int> Vector;

class Test
{
// ...
private:
static const int SIZE = 2;
Vector array[ SIZE ];
};