双向链表 只使用一个指针

来源:互联网 发布:风靡美国华人网络的菜 编辑:程序博客网 时间:2024/06/07 08:33
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 listif(  NULL == pHead ){pHead = pNode;pHead->ptr = NULL;}else{//find the cur list nodepPrevious = 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



0 0
原创粉丝点击