模板详解可以点这里
#include <iostream>
#include <string>
#include <assert.h>
using namespace std;
template<class T>
class Vector
{
protected:
T* _first;
T* _finish;
T* _capacity;
public:
Vector()
:_first(NULL)
,_finish(NULL)
,_capacity(NULL)
{
;
}
~Vector()
{
delete[] _first;
_first = _finish = _capacity = NULL;
}
Vector(Vector<T>& v)
{
if (this != &v)
{
T* tmp = new T[v.Capacity()];
size_t i = 0;
for (i = 0; i < v.Size(); i++)
{
tmp[i] = v._first[i];
}
delete[] _first;
_first = tmp;
_finish = _first + v.Size();
_capacity = _first + v.Capacity();
}
}
Vector<T>& operator = ( Vector<T> &v)
{
if (this != &v)
{
Vector<T>tmp(v);
Swap(tmp);
}
return *this;
}
void Swap(Vector<T> &s)
{
swap(_first,s._first);
swap(_finish,s._finish);
swap(_capacity,s._capacity);
}
void PushBack(const T& x)
{
Insert(Size(), x);
}
void PopBack()
{
Erase(Size() - 1);
}
void PushFront(const T& x)
{
Insert(*_first, x);
}
void PopFront()
{
Erase(0);
}
void Expand(size_t n)
{
if (Empty())
{
_first = _finish = new T[4];
_capacity = _first + 4;
}
else
{
T* tmp = new T[n];
size_t size = Size();
memset(tmp, 0X00, sizeof(tmp)*n);
for (size_t i = 0; i < size; i++)
{
tmp[i] = _first[i];
}
delete[] _first;
_first = tmp;
_finish = _first + size;
_capacity = _first + n;
}
}
void Insert(size_t pos,const T&x)
{
assert(pos <= Size());
if (Size() >= Capacity())
{
Expand(2 * Capacity());
}
T* end = _finish - 1;
while (end > _first + pos - 1)
{
*(end + 1) = *end;
--end;
}
*(_first + pos) = x;
++_finish;
}
void Erase(size_t pos)
{
assert(pos < Size() && pos >= 0);
T* pos_next = _first + pos;
while (pos_next < _finish)
{
*(pos_next) = *(pos_next+1);
pos_next++;
}
--_finish;
}
size_t Find(const T& x)
{
T* start = _first;
int i = 0;
for (i = 0; i < _finish; i++)
{
if (x == *start)
{
return i;
}
}
return -1;
}
T& operator[](size_t pos)
{
assert(pos < Size() && pos >= 0);
return _first[pos];
}
const T& operator[](size_t pos) const
{
assert(pos < Size() && pos >= 0);
return _first[pos];
}
size_t Size()
{
return _finish - _first ;
}
size_t Capacity()
{
return _capacity - _first;
}
bool Empty()
{
if (!(_finish - _first))
{
return true;
}
else
{
return false;
}
}
void show()
{
T* cur = _first;
size_t i = 0;
for (i = 0; i < Size(); i++)
{
cout << *(cur+i) << " ";
}
cout << endl;
}
};
#include <iostream>
using namespace std;
template<class T>
struct ListNode
{
ListNode<T>*_next;
ListNode<T>* _prev;
T _data;
};
template<class T>
class List
{
protected:
Node* _head;
public:
typedef ListNode<T> Node;
List()
:_head(new Node)
{
_head->_next = _head;
_head->_prev = _head;
}
~List()
{
Clean();
delete _head;
_head = NULL;
}
void Clean()
{
Node* cur = _head->_next;
while (_head != cur)
{
Node* tmp = cur;
cur = cur->_next;
delete tmp;
}
_head->_next = _head;
_head->_prev = _head;
}
List(const List<T>& L)
:_head(new Node)
{
_head->_next = _head;
_head->_prev = _head;
Node* cur = L._head->_next;
while (L._head != cur)
{
this->PushBack(cur->_data);
cur = cur->_next;
}
}
List<T>& operator = (const List<T>& L)
{
List<T> tmp(L);
if (this != &L)
{
swap(_head,tmp._head);
}
return *this;
}
void PushBack(T x)
{
Node* tail = _head->_prev;
Node* NewNode = new Node;
NewNode->_data = x;
tail->_next = NewNode;
NewNode->_prev = tail;
_head->_prev = NewNode;
NewNode->_next = _head;
}
void PopBack()
{
Erase(_head->_prev);
}
void PushFront(T x)
{
Insert(_head->_next, x);
}
void PopFront()
{
Erase(_head->_next);
}
Node* Find(T x)
{
Node*cur = _head->_next;
while (_head != cur)
{
if (x == cur->_data)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void Insert(Node* pos, T x)
{
Node* NewNode = new Node;
NewNode->_data = x;
Node* PosPrev = pos->_prev;
NewNode->_next = pos;
pos->_prev = NewNode;
PosPrev->_next = NewNode;
NewNode->_prev = PosPrev;
}
void Erase(Node* pos)
{
Node* PosPrev = pos->_prev;
Node* PosNext = pos->_next;
delete pos;
pos = NULL;
PosPrev->_next = PosNext;
PosNext->_prev = PosPrev;
}
void Show()
{
Node* cur = _head->_next;
while (cur != _head)
{
cout << cur->_data<<" ";
cur = cur->_next;
}
cout << endl;
}
bool Empty()
{
if (_head->_next == _head)
{
return true;
}
else
{
return false;
}
}
};