数据结构单循环链表

数据结构单循环链表,第1张

#include<iostreamh>
#include"Circlisth"
template<class T>
void Josephus(Circlist<T>&Js,int n,int m){
CircLinkNode<T> p=jsgetHead(),pre=NULL;
int i,j;
for(i=0;i<n-1;i++){
for(j=1;j<m;j++)
{pre=p;p=p->link;}
cout<<"出列的人是"<<p->data<<endl;
pre->link=p->link;delete p;
p=pre->link;
}
};
void main(0{
Circlist<int>clist;
int in,m;
cout<<"请输入游戏人数和报数间隔:";
cin>>n>>m;
for(i=1;i<=n;i++)clistinsert(i);
Josephus(clist,n,m);
}

typedef int ElemType;
//定义链表
typedef struct lnode
{
ElemType data; //数据域
struct lnode next; //后继指针
}LNode;
//定义链队列将头尾指针封装在一起的链队
typedef struct
{
LNode rear; //对尾指针
}QueueNode;
//入对
void Insert(QueueNode q,ElemType x)
{
LNode s;
s=(LNode )malloc(sizeof(LNode));
if(s==NULL)
exit(1);
s->data = x;//赋值
s->next=q->rear->next;//新节点后继指向对尾后继
q->rear->next=s;//对尾后继指向新节点
q->rear=s;//对尾指向新节点
}

创建链表时,最后一个节点的next指针总是指向NULL,如果在创建函数中,将该句用hean代替NULL即可。要说明的是,在其他函数中,但凡以!= NULL作为循环条件的语句,必须都改为 != head,切记切记。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13356153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-20
下一篇 2023-07-20

发表评论

登录后才能评论

评论列表(0条)

保存