代码已经写好,输出结果有点不够理想,欢迎大虾路见不平一声吼!

yiruirui0507 yiruirui0507 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:


//CirListNode类
#include<stdio.h>
template <class T> class CirListNode
{
T data;
CirListNode<T>* link;
public:
CirListNode():link(NULL){}
CirListNode(T value):link(NULL),data(value){}
~CirListNode(){}
void SetLink(CirListNode<T>* next);
CirListNode<T>* GetLink();
T& GetData();
void SetData(T value);
};
template<class T>
void CirListNode<T>::SetData(T value)
{
data=value;
}

template<class T>
void CirListNode<T>::SetLink(CirListNode<T>* next)
{
link=next;
}
template<class T>
CirListNode<T>* CirListNode<T>::GetLink()
{
return link;
}
template<class T>
T& CirListNode<T>::GetData()
{
return data;
}

//CirList类
template<class T> class CirList
{
CirListNode<T>* head;
CirListNode<T>* tail;
CirListNode<T>* cur;
public:
CirList();
~CirList(){};
bool AddTail(T value);
void RemoveThis();
void RemoveAll();
void SetBegin();
int GetCount();
CirListNode<T>* GetCur();
bool IsEmpty();
T GetNext();
CirListNode<T>* GetNextNode();
void SetData(T value);
};
//CirList成员函数实现
template<class T>
void CirList<T>::SetData(T value)
{
cur->SetData(value);
}

template<class T>
CirListNode<T>* CirList<T>::GetNextNode()
{
cur=cur->GetLink();
return cur;
}

template<class T>
CirList<T>::CirList()
{
head=tail=new CirListNode<T>;
cur=NULL;
head->SetLink(head);
}
template <class T>
bool CirList<T>::AddTail(T value)
{
CirListNode<T>* add=new CirListNode<T>(value);
tail->SetLink(add);
tail=tail->GetLink();
tail->SetLink(head);
if(tail!=NULL)
{
return true;
}
else
return false;

}
template<class T>
void CirList<T>::RemoveThis()
{
if(cur==head)
{
cur=cur->GetLink();
}
CirListNode<T>* preCur=cur;
for(int i=0;i<this->GetCount();i++)
{
preCur=preCur->GetLink();
}
preCur->SetLink(cur->GetLink());
cur=preCur->GetLink();
preCur=NULL;
}
template<class T>
void CirList<T>::RemoveAll()
{
SetBegin();
int length=GetCount();
for(int i=0;i<length;i++)
{
RemoveThis();
}
cur=head;
}
template<class T>
void CirList<T>::SetBegin()
{
cur=head;
}
template<class T>
int CirList<T>::GetCount()
{
int num=0;
CirListNode<T>* here=cur;
while(cur->GetLink()!=here)
{
cur=cur->GetLink();
++num;
}
cur=cur->GetLink();
return num;
}
template<class T>
CirListNode<T>* CirList<T>::GetCur()
{
return cur;
}
template<class T>
bool CirList<T>::IsEmpty()
{
return head->GetLink()==head;
}
template<class T>
T CirList<T>::GetNext()
{
if(cur==head)
{
cur=cur->GetLink();
}
T num =cur->GetData();
cur=cur->GetLink();
return num;
}

#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"请输入拉丁方阶数(2<=N<=9)";
cin>>num;
CirList<int> latin;
for(int i=1;i<=num;i++)
{
latin.AddTail(i);
}
latin.SetBegin();
latin.GetNextNode();
for(int j=0;j<num;j++)
{
for(int k=0;k<num;k++)
{
cout<<latin.GetCur()->GetData()<<" ";
latin.GetNextNode();
}
cout<<endl;
latin.GetNextNode();
latin.GetNextNode();
}
return 0;
}

这个是个拉丁方阵问题,相信大家在数据结构中有所耳闻,没听过的也没关系,我就是想实现这样一个功能
当我输入4,则输出
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
借助循环单链表每次遍历都从多一个结点的位置出发,第一次从第一个结点遍历,第二次从第二个结点遍历。。。。。
代码如上所示,
运行结果图也贴出来了,请大家帮忙看看错误出现在了什么地方?多谢了!
...全文

相关推荐

最新搞笑经典语句,路见不平一声吼,吼完继续往前走.doc

最新搞笑经典语句,路见不平一声吼,吼完继续往前走.doc

CSGOGOTO

CSYuTuiMian2020:关于2020年CS预推免的汇总。欢迎大家分享预推免信息,资瓷一下互联网精神吼不吼啊?

南方通2020 关于2020年CS预推免的汇总。欢迎大家分享预推免信息,资瓷一下互联网精神吼不吼啊? 清华大学 【报名截止】 【报名截止】 【报名截止】研究生 北京大学 上海交通大学 浙江大学 【报名截止】 【报名截止】 复旦大学 【报名终止】 南京大学 【报名截止】南京大学 中国科学院大学 中国科学院软件研究所 【报名终止】 中国科学院信息工程研究所 【报名截止】 中国科学院计算机网络信息中心 【即日起至10月12日】 中国科学技术大学 中国人民大学 西湖大学 北京邮电大学 【即日起至10月9日】 北京航空航天大学 【即日起至9月30日16:00】 上海科技大学 同济大学 【即日起至9月27日】 华东师范大学 【即日起至9月27日】 武汉大学 【报名终止】 华中科技大学 【即日起至10月9日】 哈尔滨工业大学(本部,威海,深圳) 【即日起至全国推免服务系统开通前】 哈尔滨工业大学

星见勇气

关于2019年CS保研夏令营的汇总。欢迎大家分享夏令营信息,资瓷一下互联网精神吼不吼啊?_CSBaoYan2019.zip

关于2019年CS保研夏令营的汇总。欢迎大家分享夏令营信息,资瓷一下互联网精神吼不吼啊?_CSBaoYan2019

好家伙VCC

大虾的拼音怎么写.docx

大虾的拼音怎么写.docx

xilei157641554

跟我一起写大虾网(第2天)

NULL 博文链接:https://yulon.iteye.com/blog/588941

weixin_38669628
评论 7
收藏 已收藏
分享
返回顶部
yiruirui0507
C++ 语言 65,214 成员 250,514 内容