/* Note:Your choice is C IDE */
#include <stdlib.h>
#include "stdio.h"
#define NULL 0
#define LEN sizeof(struct circu)
struct circu
{
int num;
int code;
struct circu*next;
};
int n;
int ini(void)
{
char a[10];
printf("Please input the number of N:");
scanf("%s",a);
n=atoi(a); return(n);
}
void slov()
{
while(!n||n<0)
{printf("\nYou input N is wrong!");
ini();}
printf("\nYou input N is:%d\n",n);
}
int cod;
int ciph(void)
{
char cipher[10];
printf("\nPlease input the number of code:");
scanf("%s",cipher);
cod=atoi(cipher);
return(cod);
}
void slo()
{
struct circu*creat(void);
char c;
while(!cod||cod<0)
{
printf("\nYou input CODE is wrong!\n");
ciph();
}
}
struct circu*creat(void)
{
int i=1;
struct circu*head;
struct circu*p1,*p2;
p1=p2=(struct circu*)malloc(LEN);
ciph();slo();
p1->code=cod;
p1->num=i;
head=NULL;
while(i<=n)
{
if(i==1)
{head=p1;}
else
{p2->next=p1;
p2=p1;
p1=(struct circu*)malloc(LEN);
ciph();slo();
p1->code=cod;
p1->num=i;}
i++;
}
p2->next=p1;
p2=p1;
p1=(struct circu*)malloc(LEN);
p1->code=0;
p2->next=NULL;
return(head);
}
void print(struct circu*head)
{
int i;
struct circu*p;
printf("\nThese %d records are:",n);
p=head;
if(head!=NULL)
do
{
printf("\n");
for(i=1;i<=n;i++)
{
printf("%-5d",p->num);
p=p->next;
}
p=head;
printf("\n");
for(i=1;i<=n;i++)
{
printf("%-5d",p->code);
p=p->next;
}
}
while(p!=NULL);
printf("\n");
}
int m;
int init(void)
{
char n[10];
printf("\nPlease input the number of M:");
scanf("%s",n);
m=atoi(n); return(m);
}
void slove()
{
while(!m||m<0)
{printf("\nYou input M is wrong!\n");
init();}
printf("\nYou input number is:%d\n",m);
printf("\n");
}
void choose(struct circu*head)
{
struct circu*p,*p1;
int i;
int j;
p1=p=head;
printf("Correct dequeue order is:\n\n");
for(j=1;j<=n;j++)
{
for(i=1;i<m;i++)
{
if(p->next!=NULL)
{p1=p;p=p->next;}
else
{p1=p;p=head;}
}
m=p->code;
printf("%-5d",p->num);
if(p==head)
{
head=p->next;
free(p);
p1->next=head;
p=p->next;
}
else if(p->next==NULL)
{
p->next=head;
p1->next=p->next;
free(p);
p=p->next;
}
else
{
p1->next=p->next;
free(p);
p=p->next;
}
}
}
main()
{
char k='Y';
while(k=='Y')
{struct circu*head;
clrscr();
ini();slov();
head=creat();
print(head);
init();slove();
choose(head);
printf("\nIf you want to create it again(Y/N):");
getchar();k=getchar();}
}