2

I need to create an array of linked lists at runtime for a hash table assignment. The sample code that we are given only allows for creating arrays of static size, and I cannot figure out how to modify it to work with a variable, as everything I have tried has resulted in errors.

Sample Code:

typedef std::list<int> INT_LIST;
typedef INT_LIST* INT_LIST_POINTER;
int size = 13;
INT_LIST_POINTER myArray[size];
INT_LIST_POINTER tmpPtr;
// initialize the array to point to empty lists
for (int i=0; i<size; i++){
tmpPtr = new INT_LIST;
myArray[i] = tmpPtr;
}

My Current, nonworking Code:

typedef std::list<int> INT_LIST;
typedef INT_LIST* INT_LIST_POINTER;
INT_LIST_POINTER myArray = new INT_LIST[p];
INT_LIST_POINTER tmpPtr;
for (int i=0; i<n; i++){
INT_LIST* temp = new INT_LIST;
myArray[i] = temp;
}

The major problem seems to be with

myArray[i] =  temp;

which says nothing matches those operands.

3
  • check out std::vector.resize and std::vector.push_back methods. Commented Feb 29, 2012 at 9:25
  • If you want to wind up your teacher, point out that the sample code is not valid C++. It should be const int size = 13;. To demonstrate this, compile using for example GCC with the -pedantic-errors option to disable non-standard language extensions. Commented Feb 29, 2012 at 9:42
  • All uppercase identifiers are - by convention - used to denote template parameters when short (e.g. T, U, T2), otherwise preprocessor macros. They should not be used for types. A small matter, but enough to make me think whoever's given you your sample code doesn't have a clue. Further, why have an array of pointers to std::list when you can just have an array of std::list? It's less efficient and less maintainable (note - the deallocation code - another loop for delete - is missing from the sample). Commented Feb 29, 2012 at 10:04

2 Answers 2

2

If this is C++, why not use std:

std::vector<std::list<MyClass> > x;
Sign up to request clarification or add additional context in comments.

3 Comments

This is obviously a good answer but the OP tagged the question "homework", so I wouldn't be surprised if he can't use anything inside std::.
Forgot to include these two lines typedef std::list<int> INT_LIST; typedef INT_LIST* INT_LIST_POINTER;
@ereOn he uses std::list so std::vector<INT_LIST_POINTER> myArray; should be OK too.
2

You allocate an array of size p:

INT_LIST_POINTER myArray = new INT_LIST[p];

and then proceed to initialize n elements:

for (int i=0; i<n; i++){

Unless p and n are the same thing, this doesn't look right.

P.S. Don't forget to delete[] myArray when you're done with it.

2 Comments

more importantly this allocates a fixed sized array yet again.
You are also correct, I have changed the wording in the original post, I do not need it to be fully dynamic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.