当前位置:网站首页>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

原网站

版权声明
本文为[Weng Weiqiang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241510455905.html