Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- typedef struct{
- char type[FILETYPE];
- char desc[MAXDESC];
- void * next;
- } typeNode;
- typedef struct{
- typeNode * head;
- typeNode * foot;
- int count;
- }typeList;
- typeList * initList()
- {
- typeList * tl = NULL;
- tl = malloc(sizeof(typeList));
- if(tl == NULL)
- {
- //Fqiled to Allocate Memory
- fprintf(stderr,"Failed to allocate a type list.\n");
- exit(EXIT_FAILURE);
- }
- tl->head = NULL;
- tl->foot = NULL;
- tl->count = 0;
- return tl;
- }
- typeNode * createNode(char * type, char * desc)
- {
- typeNode * tn = NULL;
- tn = malloc(sizeof(typeNode));
- if(tn == NULL)
- {
- //tn failed to allocate
- fprintf(stderr,"TypeNode failed to allocate.");
- fprintf(stderr,"Type Value %s\n",type);
- fprintf(stderr,"Desc Value %s\n",desc);
- exit(EXIT_FAILURE);
- }
- memset(tn->type, 0, sizeof(tn->type));
- memset(tn->desc, 0, sizeof(tn->desc));
- strcpy(tn->type, type);
- strcpy(tn->desc, desc);
- tn->next = NULL;
- return tn;
- }
- //Insert at foot is the only necessary insertion type due to the expected low ammount of types.
- //For this reason, there is no handling of duplicate entries.
- void insertAtFoot(typeList * tList, char * type, char * desc)
- {
- typeNode * tn;
- tn = createNode(type, desc);
- //If the head is NULL, the list doesn't have
- //any members and needs to be filled at the front
- if(tList->head == NULL)
- {
- tList->head = tn;
- tList->foot = tn;
- tList->count += 1;
- return;
- }
- //Otherwise, the list has members and needs to be
- //filled from the foot end.
- else
- {
- tList->foot->next = tn;
- tList->foot = tn;
- tList->count += 1;
- return;
- }
- }
- //An insert at foot algorthm that handles duplicate entries.
- //More complexity and a bigger run time drain.
- void insertAtFootND(typeList * tList, char * type, char * desc)
- {
- typeNode * tn;
- typeNode * curr;
- tn = createNode(type, desc);
- curr = tList->head;
- while(curr != NULL)
- {
- if(strcmp(type, curr->type) == 0)
- {
- //Duplicate entry found.
- #ifdef DEBUG
- printf("Dulplicate Entry.\n");
- printf("Currnt Type: %s\n",curr->type);
- printf("Attempted Type: %s\n",tn->type);
- printf("Currnt Desc: %s\n",curr->desc);
- printf("Attempted Desc: %s\n",tn->desc);
- #endif
- return;
- }
- curr = curr->next;
- }
- //When curr == NULL, we've hit the end of the list.
- tList->foot->next = tn;
- tList->foot = tn;
- tList->count += 1;
- return;
- }
- void printTypes(typeList * tList)
- {
- typeNode * curr = NULL;
- int i = 0;
- if(tList == NULL)
- {
- printf("The list doesn't exist");
- exit(EXIT_FAILURE);
- }
- curr = tList->head;
- //If the list has no members
- if(curr == NULL)
- {
- fprintf(stderr,"Attempted to print empty list\n");
- return;
- }
- printf("Printing List\n");
- for(i = 0;i<tList->count;i++)
- {
- //Redundent check for stability reasons.
- if(curr == NULL)
- {
- fprintf(stderr,"Attempted to print a null node\n");
- return;
- }
- printf("Node %i:\n",i);
- printf("Type: %s\n",curr->type);
- printf("Description: %s\n",curr->desc);
- curr = curr->next;
- }
- //For good measure and for good formatting
- printf("\n");
- }
- //I scrub all possible data before freeing the memory
- //It adds compexity but is better practice in my opinion.
- void freeTNode(typeNode * type)
- {
- typeNode * dead;
- dead = type;
- if(dead != NULL)
- {
- if(dead->type != NULL)
- {
- //Setting the type field to all 0s.
- memset(type->type, 0, sizeof(type->type));
- }
- if(dead->desc != NULL)
- {
- //Setting the description field to all 0s.
- memset(type->desc, 0, sizeof(type->desc));
- }
- dead->next = NULL;
- //fprintf(stderr,"Freeing Node.\n");
- free(dead);
- return;
- }
- else
- {
- //Trying to free a NULL node, shoudln't happen but just in case
- fprintf(stderr,"Tried to free a NULL node.\n");
- return;
- }
- }
- void freeTList(typeList * tList)
- {
- typeNode * curr = NULL;
- typeNode * next = NULL;
- curr = tList->head;
- printf("Freeing List\n");
- //Interating though the list. I could have used a for loop
- //on the count variable but a while loop is more robust.
- while(curr != NULL)
- {
- next = curr->next;
- if(curr == tList->foot)
- {
- //Setting the foot to NULL
- tList->foot = NULL;
- }
- printf("Node %s being freed\n",curr->type);
- freeTNode(curr);
- curr = next;
- }
- tList->count = 0;
- free(tList);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement