先頭ノードの双方向ループチェーンテーブル(データ構造)(ループ付き)


データ構造における双方向ループチェーンテーブル、参照のみ
/*****************          ***************/
#ifndef Double_
#define Double_
 
#include 
#include "DoubleNode.h"
#include "Excepte.h"
#include "DCIterator.h"
using namespace std;
 
template  class DCIterator;
 
template 
class DoubleCircular{
friend DCIterator;
public:
DoubleCircular()
{
first = new DoubleNode;
        first->right = first;
        first->left = first;
}
~DoubleCircular();
int Length() const;
bool Find (int k,T& x)const;
int Search(const T& x)const;
DoubleCircular& Delete(int k,T& x);
DoubleCircular& Insert(int k,const T& x);
void Output(ostream& out)const;
private:
DoubleNode *first;
};
 
template
DoubleCircular::~DoubleCircular()
{
DoubleNode *next,//     
          *cur=first->right;
while(cur!=first)
{
next = cur->right;
delete cur;
cur = next;
}
delete first;
}
 
template
int DoubleCircular::Length()const
{
DoubleNode *cur = first->right;
int len = 0;
while(cur != first)
{
len++;
cur = cur->right;
}
return len;
}
 
template
bool DoubleCircular::Find(int k,T&x) const
{
if(k < 1||first->right ==first)
return false;
DoubleNode *cur =first->right;
int index = 1;//cur   
while(index < k && cur !=first)
{
cur = cur->right;
index++;
}
if(index == k)
{
x = cur->data;
return true;
}
return false;
} 
 
template 
int DoubleCircular::Search(const T& x)const
{
//  x ,  x   ,    
DoubleNode *cur = first->right;
int index = 1;//cur   
while(cur != first && cur->data!= x)
{
cur = cur->right;
index++;
}
if(cur->data = x)
return index;
return 0;
}
 
template
DoubleCircular& DoubleCircular::Delete(int k, T& x)
{
//  k    x ,    k   ,       
if(k < 1||first->right == first)
throw OutOfBounds();
DoubleNode *p=first->right;
 
int index;
 
//if(k == 1)//p     k   
//first = first->right;//   
 
for(index = 1;index < k&& p!= first;index++)
p = p->right;
if(k!=index)
throw OutOfBounds();
//    k
 p->right->left = p->left;
         p->left->right = p->right;
 
 //   k        
 x = p->data;
 delete p;
 return *this;
}
 
template 
DoubleCircular& DoubleCircular::Insert(int k,const T& x)
{
//  k       x
if(k < 0)
throw OutOfBounds();
DoubleNode *p = first;
int index;
// p   k   
for(index =0 ;index < k&& p ->right !=first;index++)
p = p->right;
if (index != k) 
throw OutOfBounds();
DoubleNode  *y =new DoubleNode;
y->data=x;
 
y->right = p->right;
y->right->left = y;
y->left = p;
p->right = y;
return *this;
}
 
template
void DoubleCircular::Output(ostream& out) const
{//          
   DoubleNode *cur;
   for (cur = first->right; cur != first;cur = cur->right)
      out << cur->data << "  ";
}
 
//   <<
template 
ostream& operator<& x)
{
x.Output(out);
return out;
}
 
#endif
 
//-----------------------------------------------------------------------------
#ifndef DCIterator_
#define DCIterator_
#include 
#include "DoubleNode.h"
#include "DoubleCircular.h"
 
template
class DCIterator
{
//friend DoubleCircular;
public:
T* Initialize(const DoubleCircular &d)
{
p=d.first;
location = d.first->right;
if(location != d.first)
return &location->data;
return 0;
}
T *Next()
{
if(location == p)
return 0;
location = location->right;
if(location != p)
return &location->data;
return 0;
}
private:
DoubleNode *location,*p;
};
#endif
 
#include 
using namespace std;
#include "DCIterator.h"
#include "DoubleCircular.h"
 
int main()
{
int n = 11;
DoubleCircular d;
int z;
cout << "       :";
cin >> z;
for(int i=0;i>n; 
d.Insert(0,n);
}
 
 
cout<>m;
d.Insert(1,m);
cout << "    :" << d << endl;
 
int x ;
cout<>m;
d.Delete(m,x);
cout << "  :" << d << endl;
 
int y;
cout << "      :" << d.Length() << endl; 
cout << "        :" ;
cin >> m;
if( d.Find(m,y))
cout < c;
 
x1=c.Initialize(d);
    cout<