数据结构中使用C++进行双向循环链表的相关操作,
#pragma once#include<iostream>
using namespace std;
template<class T>
class CalDoubleNode;
template<class T>
class Node
{friend class CalDoubleNode<T>;
private:
Node<T>*left,*right;
T data;
};
template<class T>
class CalDoubleNode
{
public:
CalDoubleNode(void);
~CalDoubleNode(void);
bool IsEmpty(){return head==0;}
bool Find(int k,T&x)const;
int Length();
CalDoubleNode<T>& Insert(int k,const T&x);
CalDoubleNode<T>& Delete(int k,T&x);
void Print()const;//正向输出
void ResPrint()const;//逆向输出
private:
Node<T>*head;//双向循环链表的头结点
};
template<class T>
CalDoubleNode<T>::CalDoubleNode()
{
head->left =head;
head->right =head;
}
template<class T>
CalDoubleNode<T>::~CalDoubleNode()
{
Node<T>*next;
while(head!=head->left )
{
next=head->right ;
delete head;
head=next;
}
delete head;
}
template<class T>
int CalDoubleNode<T>::Length ()
{
Node<T>*Curr=head;
int index=0;
while(Curr)
{
Curr=Curr->right ;
index++;
}
return index;
}
template<class T>
bool CalDoubleNode<T>::Find (int k,T&x)const
{
if(k<=0||k>Length())
{
cout<<"错误输入,退出!"<<endl;
exit(1);
}
else
{
Node<T>*Curr=head;
int index=1;
while(Curr&&index<k)
{
Curr=Curr->right;
index++;
}
if(k==index)
{
x=Curr->data;
return true;
}
return false ;
}
}
template<class T>
CalDoubleNode<T>&CalDoubleNode<T>::Insert(int k,const T&x)//Insert after k'th element;
{
if(k<0||k>Length())
{
cout<<"错误输入,退出!"<<endl;
exit(1);
}
else
{
Node<T>*Curr=head;
Node<T>*newNode=new Node<T>;
newNode->data =x;
int index=1;
if(k==0)
{
if(!Curr)//原始链表为空时
{
newNode->right =newNode->left =newNode;
head=newNode;
}
else
{
newNode->right =head;
newNode->left=head->left ;
head->left =newNode;
head=newNode;
}
}
else
{
while(Curr&&index<k)
{
Curr=Curr->right ;
index++;
}
if(Curr==head->left )// 作为最后一个元素插入的时候
{
newNode->left =Curr;
Curr->right =newNode;
newNode->right =head;
head->left =newNode;
}
else
{
newNode->right =Curr->right ;
Curr->right ->left =newNode;
newNode->left =Curr;
Curr->right =newNode;
}
}
}
return *this;
}
template<class T>
void CalDoubleNode<T>::Print()const
{
Node<T>*CurrPtr=head;
while(CurrPtr!=head->left)
{
cout<<CurrPtr->data <<" " ;
CurrPtr=CurrPtr->right ;
}
cout<<CurrPtr->data <<" " ;
}
template<class T>
void CalDoubleNode<T>::ResPrint ()const
{
Node<T>*CurrPtr=head;
while(CurrPtr!=head->right )
{
cout<<CurrPtr->data <<" ";
CurrPtr=CurrPtr->left ;
}
cout<<CurrPtr->data <<" ";
}
请问这个构造函数和插入函数为何好像编译不过啊,求大婶指点哈子