码迷,mamicode.com
首页 > 其他好文 > 详细

双向链表

时间:2016-04-29 19:06:43      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

双向链表介绍

       双向链表,如同其名所暗示的那样,链表元素之间由两个指针链接。双向链表中的每一个元素都由3部分组成:除了数据域成员和next指针外,每个元素还包含一个指向其前驱元素的指针,称为prev指针。双向链表的组成是这样的:将一些元素链接在一起使得每个元素的next指针都指向其后继的元素,而每个元素的prev指针都指向其前驱元素。为了标识链表的头和尾,将第一个元素的prev指针和最后一个元素的next指针设置为NULL。

       待补图


双向链表的实现

// DList.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>


/*双链表元素结构体定义*/
typedef struct  DListElmt_
{
	unsigned int uiValueA;   /*数据域 数据成员*/
	unsigned int *puiValueB; /*数据域 数据成员,需要动态申请内存*/

	struct DListElmt_ *pNext;  /*指针域 指向后一个元素结点的指针*/
	struct DListElmt_ *pPrev;  /*指针域 指向前一个元素结点的指针*/

}DListElmt;

/*双链表结构体定义*/
typedef struct DList_
{
	int  iSize;   /*链表大小*/

	void (*destory)(void *data);  /*用于释放元素结点内动态申请的内存*/
	int (*compare)(void *key1, void *key2); /*比较函数 返回值 0 相等 1 大于 -1 小于*/

	DListElmt  *pDListElmtHead;  /*指向链表头结点的指针*/
	DListElmt  *pDListElmtTail;  /*指向链表尾结点的指针*/

}DList;

/*单链表操作宏定义*/
#define DLIST_SIZE(list)   ((list)->iSize)
#define DLIST_HEAD(list)   ((list)->pDListElmtHead)
#define DLIST_TAIL(list)   ((list)->pDListElmtTail)
#define DLIST_IS_HEAD(list, element) (((element) == (list)->pDListElmtHead) ? 1:0)
#define DLIST_IS_TAIL(list, element) (((element) == (list)->pDListElmtTail) ? 1:0)

/*函数声明*/
void MemoryFree(void *data);
int DataCompare(void *key1, void *key2);
void DList_Init(DList *dlist, void (*destory)(void *data), int (*compare)(void *key1, void *key2));
int DList_Insert_Next(DList *dlist, DListElmt *Lelement, unsigned int uiDataA, unsigned int *puiDataB);
int DList_Insert_Prev(DList *dlist, DListElmt *Lelement, unsigned int uiDataA, unsigned int *puiDataB);
int DList_Remove(DList *dlist, DListElmt *Lelement, unsigned int *puiDataA, unsigned int **puiDataB);
void DList_Sort(DList *dlist);
void DList_Swap(DListElmt *LelementKey1, DListElmt *LelementKey2);
void DList_Destory(DList *dlist);

/*
*函数名:MemoryFree
*参数:data  待释放的内存
*功能:释放动态申请的内存
*返回值:无
*作者:AlbertoNo1
*日期:2016-04-20
*/
void MemoryFree(void *data)
{
	free(data);
}

