双向链表元素的插入

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; } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值