#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 *******************
*/
};