双向循环链表

#pragma once
using namespace std;

template<class ty>
struct node
{
	
	node(node*pfront,ty t,node*ptail)
	{
		this->front = pfront;
		this->t = t;
		this->tail = ptail;
	}
	node()
	{
		this->front = this;
		this->tail = this;
	}
	node(ty t)
	{
		this->front = this;
		this->t = t;
		this->tail = this;
	}

	node* front;
	ty t;
	node* tail;
};

template<class ty>
class list
{
public:
	list()
	{
		this->headnode = nullptr;
	}
	void printfbytail()
	{
		node<ty>* pmove = this->headnode;
		if (this->headnode == nullptr) return;
		while (1)
		{
		
			cout << pmove->t << " ";
			pmove = pmove->tail;
			if (pmove == this->headnode->front)
			{
				cout << pmove->t << " ";
				cout << endl;
				return;
			}
		}
		
	}
	void printbyfront()
	{
		node<ty>* pmove = this->headnode->front;
		if (this->headnode == nullptr) return;
		while (1)
		{

			cout << pmove->t << " ";
			pmove = pmove->front;
			if (pmove == this->headnode)
			{
				cout << pmove->t << " ";
				cout << endl;
				return;
			}
		}

	}
	void push_front(ty t)
	{
		
		if (this->headnode == nullptr)
		{
			this->headnode = new node<ty>(t);
		}
		else
		{
			node<ty>* pmove = new node<ty>(t);
			pmove->front = headnode->front;
			pmove->tail = headnode;
			headnode->front = pmove;
			headnode = pmove;
		}
	}
	void push_back(ty t)
	{
		if (this->headnode == nullptr)push_front(t);
		else
		{
			node<ty>* pmove = new node<ty>(t);
			pmove->front = this->headnode->front;
			pmove->tail = headnode;
			this->headnode->front->tail = pmove;
			this->headnode->front = pmove;
		}

	}
	void insert(int n,ty t)
	{
		if (headnode == nullptr) push_back(t);
		node<ty>* pmove = this->headnode->tail;
		node<ty>* pre = this->headnode;
		node<ty>* pnew = new node<ty>(t);
		for (int i = 0; i < n; i++)
		{
			pmove = pmove->tail;
			pre = pre->tail;
		}
		pnew->tail = pmove;
		pnew->front = pre;
		pre->tail = pnew;
		pmove->front = pnew;
	
	}
	void pop_front()
	{
		if (this->headnode == nullptr) return;
		this->headnode->tail->front = this->headnode->front;
		this->headnode->front->tail = this->headnode->tail;
		headnode = this->headnode->tail;
	}
	void pop_back()
	{
		if (this->headnode == nullptr) return;
		node<ty>* backnode = this->headnode->front;
		this->headnode->front = backnode->front;
		backnode->front->tail = this->headnode;
		delete backnode;
	}
	void deletes(int n)
	{
		if (headnode == nullptr) return;
		node<ty>* pmove = this->headnode->tail;
		node<ty>* pre = this->headnode;
		for (int i = 0; i < n; i++)
		{
			pmove = pmove->tail;
			pre = pre->tail;
		}
		node<ty>* backnode = pmove->tail;
		pre->tail = backnode;
		backnode->front = pre;
		delete pmove;
	}
	~list()
	{
		if (this->headnode != nullptr)
		{
			this->headnode = nullptr;
		}
	}

	node<ty>* headnode;
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值