struct Node { int Data; struct Node* prior; struct Node* next; }; /** * @brief 该函数实现了删除带头结点双链表中第i个结点 * @param[in] head 待删除结点链表 * @param[in] i 待删除结点位置 * @param[out] e 删除结点内容 * @notice 带头结点的双链表中第一个元素为头结点后的元素,所以不存在删除头结点的情况 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-03-09 */ int DelDoubleList(Node* head, const int i, int& e) { assert(head); if (head->next == NULL) { return -1; } Node* p = head; int j = 0; // 寻找待删除结点 while ((p->next != NULL) && (j < i)) { p = p->next; j++; } // 处理删除最后一个结点的情况 if (p->next == NULL) { e = p->Data; p->prior->next = NULL; delete p; p = NULL; return 0; } else { e = p->Data; p->next->prior = p->prior; p->prior->next = p->next; delete p; p = NULL; return 0; } } /** * @brief 该函数实现了删除带头结点双链表中与给定值相等的第一个结点 * @param[in] head 待删除结点链表 * @param[in] data 待删除结点元素值 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-03-09 */ int DelDoubleList(Node* head, int data) { assert(head); if (head->next == NULL) { return -1; } Node* prior = head; Node* q = head->next; // 寻找待删除结点 while((q != NULL) && (q->Data != data)) { q = q->next; } // 删除元素 if ((q != NULL) && (q->Data == data)) { // 处理删除最后一个结点的情况 if (q->next == NULL) { q->prior->next = NULL; delete q; q = NULL; return 0; } else { q->next->prior = q->prior; q->prior->next = q->next; delete q; q = NULL; return 0; } } else { cout<<"Could not find data!"<<endl; return -1; } }
posted on 2009-03-09 21:11 张云临 阅读(229) 评论(0) 编辑 收藏 举报