c语言List头文件和应用

c语言List头文件和应用util_list.h#ifndef__UTIL_LIST__#define__UTIL_LIST__/*双链节点*/typedefstructlist_node{ list_node*prev; list_node*next;}LIST_NODE;/*单链节点*/typedefstructslist_node{ slist_node*ne

大家好,又见面了,我是你们的朋友全栈君。

util_list.h

#ifndef __UTIL_LIST__
#define __UTIL_LIST__


/*双链节点*/
typedef struct list_node
{
	struct list_node * prev;
	struct list_node * next;
} LIST_NODE;

/*单链节点*/
typedef struct slist_node
{
	struct slist_node * next;
} SLIST_NODE;


#define lst_offsetof( type, member )  ( unsigned int )( &((type*)0)->member )

/* 初始化双链节点*/
#define lst_init( list )       { (list)->next = (list)->prev = list; }

/* 判断双链是否为空,1:为空,0:不为空 */
#define lst_empty( list )      ( ( (list) == (list)->next ) ? 1 : 0 )

/* 在链表list头部增加节点 */
#define lst_add(list, node)    { (list)->next->prev = (node); (node)->next = (list)->next; (list)->next = (node); (node)->prev = (list); }

/* 在链表list尾部增加节点 */
#define lst_insert(list, node) { (list)->prev->next = (node); (node)->prev = (list)->prev; (list)->prev = (node); (node)->next = (list); }

/* 删除双链节点 */
#define lst_del(node)          { (node)->prev->next = (node)->next; (node)->next->prev = (node)->prev; }

/* 从双链节点得到实例节点 */
#define lst_entity(node, type, member) ((type*)((char *)(node) - lst_offsetof( type, member )))

/* 初始化双链节点*/
#define slst_init(list)              { (list)->next =  (list); }

/* 判断单链是否为空,1:为空,0:不为空 */
#define slst_empty(list)             (((list) == (list)->next ) ? 1 : 0 )

/* 在链表list头部增加节点 */
#define slst_add(list, node)         { (node)->next = (list)->next; (list)->next = (node); }

/* 删除单链节点,prev是node的前节点,由调用者保证 */
#define slst_del_prev( prev, node )  { (prev)->next = (node)->next; }

/* 删除单链节点 */
#define slst_del( node ) \
{ \
	slist_node * p    = (node)->next; \
	slist_node * prev = (node); \
    while (( node ) != p)\
		{ prev = p; p = p->next; }\
	slst_del_prev( prev, node );\
}

/* 从单链节点得到实例节点 */
#define slst_entity(node, type, member) ((type*)((char *)(node) - lst_offsetof(type, member)))

#endif  /* __UTIL_LIST__ */

实际应用

TestList2.cpp

// TestList2.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdlib.h"
#include "util_list.h"

LIST_NODE   g_TestNodeList     = { 0 };
SLIST_NODE  g_slstTestNodeList = { 0 };

typedef struct test_node
{
	LIST_NODE  ltNeigbor;
	SLIST_NODE sltNeigbor;

	int test_number;
} TEST_NODE;


int _tmain(int argc, _TCHAR* argv[]) 
{
	lst_init(&g_TestNodeList);
	slst_init(&g_slstTestNodeList);

	TEST_NODE * pstTestNode1 = (TEST_NODE *)malloc(sizeof(TEST_NODE));
	pstTestNode1->test_number = 100;	
	lst_add( &g_TestNodeList, &(pstTestNode1->ltNeigbor));
	//lst_insert(&g_TestNodeList, &(pstTestNode1->ltNeigbor));
	slst_add(&g_slstTestNodeList, &(pstTestNode1->sltNeigbor));

	TEST_NODE * pstTestNode2 = (TEST_NODE *)malloc(sizeof(TEST_NODE));
	pstTestNode2->test_number = 101;
	//lst_add( &g_TestNodeList, &(pstTestNode2->ltNeigbor));
	lst_insert(&g_TestNodeList, &(pstTestNode2->ltNeigbor));
	slst_add(&g_slstTestNodeList, &(pstTestNode2->sltNeigbor));

	TEST_NODE * pstTestNode3 = (TEST_NODE *)malloc(sizeof(TEST_NODE));
	pstTestNode3->test_number = 102;
	lst_add(&g_TestNodeList, &(pstTestNode3->ltNeigbor));
	//lst_insert(&g_TestNodeList, &(pstTestNode3->ltNeigbor));
	slst_add(&g_slstTestNodeList, &(pstTestNode3->sltNeigbor));

	LIST_NODE * pHead = &g_TestNodeList;
	LIST_NODE * pNext = pHead->next;

	while ( pNext != pHead )
	{
		TEST_NODE * pstTestNode100 = lst_entity( pNext, TEST_NODE, ltNeigbor );	
		lst_del(&( pstTestNode100->ltNeigbor ));
		pNext = pNext->next;
	}

	if (lst_empty( &g_TestNodeList ))
	{
		printf( "test ok" );
	}

	SLIST_NODE * psltHead = &g_slstTestNodeList;
	SLIST_NODE * psltNext = psltHead->next;
	SLIST_NODE * psltPrev = psltHead;

	while ( psltNext != psltHead )
	{
		TEST_NODE * pstTestNode101 = slst_entity( psltNext, TEST_NODE, sltNeigbor);

		if ( pstTestNode101->test_number == 102 )
		{
			slst_del_prev(psltPrev, &(pstTestNode101->sltNeigbor));
		}
		else
		{
			psltPrev = psltNext;
		}

		psltNext = psltNext->next;
	}

	slst_del( &( pstTestNode1->sltNeigbor ));
	slst_del( &( pstTestNode2->sltNeigbor ));

	if (slst_empty( &g_slstTestNodeList ))
	{
		printf("test2 ok");
	}

	free( pstTestNode1 );
	free( pstTestNode2 );
	free( pstTestNode3 );
	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157946.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号