Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. /**
  2. @author Craig Cuthbertson
  3. @login 1002386c
  4. @title AP3 Exercise 1
  5. This is my own work as defined in the Academic Ethics agreement I have signed.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "mlist.h"
  10.  
  11. #define HASHSIZE 20;
  12.  
  13. typedef struct mlistnode {
  14. struct mlistnode *next;
  15. MEntry *entry;
  16. } bucket;
  17.  
  18. struct mlist {
  19. int size;
  20. bucket **hash;
  21. };
  22.  
  23. int ml_verbose=0;
  24. int size = HASHSIZE;
  25.  
  26.  
  27. MList *ml_create(void) {
  28. /** Declare mailing list */
  29. MList *ml;
  30.  
  31. /** loop variable */
  32. int i;
  33.  
  34. /** temp hashtable initialisation variable */
  35. bucket *hashtab;
  36.  
  37. if(ml_verbose)
  38. fprintf(stderr,"mlist: creating mailing list\n");
  39.  
  40.  
  41.  
  42. if( (ml = (MList *) malloc(sizeof(MList))) == NULL){
  43. return ml;
  44. }
  45.  
  46. if( (ml->hash = (bucket **) calloc(size,sizeof(bucket *)))!=NULL){
  47. for(i=0;i<size;i++){
  48. ml->hash[i] = (bucket *) malloc(sizeof(bucket));
  49. ml->hash[i]->next = NULL;
  50. }
  51. }
  52.  
  53. /** Set hash pointer to bucket */
  54. ml->size=size;
  55.  
  56. /** Set mlist size */
  57. ml->size = size;
  58.  
  59. return ml;
  60.  
  61. }
  62.  
  63. /** adds MEntry to list,
  64. Returns 1 if successful
  65. Returns 0 if not successful
  66. */
  67. int ml_add(MList **ml, MEntry *me) {
  68. MList *m = *ml;
  69. unsigned long hashval;
  70. int i;
  71. bucket *buck,*bucket_new;
  72.  
  73. /** check duplicates */
  74. if (ml_lookup(m, me) != NULL)
  75. return 1;
  76.  
  77. /** allocate space for next entry */
  78. if((bucket_new = (bucket *) malloc(sizeof(bucket)))==NULL)
  79. return 0;
  80.  
  81. bucket_new->next = NULL;
  82.  
  83. /** Compute hash value of item */
  84. hashval = me_hash(me,m->size);
  85.  
  86.  
  87. /** choose appropriate bucket array from hash table */
  88. buck = m->hash[hashval];
  89.  
  90.  
  91. /** loop until free bucket */
  92. while(buck->next!=NULL){
  93. buck = buck->next;
  94. }
  95.  
  96. /** set next to an empty bucket, and the entry to mentry */
  97. buck->next = bucket_new;
  98. buck->entry = me;
  99.  
  100. return 1;
  101.  
  102. }
  103.  
  104. /** looks for entry in ml matching me
  105. if found, return pointer, if not return NULL */
  106. MEntry *ml_lookup(MList *ml, MEntry *me) {
  107. unsigned long hashval;
  108. bucket *buck_cursor;
  109.  
  110. int tempsize = ml->size;
  111.  
  112. /** print statement if verbose */
  113. if(ml_verbose)
  114. fprintf(stderr,"mlist: ml_lookup() entered\n");
  115.  
  116. /** calculate hashval of me */
  117. hashval = me_hash(me,ml->size);
  118.  
  119.  
  120. /** loop through cursor values checking entries */
  121. buck_cursor = ml->hash[hashval];
  122.  
  123. while(buck_cursor->next!=NULL){
  124. if(me_compare(buck_cursor->entry,me)==0){
  125. /**found match, return pointer */
  126. return buck_cursor->entry;
  127. } else {
  128. /** not found, continue searching*/
  129. buck_cursor = buck_cursor->next;
  130. }
  131. }
  132.  
  133. /** entry was not found, return NULL */
  134.  
  135. ml->size = tempsize;
  136. return NULL;
  137. }
  138.  
  139.  
  140. void ml_destroy(MList *ml) {
  141. int i;
  142. bucket *to_delete; /** pointer to node to delete */
  143. bucket *next_node; /** pointer to the next node to delete */
  144.  
  145. if(ml_verbose)
  146. fprintf(stderr,"mlist: ml_destroy() entered\n");
  147.  
  148. /** loop through each hash entry, then loop through buckets, free'ing */
  149. for(i=0;i<size;i++){
  150. to_delete = ml->hash[i];
  151. while(to_delete->next!=NULL){
  152. next_node = to_delete->next;
  153. me_destroy(to_delete->entry);
  154. free(to_delete);
  155. to_delete = next_node;
  156. }
  157. free(to_delete);
  158. }
  159. /** free structures */
  160. free(ml->hash);
  161. free(ml);
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement