Advertisement
Saand1338

Untitled

Sep 20th, 2011
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.34 KB | None | 0 0
  1. typedef struct{
  2.     char type[FILETYPE];
  3.     char desc[MAXDESC];
  4.     void * next;
  5. } typeNode;
  6.  
  7. typedef struct{
  8.     typeNode * head;
  9.     typeNode * foot;
  10.     int count;
  11. }typeList;
  12.  
  13. typeList * initList()
  14. {
  15.     typeList * tl = NULL;
  16.    
  17.     tl = malloc(sizeof(typeList));
  18.     if(tl == NULL)
  19.     {
  20.         //Fqiled to Allocate Memory
  21.         fprintf(stderr,"Failed to allocate a type list.\n");
  22.         exit(EXIT_FAILURE);
  23.     }
  24.    
  25.     tl->head = NULL;
  26.     tl->foot = NULL;
  27.     tl->count = 0;
  28.    
  29.     return tl;
  30. }
  31.  
  32. typeNode * createNode(char * type, char * desc)
  33. {
  34.     typeNode * tn = NULL;
  35.    
  36.     tn = malloc(sizeof(typeNode));
  37.     if(tn == NULL)
  38.     {
  39.         //tn failed to allocate
  40.         fprintf(stderr,"TypeNode failed to allocate.");
  41.         fprintf(stderr,"Type Value %s\n",type);
  42.         fprintf(stderr,"Desc Value %s\n",desc);
  43.         exit(EXIT_FAILURE);
  44.     }
  45.    
  46.     memset(tn->type, 0, sizeof(tn->type));
  47.     memset(tn->desc, 0, sizeof(tn->desc));
  48.    
  49.     strcpy(tn->type, type);
  50.     strcpy(tn->desc, desc);
  51.    
  52.     tn->next = NULL;
  53.    
  54.     return tn;
  55. }
  56.  
  57. //Insert at foot is the only necessary insertion type due to the expected low ammount of types.
  58. //For this reason, there is no handling of duplicate entries.
  59. void insertAtFoot(typeList * tList, char * type, char * desc)
  60. {
  61.     typeNode * tn;
  62.    
  63.     tn = createNode(type, desc);
  64.    
  65.     //If the head is NULL, the list doesn't have
  66.     //any members and needs to be filled at the front
  67.     if(tList->head == NULL)
  68.     {
  69.         tList->head = tn;
  70.         tList->foot = tn;
  71.         tList->count += 1;
  72.         return;
  73.     }
  74.     //Otherwise, the list has members and needs to be
  75.     //filled from the foot end.
  76.     else
  77.     {
  78.         tList->foot->next = tn;
  79.         tList->foot = tn;
  80.         tList->count += 1;
  81.         return;
  82.     }
  83. }
  84.  
  85. //An insert at foot algorthm that handles duplicate entries.
  86. //More complexity and a bigger run time drain.
  87. void insertAtFootND(typeList * tList, char * type, char * desc)
  88. {
  89.     typeNode * tn;
  90.     typeNode * curr;
  91.    
  92.     tn = createNode(type, desc);
  93.     curr = tList->head;
  94.    
  95.     while(curr != NULL)
  96.     {
  97.         if(strcmp(type, curr->type) == 0)
  98.         {
  99.             //Duplicate entry found.
  100.             #ifdef DEBUG
  101.             printf("Dulplicate Entry.\n");
  102.             printf("Currnt Type: %s\n",curr->type);
  103.             printf("Attempted Type: %s\n",tn->type);
  104.             printf("Currnt Desc: %s\n",curr->desc);
  105.             printf("Attempted Desc: %s\n",tn->desc);
  106.             #endif
  107.             return;
  108.         }
  109.         curr = curr->next;
  110.     }
  111.     //When curr == NULL, we've hit the end of the list.
  112.     tList->foot->next = tn;
  113.     tList->foot = tn;
  114.     tList->count += 1;
  115.     return;
  116. }
  117.  
  118. void printTypes(typeList * tList)
  119. {
  120.     typeNode * curr = NULL;
  121.     int i = 0;
  122.    
  123.     if(tList == NULL)
  124.     {
  125.         printf("The list doesn't exist");
  126.         exit(EXIT_FAILURE);
  127.     }
  128.    
  129.     curr = tList->head;
  130.    
  131.     //If the list has no members
  132.     if(curr == NULL)
  133.     {
  134.         fprintf(stderr,"Attempted to print empty list\n");
  135.         return;
  136.     }
  137.    
  138.     printf("Printing List\n");
  139.    
  140.     for(i = 0;i<tList->count;i++)
  141.     {
  142.         //Redundent check for stability reasons.
  143.         if(curr == NULL)
  144.         {
  145.             fprintf(stderr,"Attempted to print a null node\n");
  146.             return;
  147.         }
  148.        
  149.         printf("Node %i:\n",i);
  150.         printf("Type: %s\n",curr->type);
  151.         printf("Description: %s\n",curr->desc);
  152.        
  153.         curr = curr->next;
  154.     }
  155.     //For good measure and for good formatting
  156.     printf("\n");
  157. }
  158.  
  159. //I scrub all possible data before freeing the memory
  160. //It adds compexity but is better practice in my opinion.
  161. void freeTNode(typeNode * type)
  162. {
  163.     typeNode * dead;
  164.    
  165.     dead = type;
  166.    
  167.     if(dead != NULL)
  168.     {
  169.         if(dead->type != NULL)
  170.         {
  171.             //Setting the type field to all 0s.
  172.             memset(type->type, 0, sizeof(type->type));
  173.         }
  174.        
  175.         if(dead->desc != NULL)
  176.         {
  177.             //Setting the description field to all 0s.
  178.             memset(type->desc, 0, sizeof(type->desc));
  179.         }
  180.        
  181.         dead->next = NULL;
  182.        
  183.         //fprintf(stderr,"Freeing Node.\n");
  184.         free(dead);
  185.         return;
  186.     }
  187.     else
  188.     {
  189.         //Trying to free a NULL node, shoudln't happen but just in case
  190.         fprintf(stderr,"Tried to free a NULL node.\n");
  191.         return;
  192.     }
  193. }
  194.  
  195. void freeTList(typeList * tList)
  196. {
  197.     typeNode * curr = NULL;
  198.     typeNode * next = NULL;
  199.    
  200.     curr = tList->head;
  201.    
  202.     printf("Freeing List\n");
  203.    
  204.     //Interating though the list. I could have used a for loop
  205.     //on the count variable but a while loop is more robust.
  206.     while(curr != NULL)
  207.     {
  208.         next = curr->next;
  209.        
  210.         if(curr == tList->foot)
  211.         {
  212.             //Setting the foot to NULL
  213.             tList->foot = NULL;
  214.         }
  215.        
  216.         printf("Node %s being freed\n",curr->type);
  217.         freeTNode(curr);
  218.        
  219.         curr = next;
  220.     }
  221.    
  222.     tList->count = 0;
  223.    
  224.     free(tList);
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement