/home/dko/projects/mobilec/trunk/src/mc_list/list.c

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

Generated on Fri May 16 14:49:54 2008 for Mobile-C by  doxygen 1.5.4