当前位置:网站首页>Double linked list implementation
Double linked list implementation
2022-06-24 22:01:00 【Weng Weiqiang】
1. Compared to a single linked list One more precursor node that can access the following nodes
The insert :
newNode->_next = pos;
pos->_front->_next = newNode;
newNode->_front = pos->_front;
pos->_front = newNode;Delete operation :
Node<T>* front = pos->_front;
Node<T>* next = pos->_next;
next->_front = front;
front->_next = next;The sorting operation :
From the initial position to the last tail The location of , Compare the size before and after , Exchange positions , then tail Position continues to decrease 1, Until the last starting position is equal to tail Location , End of sort
void sort()
{
int flag = 1;
Node<T>* cur = _head;
Node<T>* tail = NULL;
while (cur != tail)// Bubble sort process Decrease from back to front until 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;
}
}Template code :
#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)//& Use value symbol
{
os << n._data;
return os;
}
template <class T>
class LinkList
{
friend ostream& operator<<(ostream& os, LinkList<T>& list);// Output linked list class
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)// If the end is not empty
{
_tail->next = tmp;
tmp->_front = _tail;
_tail = tmp;
}
else// Empty list
{
_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;// Otherwise, it is an empty node
_tail = newNode;
}
}
void pop_back()
{
if (!_head && !_tail)
{
cout << " The linked list is empty and cannot be deleted " << endl;
return;
}
else if (_head = _tail)// If there is only one node
{
delete _tail;
_head = NULL;
_tail = NULL;
}
else// There are at least two nodes
{
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;// Implementation of two positions
}
else
{
_tail = newNode;
}
_head = newNode;// Update header
}
void popFront()
{
if (!_tail && !_head)
{
cout << " The linked list is empty and cannot be deleted " << endl;
}
else if (_head == _tail)// Only one node
{
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)// Insert after the specified position
{
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// Insert... At the last node
{
pos->_next = newNode;
newNode->_front = pos;
_tail = newNode;
}
}
void insert(int, Node<T>* pos, T x)// Insert before the specified position
{
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// Insert before the first node Be similar to 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)// Except for the first node The rest can be deleted
{
while (cur)
{
remove(x);
cur = cur->_next;
}
}
}
void sort()
{
int flag = 1;
Node<T>* cur = _head;
Node<T>* tail = NULL;
while (cur != tail)// Bubble sort process Decrease from back to front until 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();
}Output results :
1 2 6
边栏推荐
- 二叉搜索树模板
- 03---增反膜
- Data link layer & some other protocols or technologies
- The collection of zero code enterprise application cases in various industries was officially released
- 并查集+建图
- 即构「畅直播」上线!提供全链路升级的一站式直播服务
- Object. Defineproperty and reflect Fault tolerance of defineproperty
- Transport layer UDP & TCP
- EasyBypass
- Kubernetes 集群中流量暴露的几种方案
猜你喜欢
![[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture](/img/b5/23e3aed317ca262ebd8ff4579a41a9.png)
[camera Foundation (II)] camera driving principle and Development & v4l2 subsystem driving architecture

【吴恩达笔记】机器学习基础

Multiplexer select

You are using pip version 21.1.2; however, version 22.1.2 is available

openGauss内核:简单查询的执行

That is to say, "live broadcast" is launched! One stop live broadcast service with full link upgrade
![[精选] 多账号统一登录,你如何设计?](/img/df/9b4fc11a6971ebe8162ae84250a782.png)
[精选] 多账号统一登录,你如何设计?
![[notes of Wu Enda] multivariable linear regression](/img/b1/60a702aaca58b0afa57ac2f552dabf.png)
[notes of Wu Enda] multivariable linear regression

Data link layer & some other protocols or technologies

排查到解决问题的过程:浏览器突然无法访问网页,错误代码:0x80004005,最终定位:“电脑打开热点,电脑就不能上网了”
随机推荐
Excel layout
Practice of hierarchical management based on kubesphere
最大流问题
Flutter-使用 typedef的注意事项
[camera Foundation (I)] working principle and overall structure of camera
Datakit agent realizes unified data aggregation in LAN
机器学习:梯度下降法
平衡二叉搜索树
Implementation of heap sort and quick sort principle
Summary of papers on traveling salesman problem (TSP)
Machine learning: gradient descent method
【论】A deep-learning model for urban traffic flow prediction with traffic events mined from twitter
Redis+Caffeine两级缓存,让访问速度纵享丝滑
02---纵波不可能产生的现象
[精选] 多账号统一登录,你如何设计?
Flutter 库冲突问题解决
Principle and application of queue implementation
[untitled]
在每个树行中找最大值[分层遍历之一的扩展]
2022 international women engineers' Day: Dyson design award shows women's design strength