single_link_list.h
#ifndef __COMM_UTILS_H__
#define __COMM_UTILS_H__
#ifdef __cplusplus
extern "C" {
#endif
#pragma pack(1)
/**************************************************/
/********************Cfg Macros********************/
/**************************************************/
#define COMM_LINK_UNUSED 0
#define COMM_LINK_USED 1
#define COMM_MAX_LINK_NUM 20
/**************************************************/
/*********************Structs**********************/
/**************************************************/
typedef struct NODE
{
void *element;
struct NODE *next;
}LinkNode;
typedef struct HEAD
{
BYTE flag;
WORD32 num; /* number of nodes, exclude head*/
WORD32 size;
struct NODE *next;
}LinkHead;
extern void InitSLink();
extern ODVS_STATUS CreateLinkList(WORD32 size);
extern INT32 GetLinkListSize(WORD16 linkId);
extern INT32 GetLinkListNum(WORD16 linkId);
extern ODVS_STATUS AddLinkListElem(WORD16 linkId, void *pElem, WORD16 position);
extern INT32 DelLinkListElem(WORD16 linkId, WORD16 elemNo);
extern void *GetLinkListElem(WORD16 linkId, WORD16 elemNo);
#pragma pack(0)
#ifdef __cplusplus
}
#endif
#endif
single_link_list.c
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <time.h>
LinkHead g_SLinkHead[COMM_MAX_LINK_NUM] = {0};
void InitSLink()
{
memset(g_SLinkHead, 0 , sizeof(g_SLinkHead));
}
ODVS_STATUS CreateLinkList(WORD32 size)
{
WORD16 i = 0;
for(i = 0; i < COMM_MAX_LINK_NUM; i++)
{
if(g_SLinkHead[i].flag == COMM_LINK_UNUSED)
{
g_SLinkHead[i].num = 0;
g_SLinkHead[i].size = size;
g_SLinkHead[i].next = NULL;
g_SLinkHead[i].flag = COMM_LINK_USED;
return i;
}
}
return ERROR;
}
INT32 GetLinkListNum(WORD16 linkId)
{
if((linkId >= COMM_MAX_LINK_NUM) || (g_SLinkHead[linkId].flag == COMM_LINK_UNUSED))
{
return ERROR;
}
return g_SLinkHead[linkId].num;
}
INT32 GetLinkListSize(WORD16 linkId)
{
if((linkId >= COMM_MAX_LINK_NUM) || (g_SLinkHead[linkId].flag == COMM_LINK_UNUSED))
{
return ERROR;
}
return g_SLinkHead[linkId].size;
}
/*GetLinkListElem
linkId:
elemNo: start from 1 to size of link
return: pointer of element
*/
void *GetLinkListElem(WORD16 linkId, WORD16 elemNo)
{
WORD16 i = elemNo;
LinkNode *currN = NULL;
if((linkId >= COMM_MAX_LINK_NUM) || (g_SLinkHead[linkId].flag == COMM_LINK_UNUSED))
{
return NULL;
}
if(elemNo > g_SLinkHead[linkId].num)
{
return NULL; /* no this node of postion */
}
currN = g_SLinkHead[linkId].next;
while(--i)
{
currN = currN->next;
}
return currN->element;
}
/* AddLinkListElem: insert element after position
linkId :
pElem : data to insert into link list
postion: start from 0 to size of link and 0 for meaning of inserting element after head
*/
INT32 AddLinkListElem(WORD16 linkId, void *pElem, WORD16 position)
{
ODVS_STATUS ret = ODVS_OK;
WORD16 i = position;
LinkNode *currN = NULL;
LinkNode *node = (LinkNode *)malloc(sizeof(LinkNode));
if(node == NULL)
{
return ODVS_ERROR;
}
if((linkId >= COMM_MAX_LINK_NUM) || (g_SLinkHead[linkId].flag == COMM_LINK_UNUSED))
{
ret = ODVS_ERROR;
}
if(g_SLinkHead[linkId].num == g_SLinkHead[linkId].size)
{
ret = ODVS_ERROR; /* full */
}
if(position > g_SLinkHead[linkId].num)
{
ret = ODVS_ERROR; /* no this node of postion */
}
if(ODVS_OK == ret)
{
if(position == 0)
{
node->next = g_SLinkHead[linkId].next;
g_SLinkHead[linkId].next = node;
}
else
{
currN = g_SLinkHead[linkId].next;
while(--i)
{
currN = currN->next;
}
node->next = currN->next;
currN->next = node;
}
g_SLinkHead[linkId].num++;
node->element = pElem;
}
else
{
free(node);
}
return ret;
}
/* AddLinkListElem: delete current element at position
linkId :
elemNo: start from 1 to size of link
*/
INT32 DelLinkListElem(WORD16 linkId, WORD16 elemNo)
{
WORD16 i = elemNo-1;
LinkNode *currN = NULL;
LinkNode *preN = NULL;
if((linkId >= COMM_MAX_LINK_NUM) || (g_SLinkHead[linkId].flag == COMM_LINK_UNUSED))
{
return ERROR;
}
if(elemNo > g_SLinkHead[linkId].num)
{
return ERROR; /* no this node of postion */
}
if(elemNo == 1)
{
currN = g_SLinkHead[linkId].next;
g_SLinkHead[linkId].next = currN->next;
free(currN);
}
else
{
preN = g_SLinkHead[linkId].next;
while(--i)
{
preN = preN->next;
}
currN = preN->next;
preN->next = currN->next;
free(currN);
}
g_SLinkHead[linkId].num--;
return OK;
}