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); \
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是将面向过程的单链表程序封装成类的示例: 头文件 LinkedList.h: ```cpp #ifndef LINKEDLIST_H #define LINKEDLIST_H struct Node { int data; Node* next; }; class LinkedList { private: Node* head; public: LinkedList(); ~LinkedList(); void insert(int value); void remove(int value); void display(); }; #endif ``` 实现文件 LinkedList.cpp: ```cpp #include "LinkedList.h" #include <iostream> LinkedList::LinkedList() { head = nullptr; } LinkedList::~LinkedList() { Node* current = head; while (current != nullptr) { Node* next = current->next; delete current; current = next; } } void LinkedList::insert(int value) { Node* newNode = new Node; newNode->data = value; newNode->next = nullptr; if (head == nullptr) { head = newNode; } else { Node* current = head; while (current->next != nullptr) { current = current->next; } current->next = newNode; } } void LinkedList::remove(int value) { if (head == nullptr) { std::cout << "List is empty." << std::endl; return; } if (head->data == value) { Node* temp = head; head = head->next; delete temp; return; } Node* prev = nullptr; Node* current = head; while (current != nullptr && current->data != value) { prev = current; current = current->next; } if (current == nullptr) { std::cout << "Value not found in the list." << std::endl; return; } prev->next = current->next; delete current; } void LinkedList::display() { if (head == nullptr) { std::cout << "List is empty." << std::endl; return; } Node* current = head; while (current != nullptr) { std::cout << current->data << " "; current = current->next; } std::cout << std::endl; } ``` 这样,你就可以在其他文件中包含 LinkedList.h 头文件,并使用 LinkedList 类来操作单链表了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值