带头节点的双向循环链表(数据结构)(带有遍历)

数据结构中双向循环链表,仅供参考

/*****************带头节点双向循环链表***************/
#ifndef Double_
#define Double_
 
#include <iostream>
#include "DoubleNode.h"
#include "Excepte.h"
#include "DCIterator.h"
using namespace std;
 
template <class T> class DCIterator;
 
template <class T>
class DoubleCircular{
friend DCIterator<T>;
public:
DoubleCircular()
{
first = new DoubleNode<T>;
        first->right = first;
        first->left = first;
}
~DoubleCircular();
int Length() const;
bool Find (int k,T& x)const;
int Search(const T& x)const;
DoubleCircular<T>& Delete(int k,T& x);
DoubleCircular<T>& Insert(int k,const T& x);
void Output(ostream& out)const;
private:
DoubleNode<T> *first;
};
 
template<class T>
DoubleCircular<T>::~DoubleCircular()
{
DoubleNode<T> *next,//下一个节点
          *cur=first->right;
while(cur!=first)
{
next = cur->right;
delete cur;
cur = next;
}
delete first;
}
 
template<class T>
int DoubleCircular<T>::Length()const
{
DoubleNode<T> *cur = first->right;
int len = 0;
while(cur != first)
{
len++;
cur = cur->right;
}
return len;
}
 
template<class T>
bool DoubleCircular<T>::Find(int k,T&x) const
{
if(k < 1||first->right ==first)
return false;
DoubleNode<T> *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 <class T>
int DoubleCircular<T>::Search(const T& x)const
{
//寻找x ,返回x的地址,否则返回
DoubleNode<T> *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<class T>
DoubleCircular<T>& DoubleCircular<T>::Delete(int k, T& x)
{
//把第k元素放到x中,然后删除k个元素,不存在引发异常
if(k < 1||first->right == first)
throw OutOfBounds();
DoubleNode<T> *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 <class T>
DoubleCircular<T>& DoubleCircular<T>::Insert(int k,const T& x)
{
//在第k个元素之后插入x
if(k < 0)
throw OutOfBounds();
DoubleNode<T> *p = first;
int index;
//将p移动到k个元素
for(index =0 ;index < k&& p ->right !=first;index++)
p = p->right;
if (index != k) 
throw OutOfBounds();
DoubleNode <T> *y =new DoubleNode<T>;
y->data=x;
 
y->right = p->right;
y->right->left = y;
y->left = p;
p->right = y;
return *this;
}
 
template<class T>
void DoubleCircular<T>::Output(ostream& out) const
{//将连表元素送至输出流
   DoubleNode<T> *cur;
   for (cur = first->right; cur != first;cur = cur->right)
      out << cur->data << "  ";
}
 
// 重载<<
template <class T>
ostream& operator<<(ostream& out, const DoubleCircular<T>& x)
{
x.Output(out);
return out;
}
 
#endif
 
//-----------------------------------------------------------------------------
#ifndef DCIterator_
#define DCIterator_
#include <iostream>
#include "DoubleNode.h"
#include "DoubleCircular.h"
 
template<class T>
class DCIterator
{
//friend DoubleCircular;
public:
T* Initialize(const DoubleCircular<T> &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<T> *location,*p;
};
#endif
 
#include <iostream>
using namespace std;
#include "DCIterator.h"
#include "DoubleCircular.h"
 
int main()
{
int n = 11;
DoubleCircular<int> d;
int z;
cout << "输入链表的长度:";
cin >> z;
for(int i=0;i<z;i++)
{
cout<<"输入元素:";
int n;
cin >>n; 
d.Insert(0,n);
}
 
 
cout<<"链表的长度:"<<d.Length()<<endl;
cout<<"输出链表:"<<d<<endl;
cout<<"在第一个元素后插入一个元素:";
int m;
cin>>m;
d.Insert(1,m);
cout << "输出链表:" << d << endl;
 
int x ;
cout<<"输入删除第几个元素:";
cin>>m;
d.Delete(m,x);
cout << "查看:" << d << endl;
 
int y;
cout << "查询链表长度:" << d.Length() << endl; 
cout << "查询元素并返回值:" ;
cin >> m;
if( d.Find(m,y))
cout <<"查看值:" << y <<endl;
else
cout<<"查询不存在。"<<endl;
 
y = 5;
cout << "查询,如果存在返回地址:" << d.Search(y) << endl;
 
 
    int *x1;
DCIterator<int> c;
 
x1=c.Initialize(d);
    cout<<"遍历器遍历:"<<endl;
while(x1)
{
cout<< *x1<<" ";
x1 = c.Next();
}
 
cout << endl;
 
return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值