//doublelinkedlist.h
#ifndef DOUBLELINKEDLIST_DEFINE
#define DOUBLELINKEDLIST_DEFINE
#include <stdio.h>
#include <malloc.h>
typedef struct _NODE
{
struct _NODE * m_pPre;
struct _NODE * m_pNext;
void * m_pData;
}NODE;
void InsertBefore(NODE ** ppTop,NODE ** ppBottom,NODE * pCurrent,void * pData);
void InsertTop(NODE ** ppTop,NODE ** ppBottom,void * pData);
void InsertBottom(NODE ** ppTop,NODE ** ppBottom,void * pData);
int DeleteAt(NODE ** ppTop,NODE ** ppBottom,NODE * pCurrent);
int DeleteTop(NODE ** ppTop,NODE ** ppBottom);
int DeleteBottom(NODE ** ppTop,NODE ** ppBottom);
void PrintLinkedListData(NODE * pStart,void (* PrintFunc)(void *));
NODE * SearchLinkedListData(NODE * pStart,void * pData,int (* CompareFunc)(void *,void *));
#endif
//doublelinkedlist.cpp
#include "DoubleLinkedList.h"
void InsertBefore(NODE ** ppTop,NODE ** ppBottom,NODE * pCurrent,void * pData)
{
NODE * pNode = (NODE *)malloc(sizeof(NODE));
pNode->m_pPre = NULL;
pNode->m_pNext = NULL;
pNode->m_pData = pData;
if (*ppTop == NULL)
{
*ppTop = *ppBottom = pNode;
}
else if(*ppTop == pCurrent)
{
pNode->m_pNext = (*ppTop);
(*ppTop)->m_pPre = pNode;
(*ppTop) = pNode;
}
else if (pCurrent == NULL)
{
(*ppBottom)->m_pNext = pNode;
pNode->m_pPre = (*ppBottom);
(*ppBottom) = pNode;
}
else
{
pNode->m_pPre = pCurrent->m_pPre;
pNode->m_pNext = pCurrent;
pCurrent->m_pPre->m_pNext = pNode;
pCurrent->m_pPre = pNode;
}
};
void InsertTop(NODE ** ppTop,NODE ** ppBottom,void * pData)
{
InsertBefore(ppTop,ppBottom,*ppTop,pData);
}
void InsertBottom(NODE ** ppTop,NODE ** ppBottom,void * pData)
{
InsertBefore(ppTop,ppBottom,NULL,pData);
}
int DeleteAt(NODE ** ppTop,NODE ** ppBottom,NODE * pCurrent)
{
if (*ppTop == NULL || pCurrent == NULL)
{
return 0;
}
else if (*ppTop == pCurrent && *ppBottom == pCurrent)
{
*ppTop = *ppBottom = NULL;
}
else if (*ppTop == pCurrent)
{
(*ppTop) = (*ppTop)->m_pNext;
(*ppTop)->m_pPre = NULL;
}
else if (*ppBottom == pCurrent)
{
(*ppBottom) = (*ppBottom)->m_pPre;
(*ppBottom)->m_pNext = NULL;
}
else
{
pCurrent->m_pPre->m_pNext = pCurrent->m_pNext;
pCurrent->m_pNext->m_pPre = pCurrent->m_pPre;
}
free(pCurrent);
return 1;
}
int DeleteTop(NODE ** ppTop,NODE ** ppBottom)
{
return DeleteAt(ppTop,ppBottom,(*ppTop));
}
int DeleteBottom(NODE ** ppTop,NODE ** ppBottom)
{
return DeleteAt(ppTop,ppBottom,(*ppBottom));
}
void PrintLinkedListData(NODE * pStart,void (* PrintFunc)(void *))
{
while (pStart)
{
PrintFunc(pStart->m_pData);
pStart = pStart->m_pNext;
}
}
NODE * SearchLinkedListData(NODE * pStart,void * pData,int (* CompareFunc)(void *,void *))
{
while (pStart)
{
if (CompareFunc(pStart->m_pData,pData))
{
return pStart;
}
pStart = pStart->m_pNext;
}
return NULL;
}
'Study' 카테고리의 다른 글
queue(double linked list로 구현) (0) | 2008.12.16 |
---|---|
stack (double linked list로 구현) (0) | 2008.12.16 |
malloc 사용 예 (0) | 2008.12.16 |
함수 포인터 사용 예 (0) | 2008.12.16 |
gray code & binary code (0) | 2008.12.15 |