/*
*函数名:DataCompare
*参数:key1  待比较的元素1
*      key2  待比较的元素2
*功能:比较两个元素的大小(无符号整型)
*返回值:0 key1 等于 key2  1 key1 大于 key2  -1 key1 小于 key2 
*作者:AlbertoNo1
*日期:2016-04-20
*/
int DataCompare(void *key1, void *key2)
{
	unsigned int uiKey1 = 0;
	unsigned int uiKey2 = 0;

	uiKey1 = *((unsigned int*)key1);
	uiKey2 = *((unsigned int*)key2);

	if (uiKey1 < uiKey2)
	{
		return -1;
	}
	else if (uiKey1 > uiKey2)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/*
*函数名:DList_Init
*参数:dlist  待初始化的链表
*      destory  数据域动态内存释放函数
*      compare  比较函数
*功能:双链表初始化函数
*返回值:无 
*作者:AlbertoNo1
*日期:2016-04-20
*/
void DList_Init(DList *dlist, void (*destory)(void *data), int (*compare)(void *key1, void *key2))
{
	dlist->iSize = 0;

	dlist->destory = destory;
	dlist->compare = compare;

	dlist->pDListElmtHead = NULL;
	dlist->pDListElmtTail = NULL;
}

/*
*函数名:DList_Insert_Next
*参数:dlist  链表
*      Lelment  待插入元素的前一个元素指针
*      uiDataA  数据成员值
*      puiDataB 数据成员值(动态申请内存)
*功能:双链表插入元素函数(指定元素之后)
*返回值:0 成功 -1 失败 
*作者:AlbertoNo1
*日期:2016-04-20
*/
int DList_Insert_Next(DList *dlist, DListElmt *Lelement, unsigned int uiDataA, unsigned int *puiDataB)
{
	DListElmt  *pElment = NULL;

	/*入参合法值由调用者保证*/

	/*申请内存*/
	pElment = (DListElmt*)malloc(sizeof(DListElmt));
	if (NULL == pElment)
	{
		return -1;
	}

	/*数据域赋值*/
	pElment->uiValueA = uiDataA;
	pElment->puiValueB = puiDataB;

	if (0 == DLIST_SIZE(dlist))
	{/*空链表插入元素*/
		pElment->pNext = NULL;
		pElment->pPrev = NULL;

		/*更新链表头尾结点指针*/
		dlist->pDListElmtHead = pElment;
		dlist->pDListElmtTail = pElment;
	}
	else
	{
		if (NULL == Lelement)
		{/*插入链表头*/
			pElment->pNext = dlist->pDListElmtHead;
			pElment->pPrev = NULL;

			dlist->pDListElmtHead->pPrev = pElment;
			dlist->pDListElmtHead = pElment;
		}
		else if (DLIST_IS_TAIL(dlist, Lelement))
		{/*插入链表尾*/
			pElment->pNext = NULL;
			pElment->pPrev = dlist->pDListElmtTail;

			dlist->pDListElmtTail->pNext = pElment;
			dlist->pDListElmtTail = pElment;
		}
		else
		{
			Lelement->pNext->pPrev = pElment;

			pElment->pNext = Lelement->pNext;
			pElment->pPrev = Lelement;

			Lelement->pNext = pElment;
		}
	}

	dlist->iSize++;

	return 0;
}

/*
*函数名:DList_Insert_Prev
*参数:dlist  链表
*      Lelment  待插入元素的后一个元素指针
*      uiDataA  数据成员值
*      puiDataB 数据成员值(动态申请内存)
*功能:双链表插入元素函数(指定元素之前)
*返回值:0 成功 -1 失败 
*作者:AlbertoNo1
*日期:2016-04-20
*/
int DList_Insert_Prev(DList *dlist, DListElmt *Lelement, unsigned int uiDataA, unsigned int *puiDataB)
{
	DListElmt  *pElment = NULL;

	/*入参合法值由调用者保证*/

	/*申请内存*/
	pElment = (DListElmt*)malloc(sizeof(DListElmt));
	if (NULL == pElment)
	{
		return -1;
	}

	/*数据域赋值*/
	pElment->uiValueA = uiDataA;
	pElment->puiValueB = puiDataB;

	if (0 == DLIST_SIZE(dlist))
	{/*空链表插入元素*/
		pElment->pNext = NULL;
		pElment->pPrev = NULL;

		/*更新链表头尾结点指针*/
		dlist->pDListElmtHead = pElment;
		dlist->pDListElmtTail = pElment;
	}
	else
	{
		if (NULL == Lelement)
		{/*插入链表尾*/
			pElment->pNext = NULL;
			pElment->pPrev = dlist->pDListElmtTail;

			dlist->pDListElmtTail->pNext = pElment;
			dlist->pDListElmtTail = pElment;
		}
		else if (DLIST_IS_HEAD(dlist, Lelement))
		{/*插入链表头*/
			pElment->pNext = dlist->pDListElmtHead;
			pElment->pPrev = NULL;

			dlist->pDListElmtHead->pPrev = pElment;
			dlist->pDListElmtHead = pElment;
		}
		else
		{
			Lelement->pPrev->pNext = pElment;

			pElment->pPrev = Lelement->pPrev;
			pElment->pNext = Lelement;

			Lelement->pPrev = pElment;
		}
	}

	dlist->iSize++;

	return 0;
}


/*
*函数名:DList_Remove
*参数:dlist  链表
*      Lelment  待删除元素的指针
*      puiDataA  返回数据成员值
*      puiDataB 返回数据成员值(动态申请内存)
*功能:双链表删除指定的元素
*返回值:0 成功 -1 失败 
*作者:AlbertoNo1
*日期:2016-04-20
*/
int DList_Remove(DList *dlist, DListElmt *Lelement, unsigned int *puiDataA, unsigned int **puiDataB)
{
	if (0 == DLIST_SIZE(dlist) || (NULL == Lelement))
	{
		return -1;
	}

	*puiDataA = Lelement->uiValueA;
	*puiDataB = Lelement->puiValueB;

	if (DLIST_IS_HEAD(dlist, Lelement))
	{
		dlist->pDListElmtHead = dlist->pDListElmtHead->pNext;

		if (1 == DLIST_SIZE(dlist))
		{/*最后一个元素*/
			dlist->pDListElmtHead = NULL;
			dlist->pDListElmtTail = NULL;
		}
		else
		{
			dlist->pDListElmtHead->pPrev = NULL;
		}

	}
	else if (DLIST_IS_TAIL(dlist, Lelement))
	{
		dlist->pDListElmtTail = dlist->pDListElmtTail->pPrev;

		if (1 == DLIST_SIZE(dlist))
		{/*最后一个元素*/
			dlist->pDListElmtHead = NULL;
			dlist->pDListElmtTail = NULL;
		}
		else
		{
			dlist->pDListElmtTail->pNext = NULL;
		}
	}
	else
	{
		Lelement->pNext->pPrev = Lelement->pPrev;
		Lelement->pPrev->pNext = Lelement->pNext;
	}

	free(Lelement);

	dlist->iSize--;

	return 0 ;
}

/*
*函数名:DList_Sort
*参数:dlist 链表
*功能:双链表排序
*返回值:无 
*作者:AlbertoNo1
*日期:2016-04-20
*/
void DList_Sort(DList *dlist)
{
	DListElmt  *pElmentPos = NULL;
	DListElmt  *pElmentA = NULL;
	DListElmt  *pElmentB = NULL;

	if (0 == DLIST_SIZE(dlist))
	{
		return ;
	}

	for (pElmentA = dlist->pDListElmtHead; NULL != pElmentA; pElmentA = pElmentA->pNext)
	{
		pElmentPos = pElmentA;

		for (pElmentB = pElmentA->pNext; NULL != pElmentB; pElmentB = pElmentB->pNext)
		{
			if (dlist->compare(&(pElmentB->uiValueA), &(pElmentPos->uiValueA)) < 0)
			{
				pElmentPos = pElmentB;
			}
		}

		if (pElmentPos != pElmentA)
		{
			DList_Swap(pElmentPos, pElmentA);
		}
	}
}

/*
*函数名:DList_Swap
*参数:dlist  链表
*功能:双链表交换两个结点的数据域
*返回值:无 
*作者:AlbertoNo1
*日期:2016-04-20
*/
void DList_Swap(DListElmt *LelementKey1, DListElmt *LelementKey2)
{
	unsigned int uiValueA = 0;   
	unsigned int *puiValueB = NULL; 

	uiValueA = LelementKey1->uiValueA;
	puiValueB = LelementKey1->puiValueB;

	LelementKey1->uiValueA = LelementKey2->uiValueA;
	LelementKey1->puiValueB = LelementKey2->puiValueB;

	LelementKey2->uiValueA = uiValueA;
	LelementKey2->puiValueB = puiValueB;
}


/*
*函数名:DList_Destory
*参数:dlist 链表
*功能:双链表销毁函数
*返回值:无 
*作者:AlbertoNo1
*日期:2016-04-20
*/
void DList_Destory(DList *dlist)
{
	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	while (DLIST_SIZE(dlist) > 0)
	{
		if ((0 == DList_Remove(dlist, DLIST_HEAD(dlist), &uiDataA, &puiDataB))&&(NULL != dlist->destory))
		{
			dlist->destory(puiDataB);
		}
	}
}

/*空链表插入*/
void test_func1(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 11;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 99;

	iRet = DList_Insert_Next(dlist, NULL, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:11 99 \n");
	printf("链表大小:1 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入头结点*/
void test_func2(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 12;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 98;

	iRet = DList_Insert_Next(dlist, NULL, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 \n");
	printf("链表大小:2 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入尾结点*/
void test_func3(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 13;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 97;

	iRet = DList_Insert_Next(dlist, DLIST_TAIL(dlist), uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 13 97\n");
	printf("链表大小:3 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入非头尾结点*/
void test_func4(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 14;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 96;

	iRet = DList_Insert_Next(dlist, dlist->pDListElmtHead->pNext, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 14 96 13 97\n");
	printf("链表大小:4 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*空链表插入*/
void test_func5(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 11;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 99;

	iRet = DList_Insert_Prev(dlist, NULL, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:11 99 \n");
	printf("链表大小:1 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入头结点*/
void test_func6(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 12;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 98;

	iRet = DList_Insert_Prev(dlist, DLIST_HEAD(dlist), uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 \n");
	printf("链表大小:2 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入尾结点*/
void test_func7(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 13;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 97;

	iRet = DList_Insert_Prev(dlist, NULL, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 13 97\n");
	printf("链表大小:3 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*非空链表插入非头尾结点*/
void test_func8(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/
	uiDataA = 14;
	puiDataB = (unsigned int*)malloc(sizeof(unsigned int));
	if (NULL == puiDataB)
	{
		return;
	}

	*puiDataB = 96;

	iRet = DList_Insert_Prev(dlist, dlist->pDListElmtHead->pNext, uiDataA, puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 14 96 11 99 13 97\n");
	printf("链表大小:4 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*销毁链表*/
void test_func9(DList *dlist)
{

	DListElmt *element = NULL;

	/*测试*/


	DList_Destory(dlist);

	printf("预期结果\n");
	printf("数据:\n");
	printf("链表大小:0 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*删除链表指定元素*/
void test_func10(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/

	iRet = DList_Remove(dlist, DLIST_TAIL(dlist), &uiDataA, &puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 14 96 11 99 \n");
	printf("链表大小:3 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*删除链表指定元素*/
void test_func11(DList *dlist)
{
	int iRet = 0;

	unsigned int uiDataA = 0;
	unsigned int *puiDataB = NULL;

	DListElmt *element = NULL;

	/*测试*/

	iRet = DList_Remove(dlist, dlist->pDListElmtHead->pNext, &uiDataA, &puiDataB);
	if (-1 == iRet)
	{
		return;
	}

	printf("预期结果\n");
	printf("数据:12 98 11 99 \n");
	printf("链表大小:2 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

/*双链表排序*/
void test_func12(DList *dlist)
{
	int iRet = 0;

	DListElmt *element = NULL;

	/*测试*/
	DList_Sort(dlist);

	printf("预期结果\n");
	printf("数据:11 99 12 98 13 97 14 96\n");
	printf("链表大小:4 \n");

	printf("\n实际结果1(顺序)\n");
	printf("数据:");

	for(element = DLIST_HEAD(dlist); NULL != element; element = element->pNext)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

	printf("\n实际结果2(逆序)\n");
	printf("数据:");

	for(element = DLIST_TAIL(dlist); NULL != element; element = element->pPrev)
	{
		printf("%u %u ", element->uiValueA, *(element->puiValueB));
	}

	printf("\n链表大小:%d \n", DLIST_SIZE(dlist));

}

int _tmain(int argc, _TCHAR* argv[])
{
	int iRet = 0;
	int iLoop = 0;

	DList stDList = {0};
	DListElmt *element = NULL;

	/*链表初始化*/
	DList_Init(&stDList, MemoryFree, DataCompare);

	/*测试1*/
	test_func5(&stDList);

	printf("\n\n");

	/*测试2*/
	test_func6(&stDList);

	printf("\n\n");

	/*测试3*/
	test_func7(&stDList);

	printf("\n\n");

	/*测试4*/
	test_func8(&stDList);

	printf("\n\n");

	test_func12(&stDList);

	//test_func9(&stDList);
	//test_func10(&stDList);

	//printf("\n\n");

	//test_func9(&stDList);
	//test_func11(&stDList);

	DList_Destory(&stDList);

	getchar();
	return 0;
}


双向链表

标签:

原文地址:http://blog.csdn.net/albertono1/article/details/51234019

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!