1.相比于单链表多一个可以访问后面节点的前驱节点
插入操作:
newNode->_next = pos;
pos->_front->_next = newNode;
newNode->_front = pos->_front;
pos->_front = newNode;
删除操作:
Node<T>* front = pos->_front;
Node<T>* next = pos->_next;
next->_front = front;
front->_next = next;
排序操作:
从最初的位置到最后tail的位置,前后比较大小,进行交换位置,然后tail位置继续减1,直到最后开始位置等于tail位置,排序结束
void sort()
{
int flag = 1;
Node<T>* cur = _head;
Node<T>* tail = NULL;
while (cur != tail)//冒泡排序过程 从后往前递减 直到cur==tail
{
while (cur->_next != tail)
{
if (cur->_data > cur->_next->_data)
{
T tmp = cur->_data;
cur->_data = cur->_next->_data;
cur->_next->_data = tmp;
}
cur = cur->_next;
}
if (1 == flag)
{
break;
}
tail = cur;
cur = _head;
}
}
模板代码:
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
template <class T>
class LinkList;
template <class T>
class Node
{
friend LinkList<T>;
friend ostream& operator<<(ostream& os, Node<T>& n);
friend ostream& operator<<(ostream& os, LinkList<T>& list);
public:
Node(T x):_data(x),_next(NULL),_front(NULL){}
private:
T _data;
Node<T>* _next;
Node<T>* _front;
};
template <class T>
ostream& operator<<(ostream& os, Node<T>& n)//&采用取值符号
{
os << n._data;
return os;
}
template <class T>
class LinkList
{
friend ostream& operator<<(ostream& os, LinkList<T>& list);//输出链表类的
public:
LinkList() :_head(NULL), _tail(NULL) {}
LinkList(const LinkList&list) :_head(NULL), _tail(NULL)
{
Node<T>* cur = list._head;
while (cur)
{
Node<T>* tmp = new Node(cur->_data);
if (_tail)//若结尾不为空
{
_tail->next = tmp;
tmp->_front = _tail;
_tail = tmp;
}
else//空链表
{
_head = tmp;
_tail = tmp;
}
cur = cur->next;
}
}
~LinkList()
{
Node<T>* cur = _head;
while (cur)
{
Node<T>* del = cur;
cur = cur->_next;
delete del;
del = NULL;
}
}
public:
void push_back(T x)
{
Node<T>* newNode = new Node<T>(x);
if (_tail)
{
_tail->_next = newNode;
newNode->_front = _tail;
_tail = newNode;
}
else
{
_head = newNode;//否则就是个空节点
_tail = newNode;
}
}
void pop_back()
{
if (!_head && !_tail)
{
cout << "链表已空不可尾删" << endl;
return;
}
else if (_head = _tail)//假如只有一个节点
{
delete _tail;
_head = NULL;
_tail = NULL;
}
else//至少存在两个节点
{
Node<T>* tmp = _tail;
_tail = tmp->_front;
_tail->_next = nullptr;
delete tmp;
tmp = NULL;
}
}
void pushFront(T x)
{
Node<T>* newNode = new Node<T>(x);
if (_head)
{
Node<T>* tmp = _head;
newNode->_next = tmp;
tmp->_front = newNode;//两种位置的实现方式
}
else
{
_tail = newNode;
}
_head = newNode;//更新头
}
void popFront()
{
if (!_tail && !_head)
{
cout << "链表已空不可头删" << endl;
}
else if (_head == _tail)//只有一个节点
{
delete _head;
_head = NULL;
_tail = NULL;
}
else
{
Node<T>* tmp = _head;
_head = tmp->_next;
_head->front = NULL;
delete tmp;
tmp = NULL;
}
}
Node<T>* findNum(T x)
{
Node<T>* cur = _head;
while (cur)
{
if (cur->_data == x)
{
return cur;
}
cur = cur->_next;
}
return NULL;
}
void insert(Node<T>*pos,T x)//在指定位置后插
{
Node<T>* newNode = new Node<T>(x);
if (pos->_next)
{
newNode->_front = pos;
newNode->_next = pos->_next;
pos->_next->_front = newNode;
pos->_next = newNode;
}
else//在最后一个节点插入
{
pos->_next = newNode;
newNode->_front = pos;
_tail = newNode;
}
}
void insert(int, Node<T>* pos, T x)//在指定位置前插
{
Node<T>* newNode = new Node<T>(x);
if (pos->_front)
{
newNode->_next = pos;
pos->_front->_next = newNode;
newNode->_front = pos->_front;
pos->_front = newNode;
}
else//在第一个节点前插 类似于pushFront
{
newNode->_next = pos;
pos->_front = newNode;
_head = newNode;
}
}
void remove(T& x)
{
Node<T>* pos = this->findNum(x);
if (NULL != pos)
{
if (!(pos->_front) && pos->_next)
{
Node<T>* tmp = pos->_next;
pos->_front = NULL;
_head = tmp;
}
else if (!(pos->_next) && pos->_front)
{
Node<T>* tmp = pos->_front;
tmp->_next = NULL;
_tail = tmp;
}
else
{
Node* front = pos->_front;
Node* next = pos->_next;
next->_front = front;
next->_next = next;
}
delete pos;
pos = NULL;
}
}
void removeAll(T x)
{
Node<T>* cur = _head;
Node<T>* ret = this->findNum(x);
if (_head != ret)//除了第一个节点 其余都可以删除
{
while (cur)
{
remove(x);
cur = cur->_next;
}
}
}
void sort()
{
int flag = 1;
Node<T>* cur = _head;
Node<T>* tail = NULL;
while (cur != tail)//冒泡排序过程 从后往前递减 直到cur==tail
{
while (cur->_next != tail)
{
if (cur->_data > cur->_next->_data)
{
T tmp = cur->_data;
cur->_data = cur->_next->_data;
cur->_next->_data = tmp;
}
cur = cur->_next;
}
if (1 == flag)
{
break;
}
tail = cur;
cur = _head;
}
}
void erase(Node<T>* pos)
{
if (!(pos->_front) && pos->_next)
{
Node<T>* tmp = pos->_next;
tmp->_fornt = NULL;
_head = tmp;
}
else if (!(pos->_next) && pos->_front)
{
Node<T>* tmp = pos->_front;
tmp->_next = NULL;
_tail = tmp;
}
else
{
Node<T>* front = pos->_front;
Node<T>* next = pos->_next;
next->_front = front;
front->_next = next;
}
delete pos;
pos = NULL;
}
void traverse()
{
Node<T>* cur = _head;
while (cur)
{
cout << cur->_data << " ";
cur = cur->_next;
}
}
private:
Node<T>*_head;
Node<T>* _tail;
};
template<class T>
ostream& operator<<(ostream& os, LinkList<T>&list)
{
Node<T>* cur = list._head;
while (cur)
{
os << (*cur) << " ";
cur = cur->_next;
}
return os;
}
int main()
{
LinkList<int>list;
list.push_back(1);
list.push_back(6);
list.push_back(2);
list.sort();
list.traverse();
}
输出结果:
1 2 6