Link List (Amend)

Hmm. What a shame. I got a positive and negative feedbacks. On the assignment given. I join this one discussion board and gave me some feedbacks. This is what he wrote to me regarding the previous link list I've done. 

Amerei Acuna good job. a rather naive implementation.. but good job.

comments:

line 7: use of malloc() without including stdlib.h
line 7: not checking the return value of malloc(), it _could_ fail.
line 11: where is the return statement?
line 24: malloc() without corresponding free() is bad.

another approach to this is to use a head and tail pointer to the list like so:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct db {
  6.         int data;
  7.         struct db *next;
  8. } linklist;
  9.  
  10. typedef struct list_t {
  11.         linklist *head;
  12.         linklist *tail;
  13. } list;
  14.  
  15. linklist *make_node(const int n)
  16. {
  17.         linklist *node;
  18.  
  19.         node = malloc(sizeof *node);
  20.         memset(node, 0, sizeof *node);
  21.  
  22.         if (!node) return NULL;
  23.         node->data = n;
  24.  
  25.         return node;
  26. }
  27.  
  28. void add_to_list_tail(list *l, linklist *node)
  29. {
  30.         if (!node || !l) return;
  31.  
  32.         if (!l->head) {
  33.                 l->head = node;
  34.         } else {
  35.                 l->tail->next = node;
  36.         }
  37.  
  38.         l->tail = node;
  39. }
  40.  
  41. void print_list(list *l)
  42. {
  43.         linklist *node;
  44.  
  45.         if (!l) return ;
  46.  
  47.         node = l->head;
  48.         while (node) {
  49.                 printf("%d\n", node->data);
  50.                 node = node->next;
  51.         }
  52. }
  53.  
  54.  
  55. void free_list(list *l)
  56. {
  57.         linklist *node;
  58.         linklist *tmp;
  59.  
  60.         if (!l) return ;
  61.  
  62.         node = l->head;
  63.         while (node) {
  64.                 tmp = node;
  65.                 node = node->next;
  66.                 free(tmp);
  67.         }
  68.  
  69.         return ;
  70. }
  71.  
  72. int main(void)
  73. {
  74.         linklist *node;
  75.         list mylist = {0};
  76.  
  77.         node = make_node(509);
  78.         if (node)
  79.                 add_to_list_tail(&mylist, node);
  80.  
  81.         node = make_node(288);
  82.         if (node)
  83.                 add_to_list_tail(&mylist, node);
  84.  
  85.         node = make_node(366);
  86.         if (node)
  87.                 add_to_list_tail(&mylist, node);
  88.  
  89.         node = make_node(343);
  90.         if (node)
  91.                 add_to_list_tail(&mylist, node);
  92.  
  93.         print_list(&mylist);
  94.         free_list(&mylist);
  95.  
  96.         return 0;
  97. }
 Maybe there is alot I need to revised back. Thanks for the feed back though.

Comments

Popular Posts