#include <iostream>
#include <assert.h>
using namespace std;
typedef int DataType;
// 首先要明白一个链表的组成,链表又结点连接起来构成,我们定义出一个结点的结构体,每一个结点都有自己的next域,pre 域,以及自身的数据。
struct Node
{
Node( const DataType& data = DataType())
:_next(NULL)
, _pre(NULL)
, _data(data)
{}
Node *_next;
Node *_pre;
DataType _data;
};
// 创建链表结构体的时候,要清楚,一个链表会有自己的头指针,尾指针,还有链表的结点数。我们把链表的头,尾指针都定义为Node*类型。
class List
{
public:
List()//空链表的构造函数
:_pHead(NULL)
, _pTail(NULL)
, _size(0)
{}
Node *_pHead;
Node *_pTail;
size_t _size;
List(size_t n, const DataType & data = DataType())//明确给出构造n个结点,并且每个结点的值为默认值
:_pHead(NULL)
, _pTail(NULL)
, _size(0)
{
/*_pHead = _pTail = new Node(data);//我们可以用这样的方式来构造出一条链表
for (size_t idx = 0; idx < n - 1; idx++)
{
Node*pCur = new Node(data);
_pTail->_next = pCur;
pCur->_pre = _pTail;
_pTail = pCur;
}*/
for (size_t idx = 0; idx < n; idx++)//直接用尾插法创建链表
{
PushBack(data);
}
}
bool Empty()const//判断链表是否为空,因为后面的好多地方都要用到这些代码,为了提高代码的复用率,直接封装一个函数。
{
return (NULL == _pHead);
}
void PushBack(const DataType &data)
{
Node* pNode = new Node(data);
if (Empty())
{
_pHead = _pTail = pNode;
}
else
{
_pTail->_next = pNode;
pNode->_pre = _pTail;
_pTail = pNode;
}
_size++;
}
void PopBack(const DataType& data)
{
if (Empty())
{
assert(false);
return;
}
else if (_pHead=_pTail)//只有一个结点;
{
delete _pHead;
_pHead = _pTail = NULL;
}
else
{
_pTail = _pTail->_pre;
delete _pTail->_next;
_pTail->_next = NULL;
}
--_size;
}
void PushFront(const DataType& data)
{
Node*pNode = new Node(data);
if (Empty())
{
_pHead = _pTail = pNode;
}
else
{
pNode->_next = _pHead;
_pHead->_pre = pNode;
_pHead = _pHead->_pre;
}
_size++;
}
void PopFront()
{
if (Empty())
{
assert(false);
return;
}
else if (_size == 1)
{
delete _pHead;
_pHead = _pTail = NULL;
}
else
{
_pHead = _pHead->_next;
delete _pHead->_pre;
_pHead->_pre = NULL;
}
_size--;
}
Node*find(const DataType &data)
{
Node * pCur = _pHead;
while (pCur)
{
if (pCur->_data = data)
return pCur;
pCur = pCur->_next;
}
return NULL;
}
size_t Size()const
{
return _size;
}
DataType& Front()
{
return _pHead->_data;
}
const DataType& Front()const
{
return _pHead->_data;
}
DataType& Back()
{
return _pTail->_data;
}
const DataType& Back()const
{
return _pTail->_data;
}
void print_List()
{
Node*phead = _pHead;
while (phead){
cout << phead->_data << "->" << endl;
phead = phead->_next;
}
}
void insert(Node* pos, DataType x)
{
assert(pos);//防止pos为空指针
if (Empty())
{
return;
}
else
{
Node* pCur = _pHead;
if (_pHead == pos)
{
PushFront(x);
}
else
{
while (pCur)
{
if (pCur == pos)
{
Node* tmp = new Node[sizeof(Node)];
tmp->_data = pCur->_data;
tmp->_next = pCur->_next;
tmp->_pre = pCur;
pCur->_data = x;
pCur->_next = tmp;
return;//insert成功后不要忘了直接返回结束函数
}
pCur = pCur->_next;
}
cout << "没有找到这个数字" << endl;
}
}
}
void erase(Node* pos)//删除指定位置的结点
{
assert(pos);
if (Empty())
{
return;
}
else
{
if (pos == _pHead)
{
PopFront();
}
else
{
Node* del = _pHead->_next;
Node* pre = _pHead;
while (del)
{
if (del == pos)
{
if (del->_next != NULL)
{
pre->_next = del->_next;
del->_next->_pre = pre;
delete del;
del = NULL;
}
else
{
delete del;
pre->_next = NULL;
}
return;
}
del = del->_next;
pre = pre->_next;
}
}
}
}
};