For the following C linked list implementation, please give me some suggestions for improved efficiency, style and design. This implementation is not 100% complete, please give some suggestions for additional features. Notice the data type for the list is void*, I want it to be as generic as possible.
accList.h:
#ifndef ACCLIST_H
#define ACCLIST_H
#include <stdio.h>
#include <stdlib.h>
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
#endif
accList.c:
#include "accList.h"
#include "cmpsc311.h"
void accList_allocate(struct accList *theList) //allocate and initialize to NULL values
{
theList = Malloc(sizeof(struct accList));
theList->head = NULL;
theList->tail = NULL;
theList->size = 0;
}
void appendToEnd(void *data, struct accList *theList)
{
struct accListNode *newNode = Malloc(sizeof(struct accListNode));
newNode->data = data;
newNode->next = NULL;
if(theList->head == NULL) //the list is empty
{
theList->head = theList->tail = newNode;
}
else //the list is not empty
{
theList->tail->next = newNode;
theList->tail = newNode;
}
}
void removeData(void *data, struct accList *theList)
{
if(theList->head == NULL) //the list is empty
return;
else if(theList->head == theList->tail) //there is one element in the list
{
free(theList->head);
theList->head = NULL;
}
else if(data == theList->head->data) //the element to be removed is the head
{
struct accListNode *temp = theList->head->next;
free(theList->head);
theList->head = temp;
}
else if(data == theList->tail->data) //the element to be removed is the tail
{
struct accListNode *cur;
struct accListNode *prev = NULL;
for(cur = theList->head; cur->next != NULL; prev = cur, cur = cur->next);
free(theList->tail);
prev->next = NULL;
theList->tail = prev;
}
else //any other node
{
struct accListNode *prev = NULL;
struct accListNode *cur;
for(cur = theList->head; cur != NULL; prev = cur, cur = cur->next)
{
if(cur->data == data) //this is the node we must free
{
prev->next = cur->next;
free(cur);
}
}
}
}
I think the remove function needs the most work. I did post that function on this site, however I think I needed to include the whole implementation to get the best feedback. Will this work on an embedded system?