天天看點

List單連結清單封裝

#include <stdlib.h>
#include <string.h>

#define MAX_PHONE_NUM 65
#define CON_OK   1
#define CON_FAIL 0


typedef struct T_RelayCallItem
{
    char caller[MAX_PHONE_NUM];
    char callee[MAX_PHONE_NUM];
    int call_type;
    struct T_RelayCallItem *pNext;
}tRelayCallItem;


typedef struct T_RelayCallList
{
    int lens;
    tRelayCallItem *pHead;
    tRelayCallItem *pRear;
}tRelayCallList;

tRelayCallList g_RelayCallList;

void InitRelayCallList()
{
    tRelayCallList *pList = &g_RelayCallList;
    pList->lens  = 0;
    pList->pHead = NULL;
    pList->pRear = NULL;
}

tRelayCallItem *AddRelayCallIntoList(tRelayCallItem*pNew)
{
    tRelayCallList *pList = &g_RelayCallList;
    tRelayCallItem *pRear = pList->pRear;
    
    if (pRear == NULL)
    {
        pList->pHead = pList->pRear = pNew;
    }
    else
    {
        pList->pRear->pNext = pNew;
        pList->pRear = pNew;
    }
    pList->lens++;

    return pNew;
}

tRelayCallList *DestroyRelayCallList()
{
    tRelayCallList *pList = &g_RelayCallList;
    tRelayCallItem *pHead = pList->pHead;
    
    while(pHead)
    {
        tRelayCallItem *pTmp = pHead->pNext;
        free(pHead);
        pHead = pTmp;     
    }

    pList->pHead = pList->pRear = NULL;
    pList->lens = 0;

    return pList;
}

int DeleteRelayCallItemFromList(tRelayCallItem *pNew)
{
    tRelayCallList *pList = &g_RelayCallList;
    tRelayCallItem *curr_node;
    tRelayCallItem *pre_node;
	
    if((pList == NULL) || (pNew == NULL))
    {
        return CON_FAIL;
    }
    
    if(pList->pHead == pNew)
    {
        pList->pHead = pNew->pNext;
        if(pNew == pList->pRear)
        {
        	pList->pRear = NULL;
        }
        free(pNew);
        pList->lens--;
        return CON_OK;
    }

    curr_node = pList->pHead;
    pre_node = curr_node;
    while(curr_node != NULL)
    {
        if(curr_node == pNew)
        {
        	pre_node->pNext = curr_node->pNext;
        	if(pNew == pList->pRear)
        	{
        		pList->pRear = pre_node;
        	}
        	free(pNew);
        	pList->lens--;
        	return CON_OK;
        }

        pre_node = curr_node;
        curr_node = curr_node->pNext;
    }

    return CON_FAIL;
}

int DeleteRelayCallItemByCallerCallee(char *caller, char *callee)
{
    tRelayCallList *pList = &g_RelayCallList;
    tRelayCallItem *curr_node;
    tRelayCallItem *tmp_node;  
    tRelayCallItem *pre_node;
	
    if((caller == NULL) || (callee == NULL))
    {
        printf("Input pointer is NULL");
        return CON_FAIL;
    }
    curr_node = pList->pHead;
    while(curr_node != NULL)
    {
        if((!strcmp(curr_node->caller,caller) && !strcmp(curr_node->callee,callee))
                || (!strcmp(curr_node->caller,callee) && !strcmp(curr_node->callee,caller)))
        {
            tmp_node = curr_node->pNext;
            if (pList->pHead == curr_node)
            {
                pList->pHead = tmp_node;
            }
            if (pList->pRear == curr_node)
            {
                pList->pRear = NULL;
            }
            free(curr_node);
            pList->lens--;
            
            curr_node = tmp_node;
            tmp_node = tmp_node->pNext;            
        }
        else
        {
            tmp_node = NULL;
            break;
        }
    }

    pre_node = curr_node;
    curr_node = curr_node->pNext;
    while(curr_node != NULL)
    {
        if((!strcmp(curr_node->caller,caller) && !strcmp(curr_node->callee,callee))
                || (!strcmp(curr_node->caller,callee) && !strcmp(curr_node->callee,caller)))
        {
        	pre_node->pNext = curr_node->pNext;
        	if(curr_node == pList->pRear)
        	{
        		pList->pRear = pre_node;
        	}
        	free(curr_node);
        	pList->lens--;

            curr_node = pre_node->pNext;
        }
        else
        {
            pre_node = curr_node;
            curr_node = curr_node->pNext;            
        }
    }    
    
    return CON_OK;
}

void PrintList()
{
    tRelayCallItem *item = g_RelayCallList.pHead;
	while (item != NULL)
	{
		printf("-------caller:%s,callee:%s\n",item->caller,item->callee);
		item = item->pNext;
	}

}

int main(int argc, char* argv[])
{
    tRelayCallItem *item;

	int i;
	for (i = 0; i < 10 ; i ++)
	{
		item = (tRelayCallItem *)malloc(sizeof(tRelayCallItem));
		memset(item,0,sizeof(tRelayCallItem));
		if (i%4 == 0)
		{
			strcpy(item->caller,"35");
			strcpy(item->callee,"36");
			item->call_type = 1;
		}
		else if (i%4 == 1)
		{
			strcpy(item->caller,"6051");
			strcpy(item->callee,"6052");
			item->call_type = 1;
		}
		else if (i%4 == 2)
		{
			strcpy(item->caller,"36");
			strcpy(item->callee,"35");
			item->call_type = 2;
		}
		else
		{
			strcpy(item->caller,"6051");
			strcpy(item->callee,"6052");
			item->call_type = 2;
		}

		AddRelayCallIntoList(item);	
	}

	PrintList();
	DeleteRelayCallItemByCallerCallee("35","36");
	PrintList();
	
	return 0;
}
           
extern int g_dbg_level;
extern FILE *g_log_fp;
/* debug level */
#define DBG_INFOR  	0x01  // call information
#define DBG_WARNING	0x02  // paramters invalid,  
#define DBG_ERROR  	0x04  // process error, leading to one call fails
#define DBG_CRITICAL	0x08  // process error, leading to voip process can't run exactly or exit
/* debug macro */
#define DBG(level, fmt, para...) 	\
	{ \
		time_t t = time(NULL);  \
		struct tm *tmm = localtime(&t); \
		if(g_dbg_level & level) 	\
			printf("[%d-%02d-%02d %02d:%02d:%02d][%s][%s:%d]" fmt "\n",tmm->tm_year+1900,tmm->tm_mon+1,tmm->tm_mday,tmm->tm_hour,tmm->tm_min,tmm->tm_sec,__FUNCTION__,__FILE__,__LINE__,##para); \
		if((g_log_fp != NULL) && (level == DBG_CRITICAL))	\
			fprintf(g_log_fp, "[%d-%02d-%02d %02d:%02d:%02d][%s][%s:%d]" fmt "\n",tmm->tm_year+1900,tmm->tm_mon+1,tmm->tm_mday,tmm->tm_hour,tmm->tm_min,tmm->tm_sec,__FUNCTION__,__FILE__,__LINE__,##para); \
	}
           

繼續閱讀