如何检查链表数组中的某个链表是否为空

how to check if a certain linked list in an array of linked lists is empty

本文关键字:链表 是否 数组 何检查 检查      更新时间:2023-10-16

使用此数据结构:(双向链表数组)

list<string> hashTable [HASH_TABLE_SIZE];

我想使用以下方法检查某个链表是否为空:

Hash HashTable;
if(Hash[hf(word)].empty() == true)
{do this}

这些是我得到的编译错误:

$ make -f makefile.txt
g++ -g -D HASH_TABLE_SIZE=10 -c hash.cpp
hash.cpp: In member function `void Hash::processFile(std::string)':
hash.cpp:19: error: expected primary-expression before '[' token
makefile.txt:6: recipe for target `hash.o' failed
make: *** [hash.o] Error 1

首先你有编译错误。请显示您的哈希.cpp文件的第 19 行。第二:

Hash[hf(word)].empty() == true

是一个不好的比较,你可以只使用

 if ( Hash[hf(word)].empty() )

第三:list是一个动态结构,所以,我认为,最好使用指向list的指针数组:

list<string> * hashTable[HASH_TABLE_SIZE]