C generische verknüpfte Liste

Ich habe eine generische verknüpfte Liste, die Daten vom Typ void enthält. * Ich versuche, meine Liste mit dem Typ struct employee zu füllen. Schließlich möchte ich auch den Objekt struct employee zerstören.

Betrachten Sie diese generische Linked-List-Header-Datei (ich habe sie mit dem Typ char * getestet):

struct accListNode                 //the nodes of a linked-list for any data type
{
  void *data;                     //generic pointer to any data type
  struct accListNode *next;       //the next node in the list
};

struct accList                    //a linked-list consisting of accListNodes
{
  struct accListNode *head;
  struct accListNode *tail;
  int size;
};

void accList_allocate(struct accList *theList);           //allocate the accList and set to NULL
void appendToEnd(void *data, struct accList *theList);    //append data to the end of the accList
void removeData(void *data, struct accList *theList);         //removes data from accList
  --------------------------------------------------------------------------------------

Berücksichtigen Sie die Mitarbeiterstruktur

struct employee 
{ 
   char name[20]; 
   float wageRate; 
} 

Betrachten Sie nun diesen Beispiel-Testfall, der von main () aufgerufen wird:

    void test2()
    {
      struct accList secondList;
      struct employee *emp = Malloc(sizeof(struct employee));
      emp->name = "Dan";
      emp->wageRate =.5;

      struct employee *emp2 = Malloc(sizeof(struct employee));
      emp2->name = "Stan";
      emp2->wageRate = .3;

      accList_allocate(&secondList);
      appendToEnd(emp, &secondList);
      appendToEnd(emp2, &secondList);

      printf("Employee: %s\n", ((struct employee*)secondList.head->data)->name);   //cast to type struct employee
      printf("Employee2: %s\n", ((struct employee*)secondList.tail->data)->name);  
    }

Warum löst die Antwort, die ich unten gepostet habe, mein Problem? Ich glaube, es hat etwas mit Zeigern und Speicherzuweisung zu tun. Die Funktion Malloc (), die ich benutze, ist eine benutzerdefinierte Malloc, die überprüft, ob NULL zurückgegeben wird.

Hier ist ein Link zu meiner gesamten Implementierung der generischen verknüpften Liste:https://codereview.stackexchange.com/questions/13007/c-linked-list-implementation

Antworten auf die Frage(5)

Ihre Antwort auf die Frage