/home/dko/projects/mobilec/tags/MobileC-v1.10.2/MobileC-v1.10.2/src/mc_list/list.c

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 207 $ : Last Committed Revision
00003  * $Date: 2008-07-11 17:55:19 -0700 (Fri, 11 Jul 2008) $ : Last Committed Date */
00004 /*
00005  * Copyright (c) 2006 Regents of the University of California.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms are permitted
00009  * provided that the above copyright notice and this paragraph are
00010  * duplicated in all such forms and that any documentation,
00011  * advertising materials, and other materials related to such
00012  * distribution and use acknowledge that the software was developed
00013  * by the Integration Engineering Laboratory of the University of 
00014  * California, Davis. The name of the University may not be used to 
00015  * endorse or promote products derived from this software without 
00016  * specific prior written permission. 
00017  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND WITHOUT ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00019  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00020  */
00021 
00022 /* Filename: list.c */
00023 
00024 #include "list.h"
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 
00028 /*****************************************************************
00029  * Function Name : ListInitialize()
00030  * Purpose : allocates and initializes the list pointer
00031  * Return : a pointer to the new linked list. 
00032  **********************************************************/
00033 list_p ListInitialize(void)
00034 {
00035     list_p list;
00036     /* allocate memory for the list */
00037     list = (list_p)malloc(sizeof(list_t));
00038     list->size = 0;
00039     list->listhead = NULL;
00040 
00041     return list;
00042 }
00043 
00044 /***********************************************************
00045  * Function Name : ListTerminate
00046  * Purpose : Delete and Deallocates all data on list
00047  * Returns :void
00048  ***********************************************************/
00049 void ListTerminate(list_p list)
00050 {
00051     /* ensure that the list has no more nodes */
00052     if(list->size > 0)
00053     {
00054         printf("ERROR: MEMORY leak Created \n");
00055         return;
00056     } 
00057 
00058     /* deallocate the listhead */
00059     if(list->listhead != NULL)
00060         free(list->listhead);
00061 
00062     /* deallocate the list */
00063     free(list);
00064 
00065     return;
00066 }
00067 
00068 /**********************************************************
00069  * Function Name : ListGetSize()
00070  * Purpose : 
00071  * Return : size of the list;
00072  *********************************************************/
00073 int list_pGetSize(list_p list)
00074 {
00075     return list->size;
00076 }
00077 
00078 
00079 DATA ListGetHead(list_p list)
00080 {
00081     return list->listhead->node_data;
00082 }
00083 
00084 /********************************************************
00085  * Function Name : ListPop()
00086  * Purpose : Removes and returns the first data element on the list
00087  * Return : DATA at the head of the linked list
00088  *******************************************************/
00089 DATA ListPop(list_p list) 
00090 {
00091     listNode_t *parsenode;
00092     DATA data;
00093     parsenode = (listNode_t *)list->listhead;
00094 
00095     if(list->listhead == NULL)
00096         return NULL;
00097 
00098     /* find the element, return and then delete */
00099     if(parsenode != NULL)
00100     {
00101         list->listhead = (listNode_t *)list->listhead->next;
00102         data = parsenode->node_data;
00103         free(parsenode);
00104         if(data == NULL)
00105         {
00106             printf("returning NULL data \n");
00107             exit(EXIT_FAILURE);
00108         }
00109         list->size--;
00110         return data;
00111     }
00112     else
00113     {
00114         printf("There is nothing in the list we are returning NULL \n");
00115         return (DATA)NULL;
00116     }
00117 }
00118 
00119 /***************************************************************
00120  * Function Name  : ListSearch()
00121  * Purpose : goes through the list by order of insertion, returns DATA 
00122  * Returns: DATA element at the index place of the list
00123  **************************************************************/
00124 DATA ListSearch(list_p list, const int index)
00125 {
00126     /* variables */
00127     listNode_t *parsenode;
00128     int i;
00129 
00130     /* check to make sure list is not null */
00131     if(list->listhead == NULL)
00132         return NULL;
00133 
00134     /* initialize variables */
00135     parsenode = list->listhead;
00136     i = 0;
00137 
00138     /* look for the index */
00139     for(
00140             parsenode = (listNode_t *)list->listhead;
00141             parsenode->next != NULL;
00142             parsenode = (listNode_t *)parsenode->next
00143        )
00144     {
00145         if(i == index)
00146             break;
00147         if(i == list->size)
00148             return NULL;
00149         i++;
00150     }
00151 
00152     /* return the entry that matches index */
00153     return parsenode->node_data;
00154 }
00155 
00156 /*************************************************************
00157  * Function Name : ListAdd
00158  * Purpose : Adds a data element to the end of the list
00159  * Returns : 0 on success -1 on failure. 
00160  **************************************************************/
00161 int ListAdd(list_p list, DATA data)
00162 {
00163     /* variables */
00164     listNode_t *parsenode;
00165     listNode_t *new_listnode;
00166     parsenode = (listNode_t *) list->listhead;
00167 
00168     /* create the new node that will be inserted into the list */
00169     new_listnode = (listNode_t *)malloc(sizeof(listNode_t));
00170     new_listnode->node_data = data;
00171 
00172     /* If the list is currently empty, we can insert into the first one */
00173     if(list->listhead == NULL)
00174     {
00175         list->listhead = new_listnode;
00176         list->listhead->next = NULL;
00177         list->size = 1;
00178 
00179         return 0;
00180     }
00181 
00182     /* look for the next empty spot to place a new node */
00183     for(
00184             parsenode = (listNode_t *) list->listhead; 
00185             parsenode->next != NULL; 
00186             parsenode = (listNode_t *) parsenode->next
00187        );
00188 
00189     /* parsenode->next = (struct listNode_t *)new_listnode; */
00190     parsenode->next = (listNode_t *)new_listnode;
00191     new_listnode->next = NULL;
00192     list->size++;
00193 
00194     /* return 0 for success */
00195     return 0;
00196 }
00197 
00198 /****************************************************************
00199  * Function Name : ListInsert
00200  * Purpose: To Add a DATA element to the idex place in the linked  list
00201  *
00202  ****************************************************************/
00203 int ListInsert(list_p list, DATA data, const int index)
00204 {
00205     /* Function has not been written yet 
00206        currently there is no need for this utility */
00207 
00208     return 0;
00209 }
00210 
00211 /******************************************************
00212  * Function Name: ListDelete 
00213  * Purpose: Deletes an element from the list
00214  * Return : 0 success , -1 failure 
00215  *****************************************************/
00216 DATA ListDelete(list_p list, const int index)
00217 {
00218     /* variables */
00219     listNode_t *parsenode;
00220     int i;
00221     listNode_t *previous_parsenode;
00222     DATA return_data;
00223     parsenode = list->listhead;
00224     previous_parsenode = NULL;
00225 
00226     /* run through the list until the index element is found */
00227     if(index >= list->size || index < 0)
00228         return NULL;
00229 
00230     if(index == 0) 
00231     {
00232         /* Delete and return the head */
00233         parsenode = list->listhead;
00234         list->listhead = list->listhead->next;
00235         list->size--;
00236         return_data = parsenode->node_data;
00237     } else {
00238 
00239         for(i = 1; i < list->size && parsenode != NULL; i++)
00240         {
00241             previous_parsenode = parsenode;
00242             parsenode = (listNode_t *) parsenode->next;
00243             if(i == index)
00244                 break;
00245         }
00246 
00247         /* destroy the pointer */  
00248         previous_parsenode->next = parsenode->next;
00249 
00250         /* save the data from the node */
00251         return_data = parsenode->node_data;
00252 
00253         list->size--;
00254     }
00255     /* free the memory for the node */
00256     free(parsenode); 
00257 
00258     /* return a pointer of the data */
00259     return return_data;
00260 
00261 }
00262 

Generated on Fri Jul 11 17:59:45 2008 for Mobile-C by  doxygen 1.5.4