数据结构-joseph环
joseph環(huán)
 //時間:05/07/04
 //程序:張建波
 //輸入 n=7?? // 3,1,7,2,4,7,4? //輸出6 7 4 1 5 3 2
 #include <iostream.h>
 #include "key.h"
 typedef struct person
 { int pwd;? //密碼
 ? int num;? //人數(shù)
 ? struct person *next;? //指向結(jié)構(gòu)體的指針
 }PERSON;
 ? 
 ?
 void OutPut(int *a,int n);//輸出結(jié)果
 int CreatPersonList(PERSON *head);// 創(chuàng)建一張鏈表
 int Fx_PersonList(PERSON *head,int n,int *a);//模擬報數(shù)
 int _f4_main() //函數(shù)入口
 ?{ 
 ?? PERSON *head;? //定義鏈表頭
 ?? cout<<"joseph環(huán) 問題模擬!"<<endl;
 ?? head=new person; //新建PERSON,并分配內(nèi)存
 ?? int n=CreatPersonList(head);? // 創(chuàng)建一張鏈表,同時返回人數(shù) n
 ?? int *a=new int[20]; //保存輸出的結(jié)果
 ?? int x=Fx_PersonList(head,n,a);? //模擬報數(shù),同時返回出隊人數(shù) count
 ?? 
 ?? OutPut(a,x);? //輸出序列
 ?? InitKey();//鍵盤中斷
 ?? return 0;
 ? }
 int CreatPersonList(PERSON *head){
 ? int n,m,i;
 ? PERSON *p,*q;
 ? cout<<"/n請輸入 n=";
 ? cin>>n;
 ? p=head;??????? //p指向表頭
 ?? for(i=1;i<=n;i++){? //建立鏈表
 ??? ? cout<<"/n請輸入第"<<i<<"的人的報數(shù)密碼m=";
 ??? ? cin>>m;
 ????? p->pwd=m;
 ????? p->num=i;
 ????? q=new person;
 ??? if(i==n)p->next=head; //當(dāng) i==n時,循環(huán)結(jié)束把p1指向表頭
 ??? else
 ??? ??? {
 ??? ??? p->next=q;
 ??? ??? p=q;
 ??? ??? }
 ?? }
 ?? return n;
 }
 int Fx_PersonList(PERSON *head,int n,int *a){
 ?? PERSON *p1,*p0,*p;
 ?? int tm;? //臨時變量,保存上一密碼值
 ?? int m;? //報數(shù)密碼 m
 ?? int count=1; //出隊計數(shù)
 ??? p0=p1=head;? //使p0,p1都指向head頭 
 ?? cout<<"/n請輸入 M 的 起始值 m=";
 ?? cin>>m;
 ?? while(count<=n-1)
 ???? {
 ??? ??? ? for(int i=1;i<m;i++)
 ??? ??? ??? { //把 m 重新作為報數(shù)上限值
 ??? ??? ??? p1=p0;
 ??? ??? ??? p0=p0->next;
 ??? ???? ??? tm=p0->pwd;
 ??? ??? ??? }
 ??? ??? m=tm;
 ??? ??? p=p0->next;
 ??? ??? a[count++]=p0->num;?? //把每次出隊的序號保存在a[x]中
 ??? ??? p1->next=p0->next;
 ??? ??? delete p0;
 ??? ??? p0=p;
 ???? }
 ??? a[count]=p0->num;?? //把每次出隊的序號保存在a[x]中
 ??? p1->next=p0->next;
 ??? delete p0;
 ??? return count;
 }
 void OutPut(int *a,int n)
 { 
 ? cout<<"正確的輸出序列為:";
 ???? for(int i=1;i<=n;i++)??? ?cout<<a[i]<<" ";
 ?? 
 }
轉(zhuǎn)載于:https://www.cnblogs.com/hgndinfo/archive/2006/02/09/2713948.html
總結(jié)
以上是生活随笔為你收集整理的数据结构-joseph环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 标准数字格式字符串
- 下一篇: Web的系统测试方法 (转载)
