#include"stdio.h"
#include"stdlib.h"
#define MAXPERSONNUMBER 100
typedef struct Node
{
int data;
int password;
struct Node *next;
}Node, *LinkList;
void CreatLinkList(LinkList *);
void InitLinkList(LinkList *,int );
int personnumber();
int GetPassword();
int password();
int GetPersonNumber();
int GetFirstCountValue();
void GetOutputOrder(LinkList* , int, int, int* );
void printResult(int * ,int );
void CreatLinkList(LinkList *L)
{
(*L) = (LinkList)malloc(sizeof(Node));
if ((*L) == NULL)
{
printf("failed ");
exit(1);
}
}
void InitLinkList(LinkList *L, int personNumber)
{
Node *p, *q;
int i ;
p = (*L);
p->data = 1;
p->password = GetPassword();
for (i = 2; i <= personNumber; i++)
{
q = (LinkList)malloc(sizeof(Node));
if (q == NULL)
{
printf("failed");
exit(1);
}
q->password = GetPassword();
q->data = i;
p->next = q;
p = q;
}
p->next = (*L);
}
int GetPassword()
{
int password;
static int count = 1;
printf("\n请输入第%d的密码:",count);
scanf("%d",&password);
while (password < 0)
{
printf("您输入的数字无效,请输入在0到%d的整数:",password);
scanf("%d",&password);
}
printf("第%d个人的密码为%d",count,password);
count++;
return password;
}
int GetPersonNumber()
{
int personNumber;
printf("请输入需要输入人的数目:");
scanf("%d",&personNumber);
while (personNumber > MAXPERSONNUMBER || personNumber < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",MAXPERSONNUMBER);
scanf("%d",&personNumber);
}
printf("最终确定的人数为%d\n",personNumber);
return personNumber;
}
int GetFirstCountValue()
{
int firstCountValue;
printf("请输入初始的上限值");
scanf("%d",&firstCountValue);
while (firstCountValue < 0)
{
printf("\n你输入的数字无效,请输入在0到%d的整数",firstCountValue);
scanf("%d",&firstCountValue);
}
printf("最终的上限值为%d",firstCountValue);
return firstCountValue;
}
void GetOutputOrder(LinkList *L, int personnumber, int reportValue, int array[MAXPERSONNUMBER])
{
Node *p, *q;
int count = 1, i = 0;
p = (*L);
while (personnumber)
{
while (count != reportValue)
{
q = p;
p = p->next;
count++;
}
array[i++] = p ->data;
reportValue = p->password;
q->next = p->next;
free(p);
p = q->next;
count = 1;
personnumber--;
}
}
void printResult(int array[],int personnumer)
{
int i;
printf("\n出队的顺序为:");
for(i = 0; i < personnumer; i++)
{
printf("%-3d",array[i]);
}
printf("\n");
}
int main(void)
{
LinkList L;
int personnumber, reportValue;
int array[MAXPERSONNUMBER];
personnumber = GetPersonnumber();
reportValue = GetFirstCountValue();
CreatLinkList(&L);
InitLinkList(&L, personnumber);
GetOutputOrder(&L, personnumber, reportValue,array);
printResult(array, personnumber);
system("pause");
return 0;
getchar();getchar();
}