#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#pragma once
typedef int DataType;
struct Node
{
Node(const DataType& data = 0)
: _data(data)
, _pNext(NULL)
, _pPre(NULL)
{}
DataType _data;
Node* _pNext;
Node* _pPre;
};
class List
{
public:
List()
{
_pHead = new Node();
_pTail = new Node();
_pHead->_pNext = NULL;
_pHead->_pPre = NULL;
_pTail = _pHead;
_size = 0;
}
List(size_t n, const DataType& data = DataType())
{
_pHead = new Node();
_pTail = new Node();
while (n--)
{
PushBack(data);
}
}
List(const List& l)
{
Node* tmp = l._pHead;
_pHead = new Node;
_pTail = new Node;
while (tmp)
{
PushBack(tmp->_data);
tmp = tmp->_pNext;
}
_size = l._size;
}
List& operator=(const List& l)
{
Node* tmp = l._pHead;
while (tmp)
{
PushBack(tmp->_data);
tmp = tmp->_pNext;
}
_size = l._size;
return *this;
}
~List()
{
Clear();
delete _pTail;
_pTail = NULL;
_size = 0;
}
void PushBack(const DataType& data)
{
Node* Head = _pHead;
if (0 == Head->_data && NULL == Head->_pNext)
{
Head->_data = data;
Head->_pPre = NULL;
Head->_pNext = NULL;
_size++;
_pTail = Head;
}
else if (Head->_pPre == Head->_pNext)
{
Node* tmp = new Node;
tmp->_data = data;
Head->_pNext = tmp;
tmp->_pPre = Head;
tmp->_pNext = NULL;
_size++;
_pTail = Head->_pNext;
}
else
{
Node* pCur = Head;
while (Head)
{
pCur = Head;
Head = Head->_pNext;
}
Head = new Node;
Head->_data = data;
pCur->_pNext = Head;
Head->_pPre = pCur;
Head->_pNext = NULL;
_size++;
_pTail = Head;
}
}
void PopBack()
{
Node* tmp = _pTail->_pPre;
delete _pTail;
_pTail = tmp;
_pTail->_pNext = NULL;
_size--;
}
void PushFront(const DataType& data)
{
Node* NewNode = new Node;
NewNode->_data = data;
NewNode->_pNext = _pHead;
_pHead->_pPre = NewNode;
NewNode->_pPre = NULL;
_pHead = NewNode;
_size++;
}
void PopFront()
{
Node* Head = _pHead;
_pHead = _pHead->_pNext;
delete Head;
Head = NULL;
_pHead->_pPre = NULL;
_size--;
}
Node* Find(const DataType& data)
{
Node* Head = _pHead;
if (Head)
{
while (data != Head->_data)
{
Head = Head->_pNext;
}
return Head;
}
}
void Insert(Node* pos, const DataType& data)
{
Node* NewNode = new Node;
NewNode->_data = data;
NewNode->_pPre = pos->_pPre;
pos->_pPre->_pNext = NewNode;
NewNode->_pNext = pos;
pos->_pPre = NewNode;
_size++;
}
void Erase(Node* pos)
{
if (NULL == _pHead->_pNext)
{
delete _pHead;
_pHead = NULL;
_size--;
}
else if (NULL == _pHead->_pPre)
{
Node* pCur = _pHead->_pNext;
pCur->_pPre = NULL;
delete _pHead;
_pHead = pCur;
_size--;
}
else
{
pos->_pPre->_pNext = pos->_pNext;
pos->_pNext->_pPre = pos->_pPre;
delete pos;
pos = NULL;
_size--;
}
}
void Assign(size_t n, const DataType data = DataType())
{
while (n--)
{
PushBack(data);
}
}
void Clear()
{
Node* Next = _pHead->_pNext;
while (Next)
{
_pHead->_pPre = NULL;
delete _pHead;
_pHead = NULL;
_pHead = Next;
if (NULL == Next)
{
break;
}
Next = Next->_pNext;
}
_size = 0;
}
Node& Front()
{
if (_pHead)
{
return *_pHead;
}
}
const Node& Front()const
{
if (_pHead)
{
return *_pHead;
}
}
Node& Back()
{
if (_pTail)
{
return *_pTail;
}
}
const Node& Back()const
{
if (_pTail)
{
return *_pTail;
}
}
// "[]"的第一种方式
DataType& operator[](size_t index)
{
if (index <= _size)
{
Node* Head = _pHead;
while (index--)
{
Head = Head->_pNext;
}
return Head->_data;
}
}
const DataType& operator[](size_t index) const
{
if (index <= _size)
{
Node* Head = _pHead;
while (index--)
{
Head = Head->_pNext;
}
return Head->_data;
}
}
// "[]"的第二种方式
//Node& operator[](size_t index)
//{
// if (index <= _size)
// {
// Node* tmp = new Node;
// Node* Head = _pHead;
// while (index--)
// {
// Head = Head->_pNext;
// }
// tmp = Head;
// return *tmp;
// }
//}
//const Node& operator[](size_t index)const
//{
// if (index <= _size)
// {
// Node* Head = _pHead;
// while (index--)
// {
// Head = Head->_pNext;
// }
// Node* tmp = Head;
// tmp->_pNext = Head->_pNext;
// tmp->_pPre = tmp->_pPre;
// return *Head;
// }
//}
size_t Size()const
{
return _size;
}
bool Empty()const
{
if (_pHead)
{
return true;
}
return false;
}
private:
Node* _pHead;
Node* _pTail;
size_t _size;
};
void FunTest()
{
List L2(5, 3);
List L1(L2);
List L3;
L3 = L1;
L1.PushFront(0);
cout << "L1尾插‘0’"<< endl;
L1.PopFront();
cout << "L1尾删一个数据:" << endl;
cout << "打印L3中第一个数据‘3’的地址:" << endl;
Node* node = L3.Find(3);
cout << node << endl;
cout << "删除L3中第一个数据‘3’的地址:" << endl;
L3.Erase(node);
cout << "L3头插数据‘1’:" << endl;
L3.PushFront(1);
cout << "L3头删一个数据:" << endl;
L3.PopBack();
cout << "给L3中‘3’的位置插入‘2’:" << endl;
Node* node1 = L3.Find(3);
L3.Insert(node1, 2);
cout << "返回L1的头结点的地址:" << endl;
Node node2 = L1.Front();
cout << "返回L2的尾节点的地址:" << endl;
Node node3 = L2.Back();
// "[]"第一种重载的使用方式
L1[1] = 5;
// "[]"第二种重载的使用方式
//L1[1]._data = 5;
}
int main()
{
FunTest();
system("pause");
return 0;
}
C++版双链表的实现
最新推荐文章于 2024-01-26 04:14:56 发布