vector
#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
class Vector
{
public:
Vector()
:_data(new T[3])
,_size(0)
,_capacity(3)
{}
Vector(size_t n, const T& data)
:_data(new T[n])
{
for(size_t i=0; i<n; i++)
{
_data[i] = data;
}
_size = n;
_capacity = n;
}
Vector(const Vector& d)
:_data(new T[d._capacity])
{
for(size_t i=0; i<d._size; i++)
{
_data[i] = d._data[i];
}
_size = d._size;
_capacity = d._capacity;
}
Vector& operator=(const Vector& d)
{
if(_data != d._data)
{
Vector tmp(d);
swap(d._data, _data);
}
return *this;
}
~Vector()
{
delete[] _data;
_data = NULL;
_size = 0;
_capacity = 0;
}
void Swap(Vector& v)
{
std::swap(_pData, v._pData);
std::swap(_capacity, v._capacity);
std::swap(_size, v._size);
}
void PushBack(const T data)
{
CheckCapacity();
_data[_size++] = data;
}
void PopBack()
{
assert(_size>0);
_size--;
}
void Insert(size_t pos, const T& data)
{
assert(pos<_size);
CheckCapacity();
int tmp =(int)_size;
while ((int)pos <= tmp)
{
_data[tmp] = _data[tmp-1];
tmp--;
}
_data[pos] = data;
++_size;
}
void Erase(size_t pos)
{
assert(pos < _size);
size_t tmp = pos;
while (tmp < _size)
{
_data[tmp] = _data[tmp+1];
tmp++;
}
_size--;
}
void Assign(size_t n, const T& data)
{
assert(n>0);
if(n<=_capacity)
{
size_t tmp = 0;
while (tmp<n)
{
_data[tmp] = data;
tmp++;
}
}
else
{
delete[]_data;
_data = new T[n];
size_t tmp = 0;
while (tmp<n)
{
_data[tmp] = data;
tmp++;
}
_capacity = n;
}
_size = n;
}
void Clear()
{
_size = 0;
}
T& operator[](size_t index)
{
assert(index < _size);
return _data[index];
}
const T& operator[](size_t index)const
{
assert(index < _size);
return _data[index];
}
T& Front()
{
assert(_size>0);
return _data[0];
}
const T& Front()const
{
assert(_size>0);
return _data[0];
}
T& Back()
{
assert(_size>0);
return _data[_size-1];
}
const T& Back()const
{
assert(_size>0);
return _data[_size-1];
}
void PrintVector()
{
size_t i = 0;
while(i<_size)
{
cout<< _data[i]<<" " ;
++i;
}
cout<<"\n";
cout<<"_size:"<<_size<<endl;
cout<<"_capacity:"<<_capacity<<endl;
}
size_t Size()const
{
return _size;
}
size_t Capacity()const
{
return _capacity;
}
bool Empty()const
{
return !_size;
}
void ReSize(size_t sz, const T& data = T())
{
if(sz == _size)
{
return ;
}
if(sz <_size)
{
_size = sz;
}
else if(sz>_size && sz< _capacity)
{
size_t tmp = _size;
while(tmp<sz)
{
_data[tmp] = data;
tmp++;
}
_size = sz;
}else
{
T* tmp = new T[sz];
size_t i=0;
while(i<_size)
{
tmp[i]=_data[i];
i++;
}
while(i<sz)
{
tmp[i]=data;
i++;
}
delete[]_data;
_data = tmp;
_size = sz;
_capacity = sz;
}
}
void Reserve(size_t sz)
{
if(sz>_capacity)
{
T* tmp = new T[sz];
size_t i=0;
while(i<_size)
{
tmp[i]=_data[i];
i++;
}
delete[]_data;
_data = tmp;
_capacity = sz;
}
}
private:
void CheckCapacity()
{
if(_size == _capacity)
{
T* tmp = new T[_capacity+3];
assert(tmp);
for(size_t i=0; i<_size; i++)
{
tmp[i] = _data[i];
}
delete[] _data;
_data = tmp;
_capacity += 3;
}
}
private:
T* _data;
size_t _size;
size_t _capacity;
};
void test1()
{
Vector<int> d1;
d1.PushBack(1);
d1.PushBack(2);
d1.PushBack(3);
d1.PushBack(4);
d1.PopBack();
d1.PopBack();
d1.PopBack();
d1.PopBack();
Vector<char> d2;
d2.PushBack('a');
d2.PushBack('b');
d2.PushBack('c');
d2.PushBack('d');
d2.PopBack();
d2.PopBack();
d2.PopBack();
d2.PopBack();
}
void test2()
{
Vector<int> d3(5,1);
d3.PrintVector();
d3.Insert(0,0);
d3.PrintVector();
d3.Erase(0);
d3.PrintVector();
d3.ReSize(10,0);
d3.PrintVector();
d3.Reserve(15);
d3.PrintVector();
d3.Assign(20,5);
d3.PrintVector();
}
int main()
{
test2();
system("pause");
return 0;
}
list
#define _CRT_SECURE_NO_DEPRECATE 1
#include<iostream>
#include<assert.h>
using namespace std;
template<class T>
struct ListNode
{
ListNode(const T& data = T())
:_pre(NULL)
,_next(NULL)
,_data(data)
{
}
ListNode<T>* _pre;
ListNode<T>* _next;
T _data;
};
template<class T>
class ListItertor
{
typedef ListItertor<T> itor;
public:
ListItertor(ListNode<T>* p = NULL)
:_pNode(p)
{
}
itor& operator++()
{
_pNode = _pNode->_next;
return *this;
}
itor operator++(int)
{
itor temp(*this);
_pNode = _pNode->_next;
return temp;
}
itor& operator--()
{
_pNode = _pNode->_pre;
return *this;
}
itor operator--(int)
{
itor tmp(*this);
_pNode = _pNode->_pre;
return tmp;
}
bool operator==(const itor& d)
{
return _pNode == d._pNode;
}
bool operator!=(const itor& d)
{
return _pNode != d._pNode;
}
private:
ListNode<T>* _pNode;
};
template<class T>
class List
{
typedef ListItertor<T> itor;
public:
ListNode<T>* Begin()
{
return (_pHead->_next);
}
ListNode<T>* End()
{
return (_pHead);
}
List()
{
CreateHead();
}
List(size_t n, const T& data)
{
CreateHead();
while (n-->0)
{
Insert(End(),data);
}
}
List(const List<T>& L)
{
CreateHead();
ListNode<T>* tmp = L._pHead->_next;
while(tmp != L._pHead)
{
Insert(End(),tmp->_data);
tmp = tmp->_next;
}
}
void Swap(ListNode<T>** p1,ListNode<T>** p2)
{
ListNode<T>* tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
List& operator=(const List<T>& L)
{
if(_pHead != L._pHead)
{
List<T> tmp(L);
Swap(&_pHead,&(tmp._pHead));
}
return *this;
}
void Clear()
{
ListNode<T>* cur = Begin();
while(cur != End())
{
ListNode<T>* tmpNode = cur;
cur = cur->_next;
delete tmpNode;
tmpNode = NULL;
}
}
~List()
{
Clear();
delete _pHead;
_pHead = NULL;
}
void PushBack(const T& data)
{
Insert(End(),data);
}
void PopBack()
{
ListNode<T>* tmp = End();
Erase(tmp->_pre);
}
void PushFront(const T& data)
{
Insert(Begin(),data);
}
void PopFront()
{
Erase(Begin());
}
void Insert(ListNode<T>* pos, const T& data)
{
assert(pos);
ListNode<T>* pNewNode = new ListNode<T>(data);
pNewNode->_next = pos;
pos->_pre->_next = pNewNode;
pNewNode->_pre = pos->_pre;
pos->_pre = pNewNode;
}
void Erase(ListNode<T>* pos)
{
assert(pos);
pos->_next->_pre = pos->_pre;
pos->_pre->_next = pos->_next;
delete pos;
pos = NULL;
}
size_t Size()
{
size_t count = 0;
ListNode<T>* cur = _pHead->_next;
while(cur != End())
{
count++;
cur=cur->_next;
}
return count;
}
bool Empty()
{
if(_pHead->_next == _pHead->_pre)
return true;
return false;
}
void PrintList()
{
if(_pHead->_next != _pHead->_pre)
{
ListNode<T>* tmp = Begin();
while (tmp!=End())
{
cout<<tmp->_data<<" ";
tmp = tmp->_next;
}
cout<<endl;
}
}
private:
void CreateHead()
{
_pHead = new ListNode<T>();
_pHead->_next = _pHead;
_pHead->_pre = _pHead;
}
private:
ListNode<T>* _pHead;
};
void test1()
{
List<int> L1(2,5);
L1.PrintList();
List<int> L2(L1);
L2.PrintList();
List<int> L3;
L3.PushBack(6);
L3.PushBack(6);
L3.PrintList();
L3=L2;
L3.PrintList();
}
void test2()
{
List<int> L1;
L1.PushBack(1);
L1.PushBack(2);
L1.PushBack(3);
L1.PushBack(4);
L1.PushBack(5);
L1.PrintList();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.PopBack();
L1.PrintList();
L1.PushFront(6);
L1.PushFront(5);
L1.PushFront(4);
L1.PushFront(3);
L1.PushFront(2);
L1.PushFront(1);
L1.PrintList();
cout<<L1.Size()<<endl;
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PopFront();
L1.PrintList();
cout<<L1.Empty()<<endl;
}
int main()
{
test2();
system("pause");
return 0;
}