Hello, I have to create a double linked list array of pointers for the alphabet (one pointer per letter).

Ok, so I tried starting by creating a double linked list:

struct word{
    string newWord;
    word *next;
    word *back;
};

struct wordList{
    word *head;
};

And then array of pointers:

wordList *letter[26];

Is this starting out right? I am wondering, because when I went to initialize the array, I ran into compiler problems. Thanks in advance for your tips.

Terri

Well it seems to match your description so far.
Time to elaborate with a bit of code and some error messages.

Ok, so I need to initialize the double linked list. This is the code I wrote so far that worked:

wordList* Initialize_Lists()
{

// function to create list

  char ch;
   
  // attempt to allocate list structure
  wordList *current = new (nothrow) wordList;
 
  // check if allocation successful
  if (current == NULL) {
     cout << "Error - out of heap space!!" << endl;
     cout << "Press C to continue: ";
     cin >> ch;
  }
  else
     // initialize to empty list
     current->head = NULL;
 
  return current;

 }

I was thinking though that I need to initialize all 26 double linked lists. So I need to traverse through the array of pointers. This is where I get stuck.

I tried something like this:

wordList* Initialize_Lists()
{

// function to create list

  char ch;
   
  // attempt to allocate list structure
  
  for (int count = 0; count < 26; count++)
  {    
       wordList *letter[count] = new (nothrow) wordList;
 
       // check if allocation successful
       if (letter[count] == NULL) {
          cout << "Error - out of heap space!!" << endl;
          cout << "Press C to continue: ";
          cin >> ch;
          }
       else
           // initialize to empty list
           letter[count]->head = NULL;
   }
  return letter[count];

 }

I get the error message though that "variable-sized 'letter' may not be initialized".

So I am definitely doing something wrong.

Hmmm, I wonder if I could use the first block of code that initializes one linked list as one function, and then call that function within a for loop that runs through the 26 pointers. Hmmm.

Well your first function seems fine, as does the idea of calling it in a loop from say main(), or another function.

Ok, thank you! I tried this code to initialize all 26 pointers, and then printed them to the screen to check. They are printed out as '0'. I did have change the current->head = NULL to current = NULL for it to work though. Thanks again.

wordList* Initialize_Linked_Lists()
{

// function to create list

  char ch;
   
  // attempt to allocate list structure
  wordList *current = new (nothrow) wordList;
 
  // check if allocation successful
  if (current == NULL) {
     cout << "Error - out of heap space!!" << endl;
     cout << "Press C to continue: ";
     cin >> ch;
  }
  else
     // initialize to empty list
     current = NULL;
 
  return current;

 }
 
void Initialize_Array()
{
     wordList *current;
     
     for (int count = 0; count < SIZE_ALPHABET; count++)
     {    
         current = Initialize_Linked_Lists();
         letter[count] = current;
         cout << letter[count] << " ";
         
     }   
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.