双向链表 只使用一个指针
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
相关文章推荐
- 使用一个指针实现双向链表
- 写程序。设ha和hb分别是两个带头结点的非递减有序单链表的头指针,试设计算法,将这两个有序链表合并成一个非递增有序的单链表。要求使用原链表空间,
- 【算法之链表(三)】单链表中,在仅允许使用一个指针的情况下,在指定的节点前面插入以及删除一个节点
- 一个双向链表的单指针实现
- 算法导论10.2-8-用一个整数地址替代前后指针实现双向链表
- 13 在O1的时间删除链表节点-使用一个节点指针
- 使用单指针域实现双向链表
- C++使用单指针Struct实现双向链表
- 使用LINUX C实现一个链表,要求:链表节点构成:姓名、分数、下一个节点指针...
- 一个指针实现双向链表
- 实现一个双向链表
- 内核链表实现分析与使用(双向环形链表)
- 编写一个linux内核模块,用以创建、增加、删除、和遍历一个双向链表
- c语言双向循环链表实现-使用内核链表
- 使用 this 指针和 prototype 实现 js 的 OO 时的一个区别
- 输入一个单向链表,输出该链表中倒数第 k 个结点。链表的倒数第0 个结点为链表的尾指针。
- 在一个无头指针的单链表中,删除指针p所指向的结点
- 给定链表的头指针和一个结点指针,在O(1)时间删除该结点。
- 输入一个单向链表,输出该链表中倒数第k 个结点。链表的倒数第0个结点为链表的尾指针。
- 复制一个带random指针的链表