您的位置:首页 > 其它

双向链表 只使用一个指针

2014-03-24 16:25 253 查看
typedef struct
{
void*  ptr;
int value;
}DOUBLE_LINK_T;

DOUBLE_LINK_T*  insert(DOUBLE_LINK_T*  pHead,int value)
{
DOUBLE_LINK_T* pCur = NULL;
DOUBLE_LINK_T* pNext = NULL;
DOUBLE_LINK_T* pPrevious = NULL;
DOUBLE_LINK_T* pNode = NULL;
pNode = new DOUBLE_LINK_T;
pNode->value = value;

// empty double list
if(  NULL == pHead )
{
pHead = pNode;
pHead->ptr = NULL;
}
else
{
//find the cur list node
pPrevious = NULL;
pNext = (DOUBLE_LINK_T*) (NULL^(unsigned int)pHead->ptr);
if( NULL == pNext )
{
pHead->ptr = (void*)( NULL^(unsigned int)pNode );
pNode->ptr = (void*)( (unsigned int)pHead^NULL );
}
else
{
pPrevious = pHead;
do
{
pCur = pNext;
pNext = (DOUBLE_LINK_T*) ( (unsigned int)pPrevious^(unsigned int)pCur->ptr );
if( NULL != pNext)
pPrevious = pCur;
}
while( NULL != pNext );
pCur->ptr = (void*)((unsigned int)pPrevious^(unsigned int)pNode);
pNode->ptr = (void*)((unsigned int)pCur^NULL);
}
}
return pNode ;
}

int printAll(DOUBLE_LINK_T*  pHead)
{
DOUBLE_LINK_T* pCur = NULL;
DOUBLE_LINK_T* pNext = NULL;
DOUBLE_LINK_T* pPrevious = NULL;
int nDeep = 0;

if( NULL == pHead )
return -1;

pNext = (DOUBLE_LINK_T*) ( NULL^(unsigned int)pHead->ptr );
if( NULL == pNext )
{
printf(" value =%d \n",pHead->value);
}
else
{
pPrevious = pHead;
printf(" value =%d \n",pHead->value);
do
{
printf(" value =%d \n",pNext->value);
pCur = pNext;
pNext = (DOUBLE_LINK_T*) ( (unsigned int)pPrevious^(unsigned int)pCur->ptr );

if( NULL != pNext)
pPrevious = pCur;
}
while( NULL != pNext );
}

return 0;
}

void doublelist_test(void)
{
int i = 100;
DOUBLE_LINK_T*  pHead = NULL;
DOUBLE_LINK_T*  pTail = NULL;
pHead = insert(pHead,i++);
pTail = insert(pHead,i++);
pTail = insert(pHead,i++);
pTail = insert(pHead,i++);
pTail = insert(pHead,i++);
pTail = insert(pHead,i++);

printAll(pHead);
printf("<================================>\n");
printAll(pTail);

return  ;
}

int main(int argc,char** argv)
{
doublelist_test();
return 0;
}


原理: A^B^B = A^(B^B) = A
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