2019《王道数据结构》算法题-循环双链表C++实现

#include<iostream>
using namespace std;
template<class T>
struct Node{
	T data;
	Node<T>* next;
	Node<T>* pre;
	Node(Node<T>* p = NULL , Node<T>* n = NULL)
	{
		next = n;
		pre = p;
		
	}
	Node(const T& item , Node<T>* p = NULL , Node<T>* n = NULL)
	{
		data = item;
		next = n;
		pre = p;
	}
}; 
template<class T>
class DoubleCirList{//循环双链表 
	public:
		DoubleCirList(){
			head = current = new Node<T>();
			head->next = head;
			head->pre = head;
			count = 0;
		}
		~DoubleCirList(){
			Node<T>* temp;
			while(head->next != head){
				temp = head->next;
				head->next = temp->next;
				delete temp;
				count--;
			}
		}
		int length() const
		{
			return count;
		}
		Node<T>*& getHead()
		{
			return head;
		}
		bool insert(int index , T x){
			if(index > count || index < -1){
				cerr << "索引越界!" << endl;
				return false;
			}
			Node<T>* temp = head;
			if(index != 0){
				temp = head->next;
				for(int i = 0; i < index-1; i++){
					temp = temp->next;
				}
			}
			temp->next = new Node<T>(x,temp,temp->next);
			if(index == count)
				head->pre = temp->next;
			count++;
			return true;
		}
		bool add(T x){
			return insert(count,x);
		}
	private:
		Node<T>* head;
		Node<T>* current;
		int count;
	public:
		bool isSmmetry()
		{//2.3.7.17判断带头结点的循环双链表是否对称。 
			Node<T>* n = head->next;
			Node<T>* p = head->pre;
			int t;
			if(count & 1 == 1)
				t = count/2+1;
			else t = count/2;
			int i = 0;
			while(i < t){
				if(n->data != p->data)
					return false;
				n = n->next;
				p = p->pre;
				i++;
			}
			return true;
		}
	/*
		***************************END*************************** 
	*/
	/*
		**********************作者QQ632660120 *******************
	*/ 
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值