双向链表中每个节点结构如下

代码实现
#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
struct Node
{
Node(const DataType& data)
: _pNext(NULL)
, _pPre(NULL)
, _data(data)
{
}
Node* _pNext;
Node* _pPre;
DataType _data;
};
class List
{
public:
List()
: _pHead(NULL)
{}
List(size_t n, DataType data)
{
while (n--)
{
PushBack(data);
}
}
void PushBack(const DataType& data)
{
if(_pHead == NULL)
{
_pHead = BuyNode(data);
_tail = _pHead;
}
else
{
Node* tmp = _tail;
_tail->_pNext = BuyNode(data);
_tail = _tail->_pNext;
_tail->_pPre = tmp;
}
}
void PopBack()
{
if(_pHead == NULL)
{
return ;
}
else if (_pHead->_pNext == NULL)
{
delete _pHead;
_pHead = _tail = NULL;
}
else
{
Node* tmp = _tail;
_tail = _tail->_pPre;
delete tmp;
_tail->_pNext = NULL;
}
}
void PushFront(const DataType& data)
{
if(_pHead == NULL)
{
_pHead = BuyNode(data);
_tail = _pHead;
}
else
{
Node* tmp = BuyNode(data);
tmp->_pNext = _pHead;
tmp->_pPre = NULL;
_pHead->_pPre = tmp;
_pHead = tmp;
}
}
void PopFront()
{
if(_tail == NULL)
{
return ;
}
else if (_tail->_pPre == NULL)
{
delete _tail;
_pHead = _tail = NULL;
}
else
{
Node* tmp = _pHead;
_pHead = _pHead->_pNext;
delete tmp;
_pHead->_pPre = NULL;
}
}
Node* Find(const DataType& data)
{
Node* cur=_pHead;
while (cur)
{
if(cur->_data==data)
{
return cur;
}
cur = cur->_pNext;
}
}
void Insert(Node* pos, const DataType& data)
{
assert(pos);
if (pos==_pHead)
{
PushFront(data);
}
else
{
Node* tmp = BuyNode(data);
pos->_pPre->_pNext=tmp;
tmp->_pNext = pos;
tmp->_pPre = pos->_pPre;
pos->_pPre = tmp;
}
}
void Erase(Node* pos)
{
assert(pos);
if (pos==_pHead)
{
Node* tmp=_pHead->_pNext;
delete _pHead;
_pHead = tmp;
tmp->_pPre = NULL;
}
else if (pos==_tail)
{
Node* tmp = _tail->_pPre;
delete _tail;
_tail = tmp;
tmp->_pNext = NULL;
}
else
{
Node* next = pos->_pNext;
Node* pre = pos->_pPre;
delete pos;
pre->_pNext = next;
next->_pPre = pre;
}
}
size_t Size()
{
if (_pHead==NULL)
{
return 0;
}
size_t count=0;
Node* cur = _pHead;
while (cur != NULL)
{
cur = cur->_pNext;
++count;
}
return count;
}
bool Empty()const
{
if (_pHead==_tail==NULL)
{
return true;
}
return false;
}
List(const List& l)
:_pHead(NULL)
, _tail(NULL)
{
Node* tmp=l._pHead;
while(tmp)
{
PushBack(tmp->_data);
tmp = tmp->_pNext;
}
}
List& operator=(const List& l)
{
if (this != &l)
{
List tmp(l);
Swap(tmp);
}
return *this;
}
~List()
{
Node* cur = _pHead;
while (cur)
{
Node* tmp=cur;
cur = cur->_pNext;
delete tmp;
}
}
void Swap(List& l)
{
swap(_pHead, l._pHead);
swap(_tail, l._tail);
}
void Display()const
{
if (_pHead == NULL)
{
printf("NULL");
}
Node* cur = _pHead;
while (cur)
{
cout << cur->_data << "->";
cur = cur->_pNext;
}
cout <<endl;
Node* tmp = _tail;
while (tmp)
{
cout<< tmp->_data << "->";
tmp = tmp->_pPre;
}
cout <<endl;
}
private:
Node* BuyNode(const DataType& data)
{
return new Node(data);
}
private:
Node* _pHead;
Node* _tail;
};
void test1()
{
List l1;
l1.PushBack(1);
l1.PushBack(2);
l1.PushBack(3);
l1.PushBack(4);
l1.Display();
l1.PopBack();
l1.PopBack();
l1.PopBack();
l1.Display();
}
void test2()
{
List l2;
l2.PushFront(4);
l2.PushFront(3);
l2.PushFront(2);
l2.PushFront(1);
l2.Display();
l2.PopFront();
l2.PopFront();
l2.PopFront();
l2.Display();
}
void test3()
{
List l3;
l3.PushFront(4);
l3.PushFront(3);
l3.PushFront(2);
l3.PushFront(1);
l3.Display();
List l4(l3);
l4.Display();
List l5;
l5 = l4;
l5.Display();
}
void test4()
{
List l6;
l6.PushBack(1);
l6.PushBack(2);
l6.PushBack(4);
l6.PushBack(6);
l6.Display();
cout << endl;
Node* pos =l6. Find(1);
l6.Insert(pos,0);
Node* pos2 = l6.Find(4);
l6.Insert(pos2, 3);
Node* pos3 = l6.Find(6);
l6.Insert(pos3, 5);
l6.Display();
}
void test5()
{
List l7;
l7.PushBack(1);
l7.PushBack(2);
l7.PushBack(3);
l7.PushBack(4);
l7.PushBack(5);
Node* pos1 = l7.Find(1);
l7.Erase(pos1);
Node* pos2 = l7.Find(4);
l7.Erase(pos2);
Node* pos3 =l7.Find(5);
l7.Erase(pos3);
l7.Display();
}
int main()
{
test5();
return 0;
}
顺序表
#include <iostream>
using namespace std;
typedef int DataType;
class SeqList
{
public:
SeqList()
: _pData(new DataType[3])
, _capacity(3)
, _size(0)
{}
SeqList(size_t n, DataType value);
SeqList(const SeqList& s)
{
_pData = new DataType[s._capacity];
_capacity = s._capacity;
memcpy(_pData, s._pData, s._size*sizeof(DataType));
}
SeqList& operator=(const SeqList& s)
{
_pData = new DataType[s._capacity];
_capacity = s._capacity;
memcpy(_pData, s._pData, s._size*sizeof(DataType));
}
~SeqList()
{
delete[](_pData);
_pData=NULL;
}
void* PushBack(const DataType& data)
{
_size++;
if(_size>=_capacity)
{
_CheckCapacity();
}
_pData[_size-1]=data;
}
void PopBack()
{
_size--;
}
void Insert(size_t pos, const DataType& data)
{
_size++;
_CheckCapacity();
size_t i=_size;
while(i=_size; i>=pos; --i)
{
_pData[i]=_pData[i-1];
}
_pData[pos-1]=data ;
}
void Erase(size_t pos)
{
_size--;
while (size_t i=pos-1; i<_size; i--)
{
_pData[i]=_pData[i+1];
}
}
int Find(const DataType& data)
{
int i=0;
while (_pData[i++]!=data)
{}
if (i==_size-1)
{
return -1;
}
return 0;
}
size_t Size()const
{
return _size;
}
size_t Capacity()const
{
return _capacity;
}
bool Empty()const
{
if(_size==0)
{
return true;
}
return false;
}
void Clear()
{
_size=0;
}
void Resize(size_t size, const DataType& data)
{
size_t count=_size-size;
while (count; count>0; count--)
{
PopBack();
}
}
DataType& operator[](size_t index)
{
return _pData[index];
}
const DataType& operator[](size_t index)const
{
return _pData[index];
}
DataType& Front()
{
return _pData[0];
}
const DataType& Front()const
{
return _pData[0];
}
DataType& Back()
{
return _pData[_size-1];
}
const DataType& Back()const
{
return _pData[_size-1];
}
private:
void _CheckCapacity()
{
if(_size == _capacity)
{
DataType* pTemp = new DataType[_capacity*2];
for(size_t idx = 0; idx < _size; ++idx)
pTemp[idx] = _pData[idx];
delete[] _pData;
_pData = pTemp;
_capacity *= 2;
}
}
private:
DataType* _pData;
size_t _capacity;
size_t _size;
};