struct Node { int Data; struct Node* prior; struct Node* next; }; /** * @brief 该函数实现了在带头结点双链表中第i个结点之前插入元素 * @param[in] head 待插入结点链表 * @param[in] i 待插入结点位置 * @param[in] e 待插入结点值 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-03-09 */ int InsertDoubleList(Node* head, const int i, const int e) { assert(head); if (head->next == NULL) { return -1; } // 寻找待插入结点位置 int j = 0; Node* p = head; while ((p->next != NULL) && (j < i)) { p = p->next; j++; } // 插入结点 Node* q = new Node; assert(q); q->Data = e; q->prior = p->prior; p->prior->next = q; q->next = p; p->prior = q; return 0; } /** * @brief 该函数实现了在带头结点双链表中与给定值相等的第一个结点前插入结点 * @param[in] head 待插入结点链表 * @param[in] e 待插入结点的位置 * @param[in] data 待插入结点值 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-03-09 */ int InsertDoubleList1(Node* head, const int e, const int data) { assert(head); if (head->next == NULL) { return -1; } // 寻找待插入结点位置 Node* p = head->next; while ((p != NULL) && (p->Data != e)) { p = p->next; } // 插入结点 if((p != NULL) && (p->Data == e) ) { Node* q = new Node; assert(q); q->Data = e; q->prior = p->prior; p->prior->next = q; q->next = p; p->prior = q; return 0; } else { cout<<"Could not find data!"<<endl; return -1; } }
posted on 2009-03-09 21:15 张云临 阅读(491) 评论(0) 编辑 收藏 举报