一个关于循环链表的典型应用.
大致过程建立循环链表,输出循环链表,处理循环链表依次去掉那些节点. 程序如下:
// Jonse.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "iomanip.h"
struct Jonse
{
int code;
Jonse *next;
};
Jonse *Create(int);
void ShowList(Jonse*);
void Out(Jonse*,int,int);
int main(int argc, char* argv[])
{
Jonse *head;
int num,val,beg;
cout<<"/nplease input the number of total:/n";
cin>>num;
head=Create(num);
ShowList(head);
cout<<"/nplease input the code of begin:/n";
cin>>beg;
cout<<"/nplease input the interval of couting:/n";
cin>>val;
cout<<"the new list is:/n";
Out(head,beg,val);
return 0;
}
Jonse *Create(int n)
{
Jonse *h,*p;
h=new Jonse;
p=h;
for(int i=1;i<=n;i++)
{
p->code=i;
if(i<n)
{
p->next=new Jonse;
p=p->next;
}
}
p->next=h;
return h;
}
void ShowList(Jonse *h)
{
Jonse *p;
p=h;
do
{
cout<<p->code<<'/t';
}while(p!=h);
}
void Out(Jonse *h,int i,int d)
{
Jonse *p,*q;
int k;
p=h;
for(q=h;q->next!=h;q=q->next)
for(k=1;k<i;k++)
{
q=p;
p=p->next;
}
while(p!=p->next)
{
for(k=1;k<d;k++)
{
q=p;
p=p->next;
}
cout<<p->code<<'/t';
q->next=p->next;
delete p;
p=NULL;
p=q->next;
}
cout<<p->code<<endl;
delete p;
p=NULL;
}