双链表实现

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值