Array of Linked Lists C++

耗尽温柔 提交于 2019-12-03 18:14:21
arrayOfPointers = new node*[arraySize];

That returns a bunch of unallocated pointers. Your top level array is fine, but its elements are still uninitialized pointers, so when you do this:

->next

You invoke undefined behavior. You're dereferencing an uninitialized pointer.

You allocated the array properly, now you need to allocate each pointer, i.e.,

for(int i = 0; i < arraySize; ++i) {
    arrayOfPointers[i] = new node;
}

As an aside, I realize that you're learning, but you should realize that you're essentially writing C here. In C++ you have a myriad of wonderful data structures that will handle memory allocation (and, more importantly, deallocation) for you.

Your code is good, but it's about how you declared your InitDynamicArrayList. One way is to use ***chainListArray, or the more C++-like syntax to use references like this:

void InitDynamicArrayList (int tableSize, chainNode **&chainListArray)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!