Ziltoid

llist

Apr 11th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include "linkedlist.h"
  2.  
  3. typedef struct netobj
  4. {
  5.     int id;
  6.     int socket;
  7.     int status;
  8.     in_addr_t ip_addr;
  9. }netobj;
  10.  
  11. typedef struct node
  12. {
  13.     netobj obj;
  14.     struct node *next;
  15. }node;
  16.  
  17. node *head;
  18. node *tail;
  19. pthread_mutex_t llist_mutex = PTHREAD_MUTEX_INITIALIZER;
  20.  
  21. void init_llist()
  22. {
  23.     head = (node*)malloc(sizeof(node));
  24.     (head->obj).id = 0;
  25.     (head->obj).socket = 0;
  26.     (head->obj).status = 0;
  27.     (head->obj).ip_addr = 0;
  28.     tail = (node*)malloc(sizeof(node));
  29.     (tail->obj).id = 0;
  30.     (tail->obj).socket = 0;
  31.     (tail->obj).status = 0;
  32.     (tail->obj).ip_addr = 0;
  33.     tail->next = 0;
  34.     head->next = tail;
  35. }
  36.  
  37. node *init_node(netobj obj)
  38. {
  39.     node *ptr;
  40.     ptr = (node*)calloc(1, sizeof(node));
  41.     if(ptr == 0)
  42.     {
  43.         return (node*)0;
  44.     }
  45.     else
  46.     {
  47.         (ptr->obj).id = obj.id;
  48.         (ptr->obj).socket = obj.socket;
  49.         (ptr->obj).status = obj.status;
  50.         (ptr->obj).ip_addr = obj.ip_addr;
  51.     }
  52.     return ptr;
  53. }
  54.  
  55. void add_llist(netobj obj)
  56. {
  57.     pthread_mutex_lock(&llist_mutex);
  58.     node *new = init_node(obj);
  59.     node *it = head;
  60.     if(head == 0)
  61.     {
  62.         head = new;
  63.     }
  64.     else if(head->next == 0)
  65.     {
  66.         head->next = new;
  67.     }
  68.     else
  69.     {
  70.         while(1)
  71.         {
  72.             if(it->next == 0)
  73.             {
  74.                 new->next = tail;
  75.                 it->next = new;
  76.                 break;
  77.             }
  78.             it = it->next;
  79.         }
  80.     }
  81.     pthread_mutex_unlock(&llist_mutex);
  82. }
  83.  
  84. node *find_node_llist(netobj obj)
  85. {
  86.     pthread_mutex_lock(&llist_mutex);
  87.     node *it = head;
  88.     while((it->obj).id != obj.id && (it->obj).ip_addr == obj.ip_addr)
  89.     {
  90.         it = it->next;
  91.         if(it == 0)
  92.         {
  93.             break;
  94.         }
  95.     }
  96.     pthread_mutex_unlock(&llist_mutex);
  97.     return it;
  98. }
  99.  
  100. int find_llist(netobj obj)
  101. {
  102.     pthread_mutex_lock(&llist_mutex);
  103.     node *it = head;
  104.     while(it != 0)
  105.     {
  106.         if((it->obj).id == obj.id && (it->obj).ip_addr == obj.ip_addr
  107.                 && (it->obj).socket == obj.socket)
  108.         {
  109.             pthread_mutex_unlock(&llist_mutex);
  110.             return 1;
  111.         }
  112.         it = it->next;
  113.     }
  114.     pthread_mutex_unlock(&llist_mutex);
  115.     return 0;
  116. }
  117.  
  118. int rm_llist(netobj obj)
  119. {
  120.     pthread_mutex_lock(&llist_mutex);
  121.     node *rmnode = find_node_llist(obj);
  122.     if(rmnode == 0)
  123.     {
  124.         pthread_mutex_unlock(&llist_mutex);
  125.         return -1;
  126.     }
  127.     else
  128.     {
  129.         node *tmp, *prev;
  130.         tmp = rmnode;
  131.         prev = head;
  132.  
  133.         if(tmp == prev)
  134.         {
  135.             head = head->next;
  136.             if(tail == tmp)
  137.             {
  138.                 tail = tail->next;
  139.             }
  140.             free(tmp);
  141.         }
  142.         else
  143.         {
  144.             while(prev->next != tmp)
  145.             {
  146.                 prev = prev->next;
  147.             }
  148.             prev->next = tmp->next;
  149.             if(tail == tmp)
  150.             {
  151.                 tail = prev;
  152.             }
  153.         }
  154.     }
  155.     pthread_mutex_unlock(&llist_mutex);
  156.     return 0;
  157. }
  158.  
  159. int cnt_llist()
  160. {
  161.     pthread_mutex_lock(&llist_mutex);
  162.     node *it = head;
  163.     int i = 0;
  164.     while(it != 0)
  165.     {
  166.         it =  it->next;
  167.         i++;
  168.     }
  169.     //printf("list count: %d\n", i - 1);
  170.     pthread_mutex_unlock(&llist_mutex);
  171.     return i - 1;
  172. }
  173.  
  174. void print_llist()
  175. {
  176.     pthread_mutex_lock(&llist_mutex);
  177.     node *it = head;
  178.     printf("printing list:\n");
  179.     printf("----------\n");
  180.     while(it != 0)
  181.     {
  182.         printf("%d\n", (it->obj).id);
  183.         it = it->next;
  184.     }
  185.     printf("----------\n");
  186.     pthread_mutex_unlock(&llist_mutex);
  187. }
Advertisement
Add Comment
Please, Sign In to add comment