⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ds_list.cpp

📁 内存数据库
💻 CPP
字号:
  
#include "ds_head.h"
#include "ds_list.h"

static void DSL_FreeNode( DS_LIST* pList, LPOSITION pos );


void DSL_InitList( DS_LIST* pList, UINT size )
{
	pList->head = NULL;
	pList->tail = NULL;
	pList->size = size;
	pList->count = 0;
}


static LPOSITION DSL_MakeNode( DS_LIST* pList, void* elem )
{
	LPOSITION pos;
	
	pos = (LPOSITION)malloc(sizeof(struct LNode));
	
	if( pos == NULL )
		return NULL;
	
	if( pList->size )
	{
		pos->data = malloc( pList->size );
		if( pos->data == NULL )
		{
			DSL_FreeNode( pList, pos );
			pos = NULL;

			return NULL;
		}

		memcpy( pos->data, elem, pList->size );
	}
	else
	{
		pos->data = elem;
	}
	
	pos->next = pos->prev = NULL;

	return pos;
}

static void DSL_FreeNode( DS_LIST* pList, LPOSITION pos )
{
	if( pos )
	{
		if( pos->data && pList->size )
		{
			free( pos->data );
		}

		free( pos );
	}
}

void* DSL_GetAt( LPOSITION pos )
{
	return pos->data;
}

BOOL DSL_IsEmpty( DS_LIST* pList )
{
	return (pList->count == 0 ? TRUE:FALSE);
}

int DSL_GetCount( DS_LIST* pList )
{
	return pList->count;
}

LPOSITION DSL_NextPos( LPOSITION pos )
{
	if( pos == NULL )
		return NULL;

	return pos->next;
}

LPOSITION DSL_PrevPos( LPOSITION pos )
{
	if( !pos )
		return NULL;

	return pos->prev;
}

LPOSITION DSL_GetHead( DS_LIST* pList )
{
	if( pList == NULL )
		return NULL;

	return pList->head;
}

LPOSITION DSL_GetLast( DS_LIST* pList )
{
	if( pList == NULL )
		return NULL;

	return pList->tail;
}

BOOL DSL_SetAt( DS_LIST* pList, LPOSITION pos, void* elem )
{
	if( pList == NULL )
		return FALSE;

	if( pos == NULL )
		return FALSE;

	if( pList->size )
	{
		memcpy( pos->data ,elem, pList->size );
	}
	else
	{
		pos->data = elem;
	}
	

	return TRUE;
}

void DSL_ClearList( DS_LIST* pList )
{
	LPOSITION currpos, nextpos;
	
	currpos = pList->head;
	while ( currpos != NULL ) 
	{
		nextpos = DSL_NextPos( currpos);
		DSL_FreeNode( pList, currpos );
		currpos = nextpos;
	}
	pList->head = pList->tail = NULL;
	pList->count = 0;
}

LPOSITION DSL_InsertBefore( DS_LIST* pList, LPOSITION pos, void* elem )
{
	LPOSITION newpos, tempos;

	if( pos == NULL )
		return FALSE;

	newpos = DSL_MakeNode( pList, elem );
	if( newpos == NULL )
		return NULL;

	if( pos->prev == NULL )
	{
		pos->prev = newpos;
		newpos->next = pos;
		pList->head = newpos;
		pList->size++;
	}
	else
	{
		tempos = pos->prev;
		pos->prev = newpos;
		newpos->next = pos;
		newpos->prev = tempos;
		tempos->next = newpos;
		pList->size++;
	}
	
	return newpos;
}

LPOSITION DSL_InsertAfter( DS_LIST* pList, LPOSITION pos, void* elem )
{
	LPOSITION newpos, tempos;

	if( pos == NULL )
		return NULL;

	newpos = DSL_MakeNode( pList, elem );
	if( newpos == NULL )
		return NULL;

	if( pos->next == NULL )
	{
		pos->next = newpos;
		newpos->prev = pos;
		pList->tail = newpos;
		pList->count++;
	}
	else
	{
		tempos = pos->next;
		pos->next = newpos;
		newpos->prev = pos;
		newpos->next = tempos;
		tempos->prev = newpos;
		pList->count++;
	}
	
	return newpos;
}

LPOSITION DSL_AddHead( DS_LIST* pList, void* elem )
{
	LPOSITION newpos;

	if( pList->head == NULL )
	{
		newpos=DSL_MakeNode( pList, elem );
		if( newpos == NULL )
			return NULL;		

		pList->head = pList->tail = newpos;
		pList->count++;
		
		return newpos;
	}
	else
		return DSL_InsertBefore( pList, pList->head, elem );
}

LPOSITION DSL_AddTail( DS_LIST* pList, void* elem )
{
	LPOSITION newpos;

	if( pList->tail == NULL )
	{
		newpos=DSL_MakeNode( pList, elem );
		if( newpos == NULL )
			return NULL;		

		pList->head = pList->tail = newpos;
		pList->count++;
		
		return newpos;
	}
	else
		return DSL_InsertAfter( pList, pList->tail, elem );
}

BOOL DSL_RemoveHead( DS_LIST* pList )
{
	LPOSITION nextpos;

	if( pList->head == NULL )
		return FALSE;

	nextpos = pList->head->next;
	DSL_FreeNode( pList, pList->head );

	if( nextpos == NULL )
	{
		pList->head = pList->tail = NULL;
		pList->count = 0;
	}
	else
	{
		nextpos->prev = NULL;
		pList->head = nextpos;
		pList->count--;
	}

	return TRUE;
}

BOOL DSL_RemoveAt( DS_LIST* pList, LPOSITION pos )
{
	LPOSITION prevpos, nextpos;

	if( pList->count == 0 )
		return FALSE;

	if( pos == NULL )
		return FALSE;
	
	prevpos = pos->prev;
	nextpos = pos->next;

	if( pos == pList->head )	//delete head element
		return DSL_RemoveHead( pList );
	else if( pos == pList->tail )
		return DSL_RemoveTail( pList );
	else
	{
		prevpos->next = nextpos;
		nextpos->prev = prevpos;
		pList->count--;
		DSL_FreeNode( pList, pos );
		return TRUE;
	}	
}

BOOL DSL_RemoveTail( DS_LIST* pList )
{
	LPOSITION prevpos;

	if( pList->tail == NULL )
		return FALSE;

	prevpos = pList->tail->prev;
	DSL_FreeNode( pList, pList->tail );

	if( prevpos == NULL )
	{
		pList->head = pList->tail = NULL;
		pList->count = 0;
	}
	else
	{
		prevpos->next = NULL;
		pList->tail = prevpos;
		pList->count--;
	}

	return TRUE;
}

BOOL DSL_IsEOF( DS_LIST* pList, LPOSITION pos )
{
	if( pos == pList->tail )
		return TRUE;

	return FALSE;
}

BOOL DSL_IsBOF( DS_LIST* pList, LPOSITION pos )
{
	if( pos == pList->head )
		return TRUE;

	return FALSE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -