sll.c

Go to the documentation of this file.
00001 /*
00002 ** Sll.c - Package to manage singly linked lists.
00003 ** ---------------------------------------------------------------
00004 **
00005 ** remarks:
00006 ** ---------------------------------------------------------------
00007 ** Functionality:
00008 ** This is a general package for generic handling of singly linked
00009 ** lists.  It is generic in the sense that it performs the basic
00010 ** operations on singly linked lists (add at the top, add at the
00011 ** end, etc.).  To use it adequately, it is required that the first 
00012 ** field of a list element be a pointer to the next element in the 
00013 ** list.  As usual, this field is null for the last element of the 
00014 ** list.
00015 **
00016 ** The generic structure for a list element is shown below:
00017 **
00018 ** typedef struct relaxsll {
00019 ** struct relaxsll    *next;
00020 ** } RSll;
00021 **
00022 **
00023 ** List head:
00024 ** All the functions in this package receive the head of the list 
00025 ** as their first argument.  The head works like a handle to the 
00026 ** list, which is used to get a hold to it.
00027 ** In the operators of this package, the head of the list is always
00028 ** returned as the function value.  This is done because there is 
00029 ** always the possibility that the head might change in each 
00030 ** operation.
00031 **
00032 ** ---------------------------------------------------------------
00033 **
00034 ** entry pts:
00035 ** ---------------------------------------------------------------
00036 **
00037 ** RSll   *RSllAddTop( RSll *head, RSll *elem )
00038 **
00039 **                head      - head of list                   (in)
00040 **                elem      - element to add                 (in)
00041 **
00042 **            This function inserts an element at the top of a list.
00043 **            If the list is empty, which is assumed the case if
00044 **            head is null, the function returns elem as the head.
00045 **
00046 **      ---------------------------------------------------------------
00047 **
00048 **       RSll   *RSllRmvTop( RSll *head )
00049 **
00050 **                head      - head of list                   (in)
00051 **
00052 **            This function removes the element at the top of a list
00053 **            and returns the next element (which can be null) as the
00054 **            new head.
00055 **            If the list is empty, which is assumed the case if
00056 **            head is null, it does not do anything.
00057 **
00058 **      ---------------------------------------------------------------
00059 **
00060 **      RSll   *RSllAddEnd( RSll *head, RSll *elem )
00061 **
00062 **                head      - head of list                   (in)
00063 **                elem      - element to add                 (in)
00064 **
00065 **            This function inserts an element at the end of a list.
00066 **            If the list is empty (null head), elem is returned 
00067 **            as the new head.
00068 **
00069 **      ---------------------------------------------------------------
00070 **
00071 **       RSll   *RSllAddAft( RSll *head, RSll *before, RSll *elem )
00072 **
00073 **                head      - head of list                   (in)
00074 **                before    - elem before the new one        (in)
00075 **                elem      - element to add                 (in)
00076 **
00077 **            This function inserts an element after a given element
00078 **            of a list.
00079 **            If the list is empty (null head), elem is returned 
00080 **            as the new head and the before parameter is
00081 **            disregarded.
00082 **            If the list is not empty, nothing will be done if the 
00083 **            before element is not found.
00084 **
00085 **      ---------------------------------------------------------------
00086 **
00087 **       RSll   *RSllAddBef( RSll *head, RSll *after, RSll *elem )
00088 **
00089 **                head      - head of list                   (in)
00090 **                after     - elem after the new one         (in)
00091 **                elem      - element to add                 (in)
00092 **
00093 **            This function inserts an element before a given element
00094 **            of a list.
00095 **            If the list is empty (null head), elem is returned 
00096 **            as the new head and the after parameter is
00097 **            disregarded.
00098 **            If the list is not empty, nothing will be done if the 
00099 **            after element is not found.
00100 **
00101 **      ---------------------------------------------------------------
00102 **
00103 **      RSll    *RSllRmvElm( RSll *head, RSll *elem )
00104 **
00105 **                head      - head of list                   (in)
00106 **                elem      - element to remove              (in)
00107 **
00108 **            This function removes an element of a list.
00109 **            If the list is empty (null head), nothing will be 
00110 **            done.
00111 **            If the element to remove is also the head, the next
00112 **            element is returned as the new head, which can also
00113 **            be null.
00114 **            If the given element does not belong to the list,
00115 **            nothing will be done.
00116 **
00117 **      ---------------------------------------------------------------
00118 **
00119 **      Version:      0-001
00120 **
00121 **      Created:    16-Aug-93    Luiz F. Martha
00122 **
00123 **      Modified:    13-Jan-06    Juan Pablo Ibañez                                 
00124 **        Modificados os nomes das funcões acrescentando uma R (relax)
00125 **        no inicio. Modificado o nome da estrutura sll para relaxsll,
00126 **        e o tipo Sll para RSll.
00127 */
00128 
00129 /*
00130 **      ---------------------------------------------------------------
00131 **      Global variables and symbols
00132 */
00133 #include "sll.h"
00134 
00135 /*
00136 **      ---------------------------------------------------------------
00137 **      Local variables and symbols: (none)
00138 */
00139 
00140 
00141 /*
00142 **      ---------------------------------------------------------------
00143 **      Private functions: (none)
00144 */
00145 
00146 
00147 /*
00148 **      ---------------------------------------------------------------
00149 **      Entry points begin here:
00150 */
00151 
00152 /*=========================  RSllAddTop  =========================*/
00153 
00154 RSll *RSllAddTop( RSll *head, RSll *elem )
00155 {
00156  elem->next = head;
00157 
00158  return( elem );
00159 }
00160 
00161 
00162 /*=========================  RSllRmvTop  =========================*/
00163 
00164 RSll *RSllRmvTop( RSll *head )
00165 {
00166  if( head == 0L )
00167  {
00168   return( 0L );
00169  }
00170  else
00171  {
00172   return( head->next );
00173  }
00174 }
00175 
00176 
00177 /*=========================  RSllAddEnd  =========================*/
00178 
00179 RSll *RSllAddEnd( RSll *head, RSll *elem )
00180 {
00181  RSll *curr;
00182 
00183  elem->next = 0L;
00184 
00185  if( head == 0L )
00186  {
00187   return( elem );
00188  }
00189  else
00190  {
00191   curr = head;
00192   while( curr->next != 0L )
00193   {
00194    curr = curr->next;
00195   }
00196   curr->next = elem;
00197  }
00198 
00199  return( head );
00200 }
00201 
00202 
00203 /*=========================  RSllAddAft  =========================*/
00204 
00205 RSll *RSllAddAft( RSll *head, RSll *before, RSll *elem )
00206 {
00207  RSll *curr;
00208 
00209  if( head == 0L)
00210  {
00211   elem->next = 0L;
00212   return( elem );
00213  }
00214 
00215  curr = head;
00216  while( (curr != before) && (curr != 0L) )
00217  {
00218   curr = curr->next;
00219  }
00220 
00221  if( curr != 0L )
00222  {
00223   elem->next = curr->next;
00224   curr->next = elem;
00225  }
00226 
00227  return( head );
00228 }
00229 
00230 
00231 /*=========================  RSllAddBef  =========================*/
00232 
00233 RSll *RSllAddBef( RSll *head, RSll *after, RSll *elem )
00234 {
00235  RSll *curr;
00236 
00237  if( head == 0L)
00238  {
00239   elem->next = 0L;
00240   return( elem );
00241  }
00242 
00243  if( after == head )
00244  {
00245   elem->next = head;
00246   return( elem );
00247  }
00248 
00249  curr = head;
00250  while( (curr->next != 0L) && (curr->next != after) )
00251  {
00252   curr = curr->next;
00253  }
00254 
00255  if( curr->next != 0L )
00256  {
00257   elem->next = curr->next;
00258   curr->next = elem;
00259  }
00260 
00261  return( head );
00262 }
00263 
00264 
00265 /*=========================  RSllRmvElm  =========================*/
00266 
00267 RSll *RSllRmvElm( RSll *head, RSll *elem )
00268 {
00269  RSll *prev;
00270 
00271  if( head == 0L ) return( 0L );
00272 
00273  if( head == elem )
00274  {
00275   head = elem->next;
00276  }
00277  else
00278  {
00279   prev = head;
00280   while( ( prev->next != elem ) && ( prev->next != 0L ) )
00281   {
00282    prev = prev->next;
00283   }
00284 
00285   if( prev->next != 0L )
00286   {
00287    prev->next = elem->next;
00288   }
00289  }
00290 
00291  return( head );
00292 }
00293 
00294 

Generated on Tue Oct 23 11:23:30 2007 for Relax by  doxygen 1.5.3