//结构体的定义
struct Node
{Node(const DataType& d):_next(NULL),_prev(NULL),_data(d){}
public:DataType _data;Node* _prev;Node* _next;
};
//双向链表类的定义
class DouList
{friend ostream& operator<<(ostream& os, DouList& l);
public:DouList()//构造函数:_head(NULL),_tail(NULL){}DouList(const DouList& l)//拷贝构造{if (l._head == NULL){return;}Node *cur = l._head;Node *newNode = new Node(cur->_data);_head = newNode;_tail = _head;cur = cur->_next;while (cur){newNode = new Node(cur->_data);_tail->_next = newNode;newNode->_prev = _tail;_tail = newNode;cur = cur->_next;}}DouList& operator=(DouList l)//=运算符的重载{swap(_head,l._head);swap(_tail, l._tail);return *this;}~DouList()//析构函数{Node* cur = _head;while (cur){Node* del = cur;cur = cur->_next;delete del;}_head = NULL;_tail = NULL;}public:void PushBack(const DataType& d);//后插void PopBack();//后出void PushFront(const DataType& d);//前插void PopFront();//前出Node* Find(const DataType& d);//查找void Insert(Node* pos, const DataType& d);//在指定位置插入void BubbSort();//冒泡排序void Reverse();//逆序void Remove(const DataType& d);//删除指定的元素void RemoveAll(const DataType& d);//删除所有指定的元素void Erase(Node* pos);//删除指定的节点
private:Node* _head;//头指针Node* _tail;//尾指针
};ostream& operator<<(ostream& os, DouList& l)//输出运算符的重载
{Node* cur = l._head;while (cur){os << cur->_data << "<=>";cur = cur->_next;}cout << "over";return os;
}void DouList::PushBack(const DataType& d)
{Node* newNode = new Node(d);if (_head == NULL){_head = newNode;_tail = _head;}else{_tail->_next = newNode;newNode->_prev = _tail;_tail = newNode;}
}void DouList::PopBack()
{if (_head == NULL){return;}if (_head == _tail){delete _head;_head = NULL;_tail = NULL;return;}Node* del = _tail;_tail = _tail->_prev;_tail->_next = NULL;delete del;
}void DouList::PushFront(const DataType& d)
{Node* newNode = new Node(d);if (_head == NULL){_head = newNode;_tail = _head;}else{newNode->_next = _head;_head->_prev = newNode;_head = newNode;}}void DouList::PopFront()
{if (_head == NULL){return;}if (_head == _tail){delete _head;_head = NULL;_tail = NULL;return;}Node* del = _head;_head = _head->_next;_head->_prev = NULL;delete del;
}Node* DouList::Find(const DataType& d)
{Node* cur = _head;while (cur){if (cur->_data == d){return cur;}cur = cur->_next;}return NULL;
}void DouList::Insert(Node* pos, const DataType& d)
{if (pos == NULL){return;}Node* newNode = new Node(d);if (pos != _tail)//非尾节点插入{newNode->_next = pos->_next;newNode->_prev = pos;pos->_next = newNode;}else//尾节点插入{_tail->_next = newNode;newNode->_prev = _tail;_tail = newNode;}
}void DouList::BubbSort()
{Node* cur = _head;Node* end = NULL;bool flag = true;while (cur->_next != end && flag){flag = false;while (cur->_next != end && cur){if (cur->_data > cur->_next->_data){flag = true;swap(cur->_data, cur->_next->_data);}cur = cur->_next;}end = cur;cur = _head;}
}void DouList::Reverse()
{Node* cur = _head;Node* newHead = NULL;Node* tmp = NULL;while (cur){tmp = cur->_next;cur->_next = newHead;if (newHead){newHead->_prev = cur;}newHead = cur;newHead->_prev = NULL;cur = tmp;}swap(_head, _tail);
}void DouList::Remove(const DataType& d)
{if (_head == NULL)//无节点{return;}Node* cur = _head;while (cur){if (cur->_data == d){if (cur == _head)//第一个相等{PopFront();return;}else if (cur == _tail&& _head != _tail)//最后一个节点相等{PopBack();return;}else//其他节点{cur->_prev->_next = cur->_next;cur->_next->_prev = cur->_prev;delete cur;return;}}cur = cur->_next;}
}void DouList::RemoveAll(const DataType& d)
{Node* cur = _head;bool flag = true;while (cur){flag = true;if (cur->_data == d){flag = false;cur = cur->_next;Remove(d);}if (flag){cur = cur->_next;}}
}void DouList::Erase(Node* pos)
{if (pos == NULL)//{return;}if (pos == _head)//删除第一个节点{PopFront();return;}if (pos == _tail)//删除最后一个节点{PopBack();return;}//非尾节点的删除pos->_next->_prev = pos->_prev;pos->_prev->_next = pos->_next;
}

转载于:https://blog.51cto.com/10788311/1751894

c++实现双向链表的常用功能相关推荐

  1. nginx介绍及常用功能

    什么是nginx nginx跟Apache一样,是一个web服务器(网站服务器),通过HTTP协议提供各种网络服务. Apache:重量级的,不支持高并发的服务器.在Apache上运行数以万计的并发访 ...

  2. Photoshop抠图、污点处理等常用功能及快捷键

    Photoshop抠图.污点处理等常用功能及快捷键 局部图片获取 图片污点修护 各种乱七八糟的常用东西 常用快捷键 常用功能的查看与设置 局部图片获取 a.钢笔工具创建路径后改为选区 b.选区工具3连 ...

  3. SAP MM不常用功能之供应商Rebate

    SAP MM不常用功能之供应商Rebate 采购中的返利流程 返利协议是卖方和买方之间签订的合同,在供应特定价值的物料时给予一定百分比的折扣. 下面是SAP系统中实现该业务场景的具体步骤. Step ...

  4. SAP QM 不常用功能点之 Physical-Sample Record

    SAP QM 不常用功能点之 Physical-Sample Record A record that contains all master data and transaction data fo ...

  5. 大白菜软件常用功能介绍

    大白菜软件常用功能介绍 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.刻录光盘介绍 1.选择你需要的刻录的光盘 2.点击刻录光盘 3.确认之后点击刻录 4.等待刻录完成 转载于 ...

  6. [转]WebPack 常用功能介绍

    概述 Webpack是一款用户打包前端模块的工具.主要是用来打包在浏览器端使用的javascript的.同时也能转换.捆绑.打包其他的静态资源,包括css.image.font file.templa ...

  7. 达梦数据库操作记录_达梦数据库常用功能及命令记录 -- 持续更新

    达梦数据库常用功能及命令记录 -- 持续更新 达梦数据库常用功能及命令记录 达梦数据库语句的使用总体来说跟 oracle 很接近的, 这篇文章主要是把常用的情况和语句做了记录, 并且后续还会不断的持续 ...

  8. Word的常用功能设置(

    Word的常用功能设置(20181229) 一.Word中中文输入为宋体,英文输入为Times New Roman实现自动切换 二.打造常用的word模板 三.word中的行间距问题 一.Word中中 ...

  9. OOnbsp;ALV常用功能完整简例(热键单击…

    OO ALV 常用功能简介 目录 OO ALV 常用功能简介... 1 一.所用类:... 2 二.效果图:... 2 单击... 2 双击... 2 F4帮助... 3 编辑某单元格并校验... 3 ...

  10. 从零开始学习jQuery (十) jQueryUI常用功能实战

    本系列文章导航 从零开始学习jQuery (一) 开天辟地入门篇 从零开始学习jQuery (二) 万能的选择器 从零开始学习jQuery (三) 管理jQuery包装集 从零开始学习jQuery ( ...

最新文章

  1. OSChina 周一乱弹 —— 六天颓废一天看剧
  2. python url请求
  3. Linux之chattr命令详解
  4. python实现简单的api接口-Python实现简单的API接口
  5. 深蓝学院《从零开始手写VIO》作业四
  6. 机器学习理论入门:第二章 经典监督学习算法-决策树
  7. WPF控件自定义样式(FasControls)
  8. 培养创造力的10个注意点
  9. c语言 while(cin),C++中while(cina)是什么意思
  10. Ubuntu快速配置指南
  11. OpenLayers 在Vue中增删改
  12. ubuntu 下openkore的运行
  13. git clone 的--single-branch和--depth参数
  14. 火影_青鸟_中日罗马音
  15. 如何在输入特殊符号,例如角度“∠”
  16. android 8.0 edittext,android – Autofill框架更新8.1导致EditText崩溃
  17. 海外网红KOL营销的合作方式
  18. PB 中获取时间的方法
  19. nasa注册_“NASA中文”更名的情况说明
  20. openvz学习笔记

热门文章

  1. VS2010开发环境之使用技巧
  2. 碰撞域与广播域的区别
  3. 混淆矩阵简介与Python实现
  4. 基于python sklearn的 SVM支持向量机 类实现
  5. 【LeetCode】【数组】题号:*189,旋转数组
  6. AttributeError lxml.etree Element object has no attribute get_attribute
  7. (转载)形态学腐蚀、膨胀——Matlab DIP(瓦)ch9形态学图像处理
  8. envi 打开影像报错:‘HISTOGRAM:illegal binsize or max/min‘.The result maybe invalid
  9. 实习成长之路:MySQL十二:为什么我删除了表的不少数据,但是大小没变呢?
  10. 数据结构-01-链表数据结构之单链表