#include<stdio.h>
#include<iostream>
#include<assert.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef int DataType;
struct Node
{
Node* _pNext;
Node* _pPre;
DataType _data;
Node(const DataType& data = DataType())
: _pNext(NULL)
, _pPre(NULL)
, _data(data)
{}
};
class List
{
public:
List()
: _pHead(new Node)
{}
List(const List& l) //拷贝构造
{
List tmp(l);
swap(_pHead, tmp._pHead);
}
List& operator=(const List& l) //赋值运算符
{
if (this != &l)
{
List tmp(l);
swap(_pHead, tmp._pHead);
}
return *this;
}
~List() //析构
{
if (NULL != _pHead)
{
delete _pHead;
_pHead = NULL;
}
}
void PushBack(DataType data)
{
if (NULL == _pHead->_pPre) //空
{
_pHead = BuyNode(data);
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
else if (_pHead->_pNext == _pHead) //有一个
{
Node* NewNode = BuyNode(data);
_pHead->_pNext = NewNode;
_pHead->_pPre = NewNode;
NewNode->_pNext = _pHead;
NewNode->_pPre = _pHead;
}
else if (_pHead->_pNext != _pHead) //多个
{
Node* NewNode = BuyNode(data);
Node* pcur = _pHead;
while (pcur->_pNext != _pHead)
{
pcur = pcur->_pNext;
}
pcur->_pNext = NewNode;
NewNode->_pPre = pcur;
NewNode->_pNext = _pHead;
_pHead->_pPre = NewNode;
}
}
void PopBack()
{
assert(_pHead != NULL);
if (NULL == _pHead->_pPre) //空
{
return;
}
else if (_pHead->_pNext == _pHead) //一个
{
delete(_pHead);
_pHead = NULL;
}
else if (_pHead->_pNext != _pHead) //多个
{
Node* pcur = _pHead;
while (pcur->_pNext != _pHead)
{
pcur = pcur->_pNext;
}
Node* ppre = pcur->_pPre;
ppre->_pNext = _pHead;
_pHead->_pPre = ppre;
delete pcur;
pcur = NULL;
}
}
void PushFront(DataType data)
{
if (NULL == _pHead->_pPre) //空
{
_pHead = BuyNode(data);
_pHead->_pNext = _pHead;
_pHead->_pPre = _pHead;
}
else if (_pHead->_pNext == _pHead) //有一个
{
Node* NewNode = BuyNode(data);
NewNode->_pNext = _pHead;
NewNode->_pPre = _pHead;
_pHead->_pNext = NewNode;
_pHead->_pPre = NewNode;
_pHead = NewNode;
}
else if (_pHead->_pNext != _pHead)
{
Node* NewNode = BuyNode(data);
Node* ptail = _pHead;
while (ptail->_pNext != _pHead)
{
ptail = ptail->_pNext;
}
NewNode->_pNext = _pHead;
_pHead->_pPre = NewNode;
ptail->_pNext = NewNode;
NewNode->_pPre = ptail;
_pHead = NewNode;
}
}
void PopFront()
{
assert(_pHead != NULL);
if (NULL == _pHead->_pPre) //空
{
return;
}
else if (_pHead->_pNext == _pHead) //一个
{
delete(_pHead);
_pHead = NULL;
}
else if (_pHead->_pNext != _pHead) //多个
{
Node* pcur = _pHead;
Node* ptail = _pHead;
while (ptail->_pNext != _pHead)
{
ptail = ptail->_pNext;
}
Node* pnext = _pHead->_pNext;
pnext->_pPre = ptail;
ptail->_pNext = pnext;
_pHead = pnext;
delete pcur;
}
}
Node* Find(DataType data)
{
assert(_pHead != NULL);
Node* pcur = _pHead;
while (pcur->_pNext != _pHead)
{
if (pcur->_data == data)
{
return pcur;
}
pcur = pcur->_pNext;
}
return NULL;
}
void Insert(Node* pos, DataType data)
{
assert(pos != NULL);
Node* pin = BuyNode(data);
Node* pnext = pos->_pNext;
pos->_pNext = pin;
pin->_pNext = pnext;
pin->_pPre = pos;
pnext->_pPre = pin;
}
void Erase(Node* pos)
{
Node* pon = pos->_pPre;
Node* pun = pos->_pNext;
pon->_pNext = pun;
pun->_pPre = pon;
delete pos;
}
size_t Size()
{
assert(_pHead != NULL);
size_t k = 1;
Node* pcur = _pHead;
while (pcur->_pNext != _pHead)
{
pcur = pcur->_pNext;
k++;
}
return k;
}
bool Empty()const
{
return _pHead == NULL;
}
void Print(void)
{
if (_pHead == NULL)
{
cout << "链表已空" << endl;
return;
}
Node* pcur = _pHead;
while (pcur->_pNext != _pHead)
{
cout << pcur->_data << " ";
pcur = pcur->_pNext;
}
cout << pcur->_data << endl;
}
private:
Node * BuyNode(const DataType& data)
{
return new Node(data);
}
private:
Node * _pHead;
};
int main(void)
{
List s1;
s1.PushBack(1);
//s1.PopFront();
s1.PushBack(2);
s1.PushBack(3);
s1.PushBack(4);
s1.PushBack(5);
//s1.PopBack();
Node* pos = s1.Find(3);
s1.Insert(pos, 5);
s1.Erase(pos);
s1.Print();
cout << s1.Size() << endl;
system("pause");
}
C++双向循环链表
最新推荐文章于 2019-12-11 21:39:41 发布