C++模板类实现链表
来源:互联网 发布:宁夏干部网络培训登录 编辑:程序博客网 时间:2024/06/15 03:48
#pragma once#include <assert.h>template<class T>struct ListNode{ T _data; ListNode<T>* _next; ListNode<T>* _prev; ListNode(const T& x) :_data(x) ,_next(NULL) ,_prev(NULL) {}};template<class T>class List{ typedef ListNode<T> Node;public: List() :_head(NULL) ,_tail(NULL) {} // l2(l1) List(const List<T>& l) :_head(NULL) ,_tail(NULL) { Node* cur = l._head; while(cur) { this->PushBack(cur->_data); cur = cur->_next; } } // l1 = l2 List<int>& operator=(List<T> l) { swap(l._head, _head); swap(l._tail, _tail); return *this; } void Clear() { Node* cur = _head; while (cur) { Node* next = cur->_next; delete cur; cur = next; } _head = _tail = NULL; } ~List() { Clear(); } void PushBack(const T& x) { //1.空 //2.多个节点 if(_head == NULL) { _head = _tail = new Node(x); } else { Node* tmp = new Node(x); _tail->_next = tmp; tmp->_prev = _tail; _tail = tmp; } } void PopBack() { // 1.空 // 2.一个节点 // 3.多个节点 /* if(_head == NULL) { return; } else if (_head == _tail) { delete _head; _head = _tail = NULL; } else { Node* prev = _tail->_prev; delete _tail; _tail = prev; _tail->_next = NULL; }*/ Erase(_tail); } void PushFront(const T& x) { Insert(_head, x); } void PopFront() { Erase(_head); } const T& Front() { assert(_head); return _head->_data; } const T& Back() { assert(_tail); return _tail->_data; } Node* Find(const T& x) { Node* cur = _head; while(cur) { if (cur->_data == x) { return cur; } cur = cur->_next; } return NULL; } // pos的前面插入 // 设计一个双向链表,实现随机位置的插入和删除 // void Insert(Node* pos, const T& x) { assert(pos); Node* prev = pos->_prev; Node* next = pos; Node* cur = new Node(x); if (prev) { prev->_next = cur; cur->_prev = prev; } cur->_next = next; next->_prev = cur; } void Erase(Node* pos) { assert(pos); if (_head == NULL) { return; } else if (_head == _tail) { assert(pos == _head); delete _head; _head = _tail = NULL; } else if (pos->_prev == NULL) { Node* next = pos->_next; delete pos; _head = next; _head->_prev = NULL; } else if (pos->_next == NULL) { Node* prev = pos->_prev; delete pos; _tail = prev; _tail->_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) { cout<<cur->_data<<" "; cur = cur->_next; } cout<<endl; } bool Empty() { return _head == NULL; }protected: Node* _head; Node* _tail;};
测试代码如下:
void TestList1(){ List<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); l1.PopBack(); l1.PopBack(); l1.PopBack(); l1.Print();}// void TestList2(){ List<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); ListNode<int>* pos = l1.Find(3); l1.Insert(pos, 30); l1.Print(); pos = l1.Find(4); l1.Insert(pos, 40); l1.Erase(pos); l1.Print();}// void TestList3(){ List<int> l1; l1.PushBack(1); l1.PushBack(2); l1.PushBack(3); l1.PushBack(4); l1.Print(); List<int> l2(l1); l2.Print(); List<int> l3; l3.PushBack(4); l3.PushBack(5); l1 = l3; l1.Print();}
阅读全文
0 0
- [C++] 模板类实现简单链表
- [数据结构]链表的实现(c++/类模板)
- c++(模板类)实现顺序表
- 类模板 实现链表
- 类模板 实现链表
- <C/C++数据结构>双向链表(C++模板实现)
- 【C++】模板实现带头节点的双向循环链表
- [数据结构] 顺序表的实现(c++/类模板)
- C 链表模板
- 数据结构(c++)栈的模板类(使用链表实现)
- 【C++】用模板实现顺序表
- 【C++】用模板实现顺序表Vector
- C++primer 实现queue的模板类
- 【C++】实现STL模板类string
- C++:栈(stack)的模板类实现
- C++,链表类,链表模板类
- C/C++--模板类实现.h和.cpp分离
- 链表的实现(类模板)
- Kinect+OpenNI学习笔记(不需要骨骼跟踪的人体手部分割)
- Spring Security之自定义数据库表
- zookeeper基础知识
- 【算法】最短路径-弗洛伊德(Floyd-Warshall)
- 枚举单例模式
- C++模板类实现链表
- Android程序猿带你学python第6章--异常处理
- LPC54114双核使用指南翻译
- C#——面向对象——重载操作符——重载运算符
- python-正则匹配在python中使用小练习
- Nginx及在windows和Linux下搭建集群
- ZOJ 1048
- bmp2yuv
- HDMI协议解析