📄 aodv_list.ex.c
字号:
/** Aodv_List.ex.c **//****************************************//* Written *//* by *//* Lyes Guemari *//* *//* National Inst. of Stands & Tech *//* Washington, D.C., U.S.A. *//****************************************//* Include directives */#include "Aodv_List.h" /***************************//* Function definitions *//***************************//*/* return the number of elements /* currently present in the list*/int aodv_list_size (Aodv_List * list){ return list->size;}/*/* Init the fields of a newly /* created list*/void aodv_list_init (Aodv_List *list){ list->size = 0; list->first = OPC_NIL; list->last = OPC_NIL;}/*/* Allocate memory and init /* the newly created list*/Aodv_List * aodv_list_create( ){ Aodv_List* list; list = (Aodv_List*) op_prg_mem_alloc(sizeof(Aodv_List)); aodv_list_init (list); return list;}/*/* Insert the element data at the index position*/Compcodeaodv_list_insert (Aodv_List* list,void* data,int index){ Compcode result = OPC_COMPCODE_FAILURE; Aodv_List_Element* newElement; newElement = (Aodv_List_Element*) op_prg_mem_alloc(sizeof(Aodv_List_Element)); if (newElement!=OPC_NIL) { newElement->data = data; newElement->index = 0; newElement->next = OPC_NIL; if (list->size < 1) { list->first = newElement; list->last = newElement; } else { list->last->next = newElement; list->last = newElement; } list->size++; result = OPC_COMPCODE_SUCCESS; } if (result != OPC_COMPCODE_FAILURE) { list->last->index = index; } return result;}/*/* Return the value of the element at the index/* position and remove the corresponding element/* from the list */void* aodv_list_remove (Aodv_List* list,int index){Aodv_List_Element* currentElement;Aodv_List_Element* previousElement;void * data;previousElement=OPC_NIL;currentElement=list->first;/* go through all elements */while (currentElement != OPC_NIL) { /* check the index of the current element */ if (currentElement->index == index) { data = currentElement->data; if (previousElement == OPC_NIL) { list->first=currentElement->next; } else { previousElement->next=currentElement->next; if (currentElement==list->last) { list->last=previousElement; } } op_prg_mem_free(currentElement); list->size--; return data; } else { previousElement=currentElement; currentElement=currentElement->next; } } return OPC_NIL;}/*/* Obtains a pointer to an element stored in the specified list /* at the specified index*/void* aodv_list_access (Aodv_List* list,int index){ Aodv_List_Element* currentElement; Aodv_List_Element* previousElement; void *data; previousElement = OPC_NIL; currentElement = list->first; while (currentElement != OPC_NIL) { if (currentElement->index == index) { data = currentElement->data; return data; } else { previousElement=currentElement; currentElement=currentElement->next; } } return OPC_NIL;}Aodv_List_Element* aodv_list_access_first_element (Aodv_List* list){ return list->first;}void* aodv_list_access_first (Aodv_List* list){ return list->first->data;}void* aodv_list_remove_first (Aodv_List* list){ return aodv_list_remove(list, list->first->index);}Aodv_List_Element* aodv_list_access_last_element (Aodv_List* list){ return list->last;}Aodv_List_Element* aodv_list_access_next (Aodv_List_Element* element){ return element->next;}/*/* Print a list of integers*/void aodv_list_int_print(Aodv_List* list){int* i;int nbr;char msg[64];Aodv_List_Element* currentElement;currentElement = list->first;nbr=1;while(currentElement != NULL) { i=(int*)(currentElement->data); sprintf(msg,"|* %d", *i);printf("\t\t%s",msg);aodv_justify(strlen(msg), "*|", 41); currentElement = currentElement->next; nbr++; }if (nbr<2) printf(" |** List is empty\n");}/*/* Print a list of integers*/void aodv_list_print(Aodv_List* list){int *dbl_ptr, dbl;int nbr;Aodv_List_Element* currentElement;currentElement = list->first;if(currentElement != NULL) printf("\t\t\t");nbr=1;while(currentElement != NULL) { dbl_ptr =(int*)(currentElement->index); dbl = *dbl_ptr; printf("%d -> ", dbl); currentElement = currentElement->next; nbr++; }if (nbr<2) printf("\t\t\t Path is empty\n");}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -