?? f22.c
字號:
#include<stdio.h>
#include<stdlib.h>
struct Lnode /*循環鏈表的存儲結構*/
{
int number;
int password;
struct Lnode *next;
}* p,*q,*head;
struct Lnode *creat(int n) /*創建循環鏈表,參數代表結點個數*/
{
int i;
int m;
head=p=(struct Lnode*)malloc(sizeof(struct Lnode)); /*建立第一個節點*/
if(p==0) return(0); /*存儲分配失敗*/
for(i=1;i<=n;i++)
{
printf("please enter the %d people's password:",i); /*取得每個人的密碼*/
scanf("%d",&(p->password));
if((p->password)<=0) /*輸入的密碼不合法*/
{
printf("password is irregular\n") ;
return 0;
}
p->number=i;
if(i<n) /*不是最后一個節點*/
{
q=(struct Lnode*)malloc(sizeof(struct Lnode)); /*q指向新建節點*/
if(q==0) return(NULL); /*存儲分配失敗*/
p->next=q; /*在第一個節點之后加入*/
p=q;
}
}
p->next=head; /*鏈表構造完成后使最后一個節點指向第一節點構成循環鏈表*/
return(head); /*返回鏈表的首地址*/
}
int main(void)
{
int n;
int i;
int m;
printf("please enter the number of people n:"); /*獲得最初圍坐的人數*/
scanf("%d",&n);
if(n<=0) /*輸入的n不合法*/
{
printf("n is irregular\n") ;
return 0;
}
printf("please enter the number m:"); /*獲得開始的報數上限值*/
scanf("%d",&m);
if(m<=0) /*輸入的m不合法*/
{
printf("n is irregular\n") ;
return 0;
}
q=creat(n); /*調用創建鏈表函數,取得鏈表首地址*/
if(p==0) {return (0);}/*創建鏈表失敗*/
printf("chu lie shun xu:\n") ; /*輸出提示*/
/*輸出結果部分*/
while(n) /*鏈表不空*/
{
for(;m>1;--m) /*找到要出列的人*/
{
q=q->next;
p=p->next;
}
m=q->password;
printf("%d ",q->number); /*輸出出列人的號碼*/
p->next=q->next;
free(q); /*刪除出列節點*/
q=p->next;
n--;
}
getch();
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -