模板实现双向链表
来源:互联网 发布:excel多表格数据合计 编辑:程序博客网 时间:2024/05/21 05:41
c++模板实现双向链表:
#include <iostream>#include <assert.h>using namespace std;template<typename T>struct ListNode { T _data; ListNode<T>* _prev; ListNode<T>* _next; ListNode() :_next(NULL) , _prev(NULL) {}};template<typename T>class List{ typedef ListNode<T> Node;public: List() :_head(NULL) ,_tail(NULL) {} List<T>& operator=(List<T> list) { swap(_head, list._head); swap(_tail, list._tail); return *this; } List( const List<T>& s)// ????? :_tail(NULL) , _head(NULL) { Node* cur = s._head; while (cur) { PushBack(cur->_data); cur = cur->_next; } } void PushBack(const T& x)//1、空链表2、有多个元素 { if (_tail == NULL)//空链表 { _head = _tail = new Node; _tail->_data = x; _head->_next = NULL; _head->_prev = NULL; } else//多个元素 { Node* p = new Node; p->_data = x; _tail->_next = p; p->_prev = _tail; _tail = p; _head->_prev = NULL; _tail->_next = NULL; } } void PopBack()//1、空链表2、一个元素3、多个元素 { if (NULL == _head) { _head = new Node(x); } else if (_head == _tail)//一个结点 { delete _tail; _tail = _head = NULL; } else//多个结点 { Node* tmp = _tail->_prev; delete[] _tail; _tail = tmp; _tail ->_next= NULL; } } void PushFront(const T&x)//头插(1、空2、不空) { Node* newnode = new Node(x); if (_head == NULL) { newnode->_next = NULL; newnode->_prev = NULL; _head = _tail = newnode; } else { Node* tmp = _head; newnode->_next = tmp; tmp->_prev = newnode; _head = newnode; newnode->_prev = NULL; } } void PopFront() { Node* tmp = _head->_next; delete[] _head; _head = tmp; } void Insert(Node* pos, T x)//pos前面插 { assert(pos); Node* newnode = new Node(x); Node* tmp = pos->_prev;//保存pos位置前一个节点地址 if (tmp)//pos不是第一个节点 { tmp->_next = newnode;//将新节点和前一个连接 newnode->_prev = tmp; } else//pos是第一个节点 { _tail = newnode; newnode->_prev = NULL; } newnode->_next = pos; pos->_prev = newnode; } ~List()//析构函数 { Node* tmp = _head; Node* cur = NULL; while (tmp) { cur = tmp->_next; delete[] tmp; tmp = cur; } } Node* Find(const T &x) { Node* cur = _head; while (cur) { if (cur->_data == x) { return cur; } cur = cur->_next; } } void Erase(Node* pos)//1、空2、删头3、删尾4、删中间 { assert(pos); if (NULL == _head)//空 { return; } else if (_head == _tail)//一个元素 { assert(_head != pos); delete[] _head; _head = _tail = NULL; } else if (_head == pos)//pos为头 { Node* cur = _head->_next; delete[] _head; _head = cur; cur->_prev = NULL; } else if (_tail == pos) { Node* cur = _tail->_prev; delete[] _tail; _tail = cur; cur->_next = NULL; } else { Node* prev = pos->_prev; Node* next = pos->_next; delete[] pos; prev->_next = next; next->_prev = prev; } } void Print() { Node* cur = _head; while (cur != NULL) { cout << cur->_data<< endl; cur = cur->_next; } }private: Node* _tail; Node* _head;};int main(){ List<int> a; a.PushBack(1); a.PushBack(2); a.PushBack(3); a.PushBack(4); a.PushBack(5); /*a.Erase(a.Find(3)); a.Erase(a.Find(2)); a.Erase(a.Find(1));*/ List<int> b(a); b.PushBack(4); b.PushBack(5); b = a; b.Print(); system("pause"); return 0;}
1 0
- 模板实现双向链表
- 模板实现双向链表
- c++实现双向链表,类模板双向链表
- c++模板实现双向链表
- 双向链表List---类模板实现
- C++模板实现双向链表
- 双向链表模板
- 模板双向链表
- 【模板】双向链表
- C++ 学习练手 - 双向链表的模板实现
- 双向链表的C++类模板实现
- <C/C++数据结构>双向链表(C++模板实现)
- 【C++】模板实现带头节点的双向循环链表
- C++ 模板实现Vector和双向链表
- C++模板--------双向链表
- C++模板-双向链表
- 类模板---双向链表
- 双向循环链表模板
- 2017 Google hosts 持续更新
- 值类型和引用类型
- C++——二进制输出一个数以及输出double型位数过多情况
- 常用R语言包介绍
- C/C++——一个因为unsigned引发的大问题
- 模板实现双向链表
- python正则表达式系列(2)——re模块常用函数
- Spring 三级父子容器
- css
- P3376 【模板】网络最大流
- 学习淘淘商城第三十五课(Solr的安装)
- 安装mysql
- 如何将低版本的myeclipse改造成可以使用servlet3.0的神器
- yum仓库的安装配置