array of linked lists

hi! is it possible to have an example of an array of linked lists?
Of course it is. You can have arrays of linked lists of arrays of linked lists of stacks of queues of linked lists. I don't recommend that though
which is the best way to create somethnig like this?
A linked list is just a data type. It's like any other object.

LinkedList<People> arr[1000];
Now you have 1000 linked lists. I think the head of each of these will be in contiguous space, and then the rest of the nodes will be placed where they fit in linked list fashion.
An array of lists is a type of hash table. It's actually a good data structure, provided the lists don't get too long.

This example shows a quick way to look up words that start with a certain letter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <list>
#include <string>
#include <iostream>

int main(void)
{
  std::list<std::string> hashTable[26];
  std::string temp;  

  // Collecting data
  std::cout << "Enter a word or enter to stop: ";
  std::cin >> temp;
  std::cin.ignore(80,'\n');
  while (temp.length() > 0)
  {
    // Create an index from the first letter 
    // A very simple hash table algorithm
    int lower = ( temp[0] <= 'Z' ? temp[0]  - 'A' : temp[0]  - 'a');
    hashTable[lower].push_back(temp);

    std::cout << "Enter a word or enter to stop: ";
    std::cin >> temp;
    std::cin.ignore(80,'\n');
  }


  // Printing Data
  for (int i = 0; i < 26; i++)
  {
    std::cout << "Printing words that start with " << i + 'A' << '\n';
    std::list<std::string>::iterator iter = hashTable[i].begin();
    while (iter != hashTable[i].end())
    {
      std::cout << '\t' << *iter << '\n';
      iter++;
    }
  }
  return 0;
}


The program didn't exactly "sort" anything, but it would be faster to find a specific word that had it just been one list.
Last edited on
thank you both of you!
I made the following code for an array of linked lists, the compiler doesn't show me any wrongs, but the code doesnt run at all.. any suggestions?

Last edited on
First, my code doesn't quite work. There is something wrong with the first loop and it doesn't end. Sort of thought this thread went into the deep.

1
2
3
4
5
6
7
int ctr, size;
char productforsearch[25];
struct product *var[size];


cout << "Please enter size: \n";
cin >> size;


Line three there should not compile, arrays must be declared with a size known at compile time. You also didn't initiate size to anything, so the size of your array (if it did compile) is unknown.

Try removing the dynamic size aspect, and see if you can get it working.

The use of your data structure (array of linked-list) is a little suspect, what do you intend to do?

I replace the input of size with "define size" and now the compiler stops the program when I am ready to input the "name"

Topic archived. No new replies allowed.