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.
#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.
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?