Created
April 13, 2013 00:59
-
-
Save yanhaijing/5376343 to your computer and use it in GitHub Desktop.
上机约瑟夫环链表实验
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*上机约瑟夫环链表实验*/ | |
/*版权 颜海镜 所有*/ | |
#include<stdio.h> | |
#include <malloc.h> | |
#include "stdlib.h" | |
typedef struct Node | |
{ | |
int data; | |
struct Node *next; | |
} *nodetype ; | |
nodetype creatList(nodetype head,int x) | |
{ | |
int i; | |
nodetype p,q; | |
head=(nodetype)malloc(sizeof(struct Node)); | |
head->data=x; | |
p=head; | |
for(i=1;i<x;i++) | |
{ | |
q=(nodetype)malloc(sizeof(struct Node)); | |
if(!q) exit(1); | |
q->data=i; | |
p->next=q; | |
p=q; | |
} | |
p->next=head; | |
return head; | |
} | |
void del (nodetype Previous,nodetype Point) | |
{ | |
Previous->next=Point->next; | |
free(Point); | |
} | |
int main() | |
{ | |
int all,count,start,i,j,out; | |
nodetype Node,point,previous,pointer; | |
Node=NULL; | |
system("cls"); | |
printf("请输入总人数\n"); | |
scanf("%d",&all); | |
Node=creatList(Node,all); | |
printf("请输入周期\n"); | |
scanf("%d",&count); | |
printf("请输入起始位置\n"); | |
scanf("%d",&start); | |
if (start>all) | |
{ | |
printf("溢出!!! 请仔细检查您输入的数据...\n"); | |
system("pause"); | |
} | |
else | |
{ | |
point=Node; | |
j=1; | |
while(j<=start) | |
{ | |
previous=point; | |
point=point->next; | |
j++; | |
} | |
out=0; | |
while(out<all-1) | |
{ | |
for(j=1;j<count;j++) | |
{ | |
point=point->next; | |
previous=previous->next; | |
} | |
printf(" %d",point->data); | |
pointer=point->next; | |
del(previous,point); | |
point=pointer; | |
out++; | |
} | |
printf(" %d\n",point->data); | |
free(point); | |
printf("\n"); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment