C++之模板实现list
来源:互联网 发布:邮箱注册淘宝流程 编辑:程序博客网 时间:2024/06/06 01:26
#include <iostream>#include <assert.h>using namespace std;template<typename T>struct ListNode{ ListNode(const T& data = T()) : _pNext(NULL) , _pPre(NULL) , _data(data) {} ListNode* _pNext; ListNode* _pPre; T _data;};template <typename T>class List{ typedef ListNode<T> Node; typedef Node* PNode;public: List() { CreateListHead(); } List(T* array, size_t size) { CreateListHead(); for (size_t i = 0; i < size; i++) { PushBack(array[i]); } } List(const List<T>& l) { _pHead = l._pHead; PNode pcur = l._pHead; while (pcur->_pNext != l._pHead) { PushBack(pcur->_pNext->_data); pcur = pcur->_pNext; } } List<T>& operator=(const List<T>& l) { if (this != &l) { this->Clear(); PNode pcur = l._pHead->_pNext; while (pcur != l._pHead) { PushBack(pcur->_data); pcur = pcur->_pNext; } } return *this; } ~List() { Clear(); delete _pHead; _pHead = NULL; } /////////////////////////////////////////////// void PushBack(const T& data) { PNode ptail = new Node(data); _pHead->_pPre->_pNext = ptail; ptail->_pNext = _pHead; ptail->_pPre = _pHead->_pPre; _pHead->_pPre = ptail; } void PopBack() { if (Empty()) return; PNode ptail = _pHead->_pPre; ptail->_pPre->_pNext = _pHead; _pHead->_pPre = ptail->_pPre; delete ptail; } void PushFront(const T& data) { PNode pNewnode = new Node(data); pNewnode->_pNext = _pHead->_pNext; pNewnode->_pPre = _pHead; _pHead->_pNext->_pPre = pNewnode; _pHead->_pNext = pNewnode; } void PopFront() { if (Empty()) return; PNode pcur = _pHead->_pNext; _pHead->_pNext = _pHead->_pNext->_pNext; _pHead->_pNext->_pPre = _pHead; delete pcur; } PNode Insert(PNode pos, const T& data) { assert(pos); PNode pNewnode = new Node(data); PNode p1 = pos; pNewnode->_pNext = p1; pNewnode->_pPre = p1->_pPre; p1->_pPre->_pNext = pNewnode; p1->_pPre = pNewnode; } PNode Erase(PNode pos) { assert(pos); PNode p2 = pos; p2->_pPre->_pNext = p2->_pNext; p2->_pNext->_pPre = p2->_pPre; delete p2; } size_t Size() { size_t count = 0; PNode pcur = _pHead; while (pcur->_pNext != _pHead) { count++; pcur = pcur->_pNext; } return count; } // 将链表中的结点清空 void Clear() { PNode pcur = _pHead->_pNext; while (pcur != _pHead) { PNode _next = pcur->_pNext; delete pcur; pcur = _next; } _pHead->_pNext = _pHead; _pHead->_pPre = _pHead; } bool Empty()const { if (_pHead->_pNext == _pHead) return true; else return false; } void Print() { PNode pcur = _pHead->_pNext; while (pcur != _pHead) { cout << pcur->_data << " "; pcur = pcur->_pNext; } cout << endl; }private: void CreateListHead() { _pHead = new Node; _pHead->_pNext = _pHead; _pHead->_pPre = _pHead; }private: PNode _pHead;};void Test1()//尾部插入和删除 { //插入 List<int> l; l.PushBack(1); l.Print(); l.PushBack(2); l.Print(); l.PushBack(3); l.Print(); l.PushBack(4); l.Print(); //删除 l.PopBack(); l.Print(); l.PopBack(); l.Print(); l.PopBack(); l.Print();}int main(){ Test1(); system("pause"); return 0;}
阅读全文
0 0
- c++::用模板实现List
- C++之模板实现list
- 【c++】模板实现vector和list
- List 模板实现
- STL之list模板
- LeetCode 之 Rotate List — C 实现
- LeetCode 之 Sort List — C 实现
- STL 之 List 模板类实现与应用
- 模板类模拟实现List
- C++模板实现list,迭代器
- C语言实现《设计模式之禅》之模板模式
- list 的c实现
- C++::模拟实现List
- LeetCode 之 Remove Linked List Elements — C 实现
- LeetCode 之 Reverse Linked List — C 实现
- c/c++之模板方法实现归并排序
- c list之 merge()
- C++List模板类的使用
- android 开发利用 html5 JavascriptInterface 实现应用层与 native 互传参数并获取返回数据
- eclipse show view .project .classpath
- <Deep Learning>Step by Step
- tomcat配置文件server.xml详解
- 如何加快sql执行速度?
- C++之模板实现list
- 移动端js分页
- DNS(Domain Name System,域名系统)
- 蓝桥杯2015C语言A组赛题解析
- Android使用百度地图息屏30分钟后界面卡死
- 【腾讯TMQ】大数据时代如何提升研发效率
- 以root权限在pycharm 中运行python 文件
- python-reportlab饼图绘制
- Web Scraping with Python:使用 Selenium 给当前网页截屏