模板实现顺序表和循环双链表
2017-08-04 09:27
281 查看
1.模板实现顺序表
2.模板实现循环双向链表
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<iostream> #include<string> using namespace std; template<class T> class SeqList { public: SeqList() :_a(NULL) ,_size(0) ,_capacity(0) { } SeqList(const SeqList<T>& s) { _size=s._size ; _capacity=_size; _a=new T[_size]; for(size_t i=0;i<_size;i++) { _a[i]=s._a[i]; } } SeqList& operator=(SeqList<T> s) { swap(_a,s._a); swap(_size,s._size); swap(_capacity,s._size); return *this; } ~SeqList() { if(_a) { delete[] _a; } } void CheckCapacity() { if(_size==_capacity) { size_t newsize=_capacity ? 2*_capacity : 3 ; T* tmp=new T[newsize];//若为自定义类型,将其初始化 for(size_t i=0;i<_size;i++) { tmp[i]=_a[i];//string类会存在深浅拷贝问题,string //类的operator=,可解决浅拷贝带来的问题 } delete[] _a; _a=tmp; _capacity=newsize; } } void PushBack(const T& x) { CheckCapacity(); _a[_size++]=x; } void PopBack() { if(_size>0) { _size--; } } void Insert(size_t pos, const T& x) { CheckCapacity(); for(int end=(int)_size;end>=(int)pos;end--) { _a[end+1]=_a[end]; } _a[pos]=x; _size++; } void Erase(size_t pos) { for(size_t i=pos;i<_size;i++) { _a[i]=_a[i+1]; } _size--; } T& operator[](size_t pos) { return _a[pos]; } void Print() { for(size_t i=0;i<_size;i++) { cout<<_a[i]<<" " ; } cout<<endl; } private: T* _a; size_t _size; size_t _capacity; };
2.模板实现循环双向链表
#define _CRT_SECURE_NO_WARNINGS 1 #pragma once #include<iostream> #include<cassert> using namespace std; template<class T> struct ListNode { T _data; ListNode* _next; ListNode* _prev; ListNode(const T& x) :_data(x) ,_next(NULL) ,_prev(NULL) { } }; template<class T> class List { typedef ListNode<T> Node; public: List() { _head=new Node(0); _head->_next =_head; _head->_prev=_head; } List(const List<T>& l) { Node* cur=l._head->_next; _head=new Node(0); _head->_next =_head; _head->_prev=_head; while(cur!=l._head) { PushBack(cur->_data); cur=cur->_next ; } } List& operator=( List<T> l) { swap(_head,l._head); return *this; } ~List() { Node* cur=_head->_next; while(cur!=_head) { Node* del=cur; delete del; cur=cur->_next; } _head->_next=NULL; _head->_prev=NULL; delete _head; } public: void PushBack(const T& x) { Insert(_head,x); } void PopBack() { Erase(_head->_prev); } void PushFront(const T& x) { Insert(_head->_next,x); } void PopFront() { Erase(_head->_next); } void Insert(Node* pos, const T& x) { assert(pos); Node* prev=pos->_prev ; Node* tmp=new Node(x); tmp->_prev =prev; prev->_next =tmp; tmp->_next =pos; pos->_prev =tmp; } void Erase(Node* pos) { assert(!Empty()&& pos); Node* prev=pos->_prev ; Node* next=pos->_next ; delete pos; prev->_next =next; next->_prev =prev; } Node* Find(const T& x) { Node* cur=_head->_next ; while(cur!=_head) { if(cur->_data==x) { return cur; } cur=cur->_next ; } return NULL; } void Print() { Node* cur=_head->_next; while(cur!=_head) { cout<<cur->_data<<" "; cur=cur->_next ; } cout<<endl; } T& Back() { assert(!Empty()); return _head->_next->_data; } bool Empty() { return _head==_head->_next; } private: Node* _head; };
相关文章推荐
- 编写一个程序实现循环双链表的各种基本运算(假设顺序表的元数基本类型为Char)
- C++模板及用模板实现双向循环链表,顺序表
- 基于数组的循环队列(C++模板实现)
- 数据结构之循环双链表的结构和实现
- 【C++】模板实现带头节点的双向循环链表
- C++使用模板实现顺序表
- 模板顺序表实现
- 循环双链表的基本操作实现
- 使用顺序表实现一个循环队列
- 循环双链表的实现
- C++用模板实现顺序表和栈
- 【数据结构】-线性表-顺序表 熟练度max=5(用一个变量实现min和循环变量i的功能)
- C++类模板实现循环队列
- 顺序表C++模板实现
- 循环双链表的C++实现
- c语言之————有头循环双链表实现队列存储
- C++类模板 实现循环队列的顺序存储结构算法 《数据结构》(北京科海) 部分摘抄 自己编写实现
- 用模板实现顺序表
- 用javascript语言在Velocity模板下实现图片循环显示
- 数据结构之循环双链表的结构和实现