简体   繁体   中英

Need help understanding a list container

I am unsure about how this linked list would look like.

list<string> hashTable [HASH_TABLE_SIZE];

I would believe that this one:

list<string> hashTable;

would look like this:

Head->[]->[]->[]->NULL

but what does

list<string> hashTable [HASH_TABLE_SIZE];

look like?

it would look like this:

[Head][Head][Head]...
  ^     ^      ^
  |     |      |
  v     v      v
 [ ]   [ ]    [ ]
  ^     ^      ^
  |     |      |
  v     v      v
 NULL  NULL   NULL

That is an array of std::list's of std::string's, a std::list can be iterated both forwards and backwards, so it is really a doubly linked list. That looks more like the picture below

each:

hashTable[index]:

  begin()-*          *-rbegin()
          |          |
          v          v
  rend()<-[]<->[]<->[]->end()

/* walk index'th list in hashTable in forward order and print it out */
int f = 0;
list<string>::iterator fwdItr = hashTable[index].begin();
for (; fwdItr != hashTable[index].end(); ++f, ++fwdItr)
{
    printf("fwd list entry %d = %s\n", f, *fwdItr);
}


/* walk index'th list in hashTable in reverse order and print it out */
int r = 0;
list<string>::reverse_iterator revItr = hashTable[index].rbegin();
for (; revItr != hashTable[index].rend(); ++r, ++revItr)
{
    printf("rev list entry %d = %s\n", r, *revItr);
}

hope this helps illustrate std::list a bit better...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM