动态顺序表 与 双向链表的模板类
2015-12-29 16:52
435 查看
//////////////////////////////////////////////////////////////////////// /////////////////////泛型编程之动态顺序表的模板///////////////////////// //////////////////////////////////////////////////////////////////////// #include<iostream> #include<string> using namespace std; template<typename Type> //建立动态顺序表类模板 class Vector { public: Vector() :_head(NULL), _size(0),_capacity(0) {} Vector(const Vector<Type>& vector) :_head(NULL), _size(0), _capacity(0) { _head = new Type[vector._size]; //memcpy(_head, vector._head, sizeof(Type)*vector._size); (错误的写法) //当Type 是自定义类型时,可能会造成的浅拷贝,从而造成程序崩溃 for (int i = 0; i < vector._size; i++) { _head[i] = vector._head[i]; } _size = vector._size; _capacity = vector._capacity; } Vector<Type>& operator = (const Vector<Type>& vector) { if (this != &vector) { if (_head != NULL) { delete[] _head; _size = 0; _capacity = 0; } _head = new Type[vector._size]; for (int i = 0; i < vector._size; i++) { _head[i] = vector._head[i]; } _size = vector._size; _capacity = vector._capacity; } return *this; } ~Vector() { if (_head != NULL) { delete[] _head; } _size = 0; _capacity = 0; } public: void PushBack(Type x) { _CheckCapacity(); _head[_size++] = x; } void Print() { cout << "顺序表:" << endl; for (int i = 0; i < _size; i++) { cout << _head[i] << " "; } cout << endl << endl; } protected: void _CheckCapacity() { if (_size >= _capacity) { Type *tmp = _head; _head = new Type[2 * _capacity + 3]; //memcpy(_head, vector._head, sizeof(Type)*vector._size); (错误的写法) //当Type 是自定义类型时,可能会造成的浅拷贝,从而造成野指针问题,析构时就会出现问题 for (int i = 0; i < _size; i++) { _head[i] = tmp[i]; } delete[] tmp; _capacity = 2 * _capacity + 3; } } protected: Type *_head; size_t _size; size_t _capacity; }; void Test_string() { Vector<string> vector; vector.PushBack("aaaaa"); vector.PushBack("bbbbb"); vector.PushBack("ccccc"); vector.PushBack("ddddd"); vector.PushBack("eeeee"); Vector<string> vector_2(vector); Vector<string> vector_3; vector_3 = vector_2; vector_3.Print(); } void Test_double() { Vector<double> vector; vector.PushBack(1.1); vector.PushBack(2.2); vector.PushBack(3.3); vector.PushBack(4.4); vector.PushBack(5.5); Vector<double> vector_2(vector); Vector<double> vector_3; vector_3 = vector_2; vector_3.Print(); } void Test_char() { Vector<char> vector; vector.PushBack('a'); vector.PushBack('b'); vector.PushBack('c'); vector.PushBack('d'); vector.PushBack('e'); Vector<char> vector_2(vector); Vector<char> vector_3; vector_3 = vector_2; vector_3.Print(); } void main() { Test_string(); Test_double(); Test_char(); } ////////////////////////////////////////////////////////////////////////// ///////////////////////泛型编程之动态双向链表的模板/////////////////////// ////////////////////////////////////////////////////////////////////////// #include<iostream> #include<string> using namespace std; template<typename Type> struct ListNode { ListNode(Type x = 0) :_date(x), _next(NULL), _prev(NULL) { } Type _date; ListNode<Type> *_next; ListNode<Type> *_prev; }; template<typename Type> class List { public: List() :_head(NULL), _tail(NULL) {} List(const List<Type>& list) :_head(NULL), _tail(NULL) { ListNode<Type> *tmp = list._head; while (tmp) { PushBack(tmp->_date); tmp = tmp->_next; } } List<Type> & operator = (List<Type> list) { if (this != &list) { swap(_head, list._head); swap(_tail, list._tail); } return *this; } ~List() { if (_head != NULL) { while (_head) { ListNode<Type> *tmp = _head; _head = _head->_next; delete tmp; } } _head = NULL; _tail = NULL; } public: void PushBack(Type x) { ListNode<Type> *tmp = new ListNode<Type>(x); if (_head == NULL) { _head = _tail = tmp; } else { _tail->_next = tmp; tmp->_prev = _tail; _tail = _tail->_next; } } void Print() { cout << "单链表为:" << endl; ListNode<Type> *tmp = _head; while (tmp) { cout << tmp->_date << "->"; tmp = tmp->_next; } cout << "NULL\n" <<endl; } protected: ListNode<Type> *_head; ListNode<Type> *_tail; }; void Test_int() { List<int> list; list.PushBack(1); list.PushBack(2); list.PushBack(3); list.PushBack(4); list.PushBack(5); list.PushBack(6); list.Print(); } void Test_char() { List<char> list; list.PushBack('a'); list.PushBack('b'); list.PushBack('c'); list.PushBack('d'); list.PushBack('e'); list.PushBack('f'); list.Print(); } void Test_string() { List<string> list; list.PushBack("aaaaa"); list.PushBack("bbbbb"); list.PushBack("ccccc"); list.PushBack("dddddd"); list.PushBack("eeeeee); list.PushBack("ffffff"); list.Print(); } void Test_1() { List<string> list; list.PushBack("aaaaa"); list.PushBack("bbbbbb"); list.PushBack("cccccc"); List<string> list_2(list); List<string> list_3; list_3 = list_2; list.Print(); list_2.Print(); list_3.Print(); } void main() { Test_int(); Test_char(); Test_string(); Test_1(); }
相关文章推荐
- ASP编程入门进阶(十三):Ad & Content Rotator
- AnyDVD & AnyDVD HD v6.1.3.3 Final 多国语言版 下载
- prototype 1.5 & scriptaculous 1.6.1 学习笔记
- JoinChar 向地址中加入 ? 或 & 用于实现传参
- Javascript & DHTML 实例编程(教程)(四)初级实例篇2―动画
- Javascript & DHTML 实例编程(教程)DOM基础和基本API
- 详解java中&和&&的区别
- 探讨Oracle中的&号问题
- Javascript & DHTML 实例编程(教程)基础知识
- Javascript & DHTML 实例编程(教程)(三)初级实例篇1―上传文件控件实例
- ASP编程入门进阶(十四):Browser & Linking
- java数据结构之实现双向链表的示例
- C#双向链表LinkedList排序实现方法
- C语言实现双向链表
- PHP小教程之实现双向链表
- java中使用双向链表实现贪吃蛇程序源码分享
- 数据结构(2)-双向链表的实现
- 通用双向链表(一)——接口设计
- 通用双向链表(二)———接口实现
- Linux网络编程:原始套接字的魔力【上】