标签:
我在之前一篇博客《C语言实现非循环双链表节点的删除(不带头结点)》中详细讲解了不含头尾节点的双链表中删除一个节点,处理过程还是稍显麻烦。自从我们学习使用头尾节点来处理双链表后,删除过程就非常方便。代码上传至 https://github.com/chenyufeng1991/DeleteNodeDoubleLinkedList_HeadList 。
核心代码如下:
//删除pos位置的节点 int DeletePosList(Node *pHead,Node *pTail,int pos){ int i = 1; Node *pMove; pMove = pHead->next; while (pMove != pTail) { if (i == pos) { pMove->prior->next = pMove->next; pMove->next->prior = pMove->prior; free(pMove); pMove = NULL; printf("%s函数执行,删除pos=%d位置的节点成功\n",__FUNCTION__,pos); return 1; } i++; pMove = pMove->next; } printf("%s函数执行,删除pos=%d位置的节点失败\n",__FUNCTION__,pos); return 0; } //删除值为x的节点,若存在该节点则删除之 int DeleteValueList(Node *pHead,Node *pTail,int x){ Node *pMove; pMove = pHead->next; while (pMove != pTail) { if (pMove->element == x) { pMove->prior->next = pMove->next; pMove->next->prior = pMove->prior; free(pMove); pMove = NULL; printf("%s函数执行,删除值为x=%d的节点成功\n",__FUNCTION__,x); return 1; } pMove = pMove->next; } printf("%s函数执行,删除值为x的节点失败\n",__FUNCTION__); return 0; }
标签:
原文地址:http://blog.csdn.net/chenyufeng1991/article/details/50801798