【C++】模板实现带头节点的双向循环链表

链表分为带头节点和不带头节点,单链表和双向循环链表。

带头节点的链表虽然有头结点,但是并不存储数据,双向循环链有指向前一个数的指针和指向后一个数的指针。

如下图:

先来实现一些主要的函数:

List的构造函数和析构函数:

        List()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}

	~List()
	{
		clear();
		delete _head;
		_head = NULL;
	}

List在任意一个位置插入一个数和删除一个数

 

//任意一个位置插入一个数,在pos的前面插入一个数据
	void Insert(Node* pos, const T& x)
	{
		assert(pos);
		Node* tmp = new Node(x);
		Node* cur = pos;
		Node* prev = cur->_prev;

		prev->_next = tmp;
		tmp->_prev = prev;

		tmp->_next = cur;
		cur->_prev = tmp;
	}
//删除任意一个位置的数
	void Erase(Node* pos)
	{
		assert(pos && pos != _head);
		Node* prev = pos->_prev;
		Node* next = pos->_next;

		prev->_next = next;
		next->_prev = prev;
		delete pos;
	}

有了插入和删除函数之后,其他的函数都可以用Erase和Insert来复用:

尾部插入和删除函数:

//尾部插入一个数
	void PushBack(const T& x)
	{
		Insert(_head, x);
	}


//尾部删除一个数
	void PopBack(const T& x)
	{
		Erase(_head->_prev);
	}

头部插入和删除数据:

//头部插入一个数
	void PushFront(const T& x)
	{
		Insert(_head->_next,x);
	}
//头部删除一个数
	void PopFront(const T& x)
	{
		Erase(_head->_next);

	}

构造函数和赋值运算符重载:

//拷贝构造函数
	List(const List<T> & l)
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;

		Node* cur = (l._head)->_next;
		while (cur != l._head)
		{
			this->PushBack(cur->_data);
			cur = cur->_next;
		}
	}

//赋值运算符重载
	List<T>& operator=(const List<T>& l)
	{
		if (this != &l)
		{
			this->clear();
			Node* cur = l._head->_next;
			while (cur != l._head)
			{
				this->PushBack(cur->_data);
				cur = cur->_next;
			}
		}
		return *this;
	}

在插入和删除数据的时候要考虑双向链表里面会出现的各种情况!

以下是完整的代码:

ListNode.h
#pragma once

#include<assert.h>
template<class T>
struct ListNode
{
	T _data;
	ListNode<T>* _next;
	ListNode<T>* _prev;

	ListNode(const T& x = T())
		:_data(x)
		, _next(NULL)
		, _prev(NULL)
	{}

};

template<class T>
class List
{
	typedef ListNode<T> Node;
public:
	List()
	{
		_head = new Node;
		_head->_next = _head;
		_head->_prev = _head;
	}

	~List()
	{
		clear();
		delete _head;
		_head = NULL;
	}


	//删除任意一个位置的数
	void Erase(Node* pos)
	{
		assert(pos && pos != _head);
		Node* prev = pos->_prev;
		Node* next = pos->_next;

		prev->_next = next;
		next->_prev = prev;
		delete pos;
	}

	//任意一个位置插入一个数,在pos的前面插入一个数据
	void Insert(Node* pos, const T& x)
	{
		assert(pos);
		Node* tmp = new Node(x);
		Node* cur = pos;
		Node* prev = cur->_prev;

		prev->_next = tmp;
		tmp->_prev = prev;

		tmp->_next = cur;
		cur->_prev = tmp;
	}
	//尾部插入一个数
	void PushBack(const T& x)
	{
		Insert(_head, x);
	}


	//尾部删除一个数
	void PopBack(const T& x)
	{
		Erase(_head->_prev);
	}

	//头部插入一个数
	void PushFront(const T& x)
	{
		Insert(_head->_next,x);
	}


	//头部删除一个数
	void PopFront(const T& x)
	{
		Erase(_head->_next);

	}

	//查找一个数
	void Find(const T& x)
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			if (cur->_data == x)
			{
				return cur;
			}
			return NULL;
		}
	}

	//清除
	void clear()
	{
		Node* cur = _head->_next;
		while (cur != _head)
		{
			Node* next = cur->_next;
			delete cur;
			cur = next;
		}
		_head->_next = _head;
		_head->_prev = _head;
	}

	//打印
	void Print()
	{
		Node* cur = _head->_next;
		while (cur!=_head)
		{
			cout << cur->_data << "";
			cur = cur->_next;
		}
		cout << endl;
	}


	//拷贝构造函数
	List(const List<T> & l)
	{
		_head = new Node();
		_head->_next = _head;
		_head->_prev = _head;

		Node* cur = (l._head)->_next;
		while (cur != l._head)
		{
			this->PushBack(cur->_data);
			cur = cur->_next;
		}
	}

	//赋值运算符重载
	List<T>& operator=(const List<T>& l)
	{
		if (this != &l)
		{
			this->clear();
			Node* cur = l._head->_next;
			while (cur != l._head)
			{
				this->PushBack(cur->_data);
				cur = cur->_next;
			}
		}
		return *this;
	}


protected:
	Node* _head;
};



void Test1()//尾部插入和删除
{
	//插入
	List<int> l;
	l.PushBack(1);
	l.Print();
	l.PushBack(2);
	l.Print();
	l.PushBack(3);
	l.Print();
	l.PushBack(4);
	l.Print();

	//删除
	l.PopBack(1);
	l.Print();
	l.PopBack(1);
	l.Print();
	l.PopBack(1);
	l.Print();
}

void Test2()//头部插入和删除
{
	//插入
	List<int> l;
	l.PushFront(1);
	l.Print();
	l.PushFront(2);
	l.Print();
	l.PushFront(3);
	l.Print();

	//删除
	l.PopFront(1);
	l.Print();
	l.PopFront(1);
	l.Print(); 
	l.PopFront(1);
	l.Print(); 
}


测试函数
Test.cpp
#include<iostream>
using namespace std;
#include"ListNode.h"

int main()
{
	Test1();
	Test2();
	system("pause");
	return 0;
}

 

 


  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
约瑟夫问题是经典的数学游戏,也称为约瑟夫环,由于其富有趣味性和一定的数学难度而被广泛研究和应用。解决约瑟夫问题的方法有多种,其中一种常用方法是使用双向循环链表带头结点。 双向循环链表是一种特殊的链表,它可以遍历整个链表,即尾节点的下一个结点是头结点头结点的上一个结点是尾节点,这样形成的链表称为双向循环链表带头结点的双向循环链表头结点前面加入一个空结点,使得空结点也能对链表中结点进行操作。 在解决约瑟夫问题时,我们首先需要初始化双向循环链表,然后按照一定规则进行出队操作,直到只剩下最后一个结点为止。假设约瑟夫问题中有n个人围成一圈,从第k个人开始报数,报到第m个人就将其出队,然后从下一个人重新开始报数,求最后留下的人的编号。 具体实现时,我们可以根据输入的n和k创建双向循环链表,并从头结点开始依次插入n个结点,然后设置m的值并从头结点开始不断遍历链表直到只剩下一个结点。在遍历过程中,每经过m个结点就将该结点从链表中删除,并将该结点的下一个结点作为新的起点继续报数。最后剩下的那个结点即为答案。 总之,使用双向循环链表带头结点解决约瑟夫问题可以简化求解过程,提高代码效率和可读性,是一种比较常用的数据结构和算法应用方法。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值