TUM-Logo

libRASCH

 

Home
 

General

About libRASCH/News
Design
Screen shots
Sample programs (with source code)
License
 

Download

libRASCH
Tools
 

Documentation

User
Developer
 

Resources

Mailing list
Supported Formats
Plugins
Status
Links
 
Contact
About this site
 
Last updated
Tue Mar 27 23:03:52 2007
libRASCH: linked_list.c Source File

linked_list.c

Go to the documentation of this file.
00001 
00011 /*----------------------------------------------------------------------------
00012  *
00013  * Copyright (C) 2002-2003, Raphael Schneider
00014  * See the file COPYING for information on usage and redistribution.
00015  *
00016  * $Header: /home/cvs_repos/librasch/src/linked_list.c,v 1.4 2004/02/19 14:42:12 rasch Exp $
00017  *
00018  *--------------------------------------------------------------------------*/
00019 
00020 #include <stdio.h>
00021 
00022 #define _LIBRASCH_BUILD
00023 #include <ra_linked_list.h>
00024 
00025 
00033 int
00034 ra_list_add(void **head, void *item)
00035 {
00036         struct linked_list *h;
00037         struct linked_list *i;
00038 
00039         if ((head == NULL) || (item == NULL))
00040                 return -1;
00041 
00042         h = (struct linked_list *) *head;
00043         i = (struct linked_list *) item;
00044 
00045         if (h == NULL)
00046         {
00047                 i->next = i->prev = NULL;
00048                 *head = item;
00049 
00050                 return 0;
00051         }
00052 
00053         do
00054         {
00055                 if (h->next == NULL)
00056                 {
00057                         h->next = i;
00058                         i->prev = h;
00059                         i->next = NULL;
00060 
00061                         return 0;
00062                 }
00063 
00064                 h = h->next;
00065         }
00066         while (h != NULL);
00067 
00068         return -1;              /* if func. comes here, something went wrong */
00069 } /* ra_list_add() */
00070 
00071 
00081 int
00082 ra_list_insert(void **head, void *prev, void *item)
00083 {
00084         struct linked_list *h;
00085         struct linked_list *p;
00086         struct linked_list *i;
00087 
00088         h = (struct linked_list *) *head;
00089         p = (struct linked_list *) prev;
00090         i = (struct linked_list *) item;
00091 
00092         if ((head == NULL) || (*head == NULL) || (i == NULL))
00093                 return -1;
00094 
00095         if (p == NULL)
00096         {
00097                 i->next = h;
00098                 i->prev = NULL;
00099                 h->prev = i;
00100                 *head = item;
00101         }
00102         else
00103         {
00104                 i->next = p->next;
00105                 i->prev = p;
00106 
00107                 p->next = i;
00108 
00109                 if (i->next != NULL)
00110                         i->next->prev = item;
00111         }
00112 
00113         return 0;
00114 } /* ra_list_insert() */
00115 
00116 
00124 int
00125 ra_list_del(void **head, void *item)
00126 {
00127         struct linked_list *h;
00128         struct linked_list *i;
00129 
00130         if ((head == NULL) || (*head == NULL) || (item == NULL))
00131                 return -1;
00132 
00133         h = (struct linked_list *) *head;
00134         i = (struct linked_list *) item;
00135 
00136         if (h == i)
00137         {
00138                 *head = h->next;
00139                 if (h->next != NULL)
00140                         h->next->prev = NULL;
00141         }
00142         else
00143         {
00144                 i->prev->next = i->next;
00145                 if (i->next != NULL)
00146                         i->next->prev = i->prev;
00147         }
00148 
00149         return 0;
00150 } /* ra_list_del() */
00151 
00152 
00153 int
00154 ra_list_len(void *head)
00155 {
00156         struct linked_list *h = (struct linked_list *)head;
00157         int cnt;
00158 
00159         cnt = 0;
00160         if (h == NULL)
00161                 return cnt;
00162 
00163         while (h)
00164         {
00165                 cnt++;
00166                 h = h->next;
00167         }
00168 
00169         return cnt;
00170 } /* ra_list_len() */

Generated on Fri May 27 11:32:38 2005 for libRASCH by  doxygen 1.4.2