资源描述:
《数据结构实验参考程序.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、实验一#include#include#defineMAX_NODE_NUM100#defineTRUE1U#defineFALSE0UtypedefstructNodeType{intid。intcipher。structNodeType*next。}NodeType。staticvoidCreaList(NodeType**,constint>。staticvoidStatGame(NodeType**,int>。staticvoidPrntList(co
2、nstNodeType*>。staticNodeType*GetNode(constint,constint>。staticunsignedEmptyList(constNodeType*>。intmain(void>{intn,m。NodeType*pHead=NULL。while(1>{printf("请输入人数n<最多%d个):",MAX_NODE_NUM>。scanf("%d",&n>。printf("和初始密码m:">。scanf("%d",&m>。if(n>MAX_NODE_NUM>{
3、printf("人数太多,请重新输入!">。continue。}elsebreak。}CreaList(&pHead,n>。printf("------------循环链表原始打印-------------">。b5E2RGbCAPPrntList(pHead>。printf("--------------出队情况打印---------------">。p1EanqFDPwStatGame(&pHead,m>。printf(""约瑟夫环"问题完成!">。return
4、0。}staticvoidCreaList(NodeType**ppHead,constintn>DXDiTa9E3d20/20{inti,iCipher。NodeType*pNew,*pCur。for(i=1。i<=n。i++>{printf("输入第%d个人的密码:",i>。scanf("%d",&iCipher>。pNew=GetNode(i,iCipher>。if(*ppHead==NULL>{*ppHead=pCur=pNew。pCur->next=*ppHead。}else{pNew-
5、>next=pCur->next。pCur->next=pNew。pCur=pNew。}}printf("完成单向循环链表的创建!">。}staticvoidStatGame(NodeType**ppHead,intiCipher>RTCrpUDGiT{intiCounter,iFlag=1。NodeType*pPrv,*pCur,*pDel。pPrv=pCur=*ppHead。while(pPrv->next!=*ppHead>pPrv=pPrv->next。while(iFlag>{for
6、(iCounter=1。iCounter{pPrv=pCur。pCur=pCur->next。}if(pPrv==pCur>iFlag=0。pDel=pCur。pPrv->next=pCur->next。pCur=pCur->next。iCipher=pDel->cipher。printf("第%d个人出列,密码:%d",pDel->id,pDel->cipher>。free(pDel>。}*ppHead=NULL。20/20}staticvoidPrn
7、tList(constNodeType*pHead>{constNodeType*pCur=pHead。if(EmptyList(pHead>>return。do{printf("第%d个人,密码:%d",pCur->id,pCur=pCur->next。5PCzVD7HxA}while(pCur!=pHead>。}staticNodeType*GetNode(constintiId,constintiCipher>jLBHrnAILg{NodeType*pNew。pNew=(NodeType
8、*>malloc(sizeof(NodeType>>。if(!pNew>{printf("Error,thememoryisnotenough!">。exit(-1>。}pNew->id=iId。pNew->cipher=iCipher。pNew->next=NULL。returnpNew。}staticunsignedEmptyList(constNodeType*pHead>{if(!pHead>{printf("Thelistisempty!">。returnTRUE