Hi, I'm trying to do a hashing project using separate chaining. In order to do the separate chaining I believe I need to make a dynamic array of pointers to the linked lists. This is due to the fact the user can specify the hash table size at runtime. In VS2010 when I try to set the HEAD pointer values to NULL it won't compile and returns "no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)".

My code is super simple so I'm not sure what I am doing wrong. Any help would be great. Thx, Glenn.

struct list 
{
   list *next;
   list *prev;
   int numValue;
};

void separateChaining(int hashTable[], int hashTableSize)
{
	list * listArray;
	listArray = new list[hashTableSize];
	if(listArray == NULL)
	{
		cout << endl << "Memory error creating array of linked lists!";
		system("PAUSE");
		return;
	}
     for(int i = 0; i < hashTableSize; i++)
     {
             listArray[i] = NULL;
     }    

}

Can you use a std::vector<list>?

Also I would be very careful with the name "list". If somewhere else std::list gets included, then you're going to have conflicts.

Dave

You are not setting the pointer to NULL you are trying to assign an initialized object of type list to NULL and there is no such assignment operator.

I can not use vector unfortunately. Thanks for the tip about list, I changed my code. I've totally changed strategy as I think my problems is trying to do everything with the one Struct. I've created another struct to hold the initial hash value and HEAD/TAIL pointers to the linked lists. See below.

struct hashType
{
	listType *head;
	listType *tail;
	int numValue;
};

 for(int i = 0; i < hashTableSize; i++)
     {
         listArray[i].numValue = 0;    
		 listArray[i].head = NULL;
		 listArray[i].tail = NULL;
     }

I think I'm on the right track now, but please let me know if I've gone astray.

Edited 6 Years Ago by gtateco: n/a

I hope there is no naming conflict here , since there is a std::list. But this code:

listArray = new list[hashTableSize];

will THROW and exception, instead of setting listArray to null. So there is no
reason to check if listArray is null, because you won't get a chance to check it, unless you use nothrow.


EDIT, from your previous post, you need a good constructor :

struct hashType{
	listType *head;
	listType *tail;
	int numValue;
 //use a initializer list, to initialize head,tail,numValue using its default constructor.
 hashType() : head(), tail(), numValue(){} 
};

Edited 6 Years Ago by firstPerson: n/a

This question has already been answered. Start a new discussion instead.