/[svn]/hopm/trunk/src/list.c
ViewVC logotype

Diff of /hopm/trunk/src/list.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 8191 by michael, Sat Dec 31 13:59:46 2016 UTC revision 8192 by michael, Fri Apr 14 12:12:58 2017 UTC
# Line 18  Line 18 
18   *  USA   *  USA
19   */   */
20    
21    /*! \file list.c
22     * \brief Maintains doubly-linked lists.
23     * \version $Id$
24     */
25    
26  #include <stdlib.h>  #include <stdlib.h>
27    #include <assert.h>
28    
29  #include "memory.h"  #include "memory.h"
30  #include "list.h"  #include "list.h"
# Line 44  list_create(void) Line 50  list_create(void)
50  node_t *  node_t *
51  list_add(list_t *list, node_t *node)  list_add(list_t *list, node_t *node)
52  {  {
53    if (list == NULL || node == NULL)    node->prev = NULL;
54      return NULL;    node->next = list->head;
55    
56    if (list->tail == NULL)    /* Assumption: If list->tail != NULL, list->head != NULL */
57    {    if (list->head)
58      list->head = node;      list->head->prev = node;
59      else if (list->tail == NULL)
60      list->tail = node;      list->tail = node;
61    
62      node->next = NULL;    list->head = node;
63      node->prev = NULL;    list->elements++;
   }  
   else  
   {  
     node->prev = list->tail;  
     list->tail->next = node;  
     list->tail = node;  
     node->next = NULL;  
   }  
64    
   ++list->elements;  
65    return node;    return node;
66  }  }
67    
68  node_t *  node_t *
69  list_remove(list_t *list, node_t *node)  list_remove(list_t *list, node_t *node)
70  {  {
71    if (list == NULL || node == NULL)    /* Assumption: If node->next == NULL, then list->tail == node
72      return NULL;     *      and:   If node->prev == NULL, then list->head == node
73       */
74    if (node == list->head)    if (node->next)
75    {      node->next->prev = node->prev;
76      list->head = node->next;    else
   
     if (node->next)  
       node->next->prev = NULL;  
     else  
       list->tail = NULL;  
   }  
   else if (node == list->tail)  
77    {    {
78      list->tail = list->tail->prev;      assert(list->tail == node);
79      list->tail->next = NULL;      list->tail = node->prev;
80    }    }
81    
82      if (node->prev)
83        node->prev->next = node->next;
84    else    else
85    {    {
86      node->prev->next = node->next;      assert(list->head == node);
87      node->next->prev = node->prev;      list->head = node->next;
88    }    }
89    
90    --list->elements;    /* Set this to NULL does matter */
91      node->next = node->prev = NULL;
92      list->elements--;
93    
94    return node;    return node;
95  }  }
96    
# Line 106  list_free(list_t *list) Line 103  list_free(list_t *list)
103  void  void
104  node_free(node_t *node)  node_free(node_t *node)
105  {  {
106     xfree(node);    xfree(node);
107  }  }

Legend:
Removed from v.8191  
changed lines
  Added in v.8192

svnadmin@ircd-hybrid.org
ViewVC Help
Powered by ViewVC 1.1.28