gen_linklist.c 2.36 KB
#include    "gen_linklist.h"
#include "platform.h"

// https://blog.csdn.net/swwlqw/article/details/22498833

/*-------------- insert ------------------------------
* insert data to any postion
*/
unsigned char gen_linklist__insert(GenLinkList* const list,void *pele,unsigned short index)
{
    if(list->length < index) return 0;
    platform.disable_interrupt();//platform__system_intf.disable_INT();//	
    GenLinkNode *node = (GenLinkNode *)malloc(sizeof(GenLinkNode));
		
		while(node ==NULL){//debug only
//			my_debug.printf_error("gen_linklist__insert() malloc fail \n");
			//platform__system_intfp->delay_ms(500);
			platform.enable_interrupt();//platform__system_intf.enable_INT();//
			return 0;
		}
	
    node->next = NULL;
    node->pdata = pele;

    GenLinkNode *pnode_tmp= list->front;
    for(unsigned short i=0;i<index;i++){
        pnode_tmp =pnode_tmp->next;
    }//for

    node->next =pnode_tmp->next;
    pnode_tmp->next =node;
    platform.enable_interrupt();//platform__system_intf.enable_INT();//
    (list->length)++;  

    return 1;  
}

/* -------------- detele  ------------------------------
* detele node at any postion
*/
void* gen_linklist__detele(GenLinkList* const list,unsigned short index)
{
    if(list->length < index) return NULL;
    platform.disable_interrupt();//platform__system_intf.disable_INT();//
    GenLinkNode *pnode_tmp= list->front;
    for(unsigned short i=0;i<index;i++){
        pnode_tmp =pnode_tmp->next;
    }//for

    GenLinkNode *pnode_record= pnode_tmp->next;
    pnode_tmp->next = pnode_tmp->next->next;
    void * re =pnode_record->pdata;
    free(pnode_record);
		if(list->length)
			(list->length)--;
    platform.enable_interrupt();//platform__system_intf.enable_INT();//		
    return re;
}

/*-------------- get ele ------------------------------
* get element of index;
*/
void* gen_linklist__get_ele(GenLinkList* const list,unsigned short index)
{
    if( list->length < index) return NULL;

    GenLinkNode *pnode_tmp= list->front->next;//diff with no head
    for(unsigned short i=0;i<index;i++){
        //if(pnode_tmp->next != NULL)
        pnode_tmp =pnode_tmp->next;
    }//for
		if(pnode_tmp != NULL)
    return pnode_tmp->pdata;
		else
			return NULL;
}

/*-------------- get len ------------------------------
* get length of linklist 
*/
unsigned short gen_linklist__get_len(GenLinkList* const list)
{
    return list->length;
}