69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct circle
{
int people;
int count = 0;
struct circle *next;
}one;
//创建链表环
one *create(int n)
{
one *head, *p1, *p2, t;
head = NULL;
printf("input the numbers of the people:\n");
for (int i = 0;i < n;i++)
{
p1 = (one *)malloc(sizeof(one));
if (!p1) {
exit(1); //开辟空间,如果分配失败退出
}
scanf("%d", &p1->people);
t.count++; //这里计数没有用,除非将t声明成全局变量,或者作为引用参数传进来
if (head == NULL) {
head = p1;
} else {
p2->next = p1;
}
p2 = p1;
}
p2->next = head;
return head;
}
int main()
{
one *head, *p, *del, t;
int n, i = 2, dis;
printf("please input the people numbers: \n");
scanf("%d", &n);
head = create(n);
p = head;
t.count = n; // 要计数的
printf("please input the distance you want to selete:\n");
scanf("%d", &dis);
while (1) //无限循坏直到只剩一人break退出
{
if (t.count == 2 && i == dis) //只剩两人时
{
printf("the no.%d is out \n", p->next->people);
del = p->next;
p->next = NULL;
free(del);
t.count--;
printf("the no.%d is the last person int the end:\n", p->people);
break;
} // if()
if (i == dis) //遇到等距数dis则删除结点,继续循环
{
printf("the no.%d is out \n", p->next->people);
del = p->next;
p->next = del->next;
free(del); //删除结点
t.count--; //相应人数减一
i = 1; //重新计数
} // if( )
p = p->next;
i++;
} // while()
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct node
{
int num;
char name[20];
struct node *next;
};
struct node *create_singly_link(int num)
{
struct node*head, *prev, *cur;
int i;
head = NULL;
for(i = 0; i < num; i++) {
cur = (struct node *)malloc(sizeof(struct node));
if (!cur) {
printf("Malloc error!");
return NULL;
}
cur->next = NULL;
cur->num = i+1;
snprintf(cur->name, 20-1, "%c", 'A'+i);
if (!head) {
head = cur;
prev = cur;
} else {
prev->next = cur;
prev = prev->next;
}
}
prev->next = head;
return head;
}
static void test_print(struct node *phead)
{
struct node *pcur = phead->next;
printf("num = %d\t name = %s\n", phead->num, phead->name);
while (pcur != phead) {
printf("num = %2d\t name = %s\n", pcur->num, pcur->name);
pcur = pcur->next;
}
}
int output( struct node *head, int start, int step, int total)
{
struct node *p, *q;
int count = 0;
int num = 0, i;
q = head;
for(i = 0; i < start; i++)
q = q->next;
while(count < total-1) {
p = q->next;
num++;
if(!(num % step)) {
q->next = p->next;
printf("%s\n", p->name);
free(p);
count++;
} else {
q = p;
}
}
printf("num = %d\t, name = %s\n", q->num, q->name);
free(q);
return 0;
}
int main(int argc, const char *argv[])
{
int total, step, start;
struct node *head;
printf("Please input total test number: ");
scanf("%d", &total);
head = create_singly_link(total);
test_print(head);
printf("Please input start position and steps: ");
scanf("%d%d", &start, &step);
output(head, start, step, total);
return 0;
}
约瑟夫环,可以参考一下这个程序。#include<stdio.h>
//#include<malloc.h>
#include<stdlib.h>
typedef struct circle
{
int people;
struct circle *next;
}one;
int count;
//创建链表环
one *create(int n)
{
one *head,*p1,*p2;
int i;
head = NULL;
for(i = 0; i < n; i++)
{
p1 = (one *)malloc(sizeof(one));
if(!p1)
exit(1); //开辟空间,如果分配失败退出
scanf("%d", &p1->people);
count++;
if(!head)
head = p1;
else
p2->next = p1;
p2 = p1;
}
p2->next = head;
return head;
}
void print_cicle(one *head)
{
one *p = head;
int i = 0;
while (i < 6) {
printf("%d\t", p->people);
p = p->next;
i++;
}
putchar(10);
}
int main()
{
one *head, *p, *pre;
int n, i = 2, dis;
printf("please input the people numbers: \n");
scanf("%d",&n);
head = create(n);
printf("count = %d\n", count);
print_cicle(head);
p = head;
pre = p;
printf("please input the distance you want to selete:\n");
scanf("%d", &dis);
i = 0;
while (1) {
i++;
//printf("+++++No.%d\n", p->people);
if (count == 1)
break;
if (i % dis == 0) {
printf("the no.%d is out \n", p->people);
if (p == head) {
head = head->next;
free(p);
pre = head;
p = head;
} else {
pre->next = p->next;
free(p);
p = pre->next;
}
count--;
} else {
pre = p;
p = p->next;
}
}
printf("the no.%d is the last person int the end:\n", p->people);
free(p);
/*
while(1) //无限循坏直到只剩一人break退出
{
if(t.count==2&&i==dis) //只剩两人时
{
printf("the no.%d is out \n",p->next->people);
del=p->next;
p->next=NULL;
free(del);
t.count--;
printf("the no.%d is the last person int the end:\n",p->people );
break;
} // if()
if(i==dis) //遇到等距数dis则删除结点,继续循环
{
printf("the no.%d is out \n",p->next->people);
del=p->next ;
p->next=del->next;
free(del); //删除结点
t.count--; //相应人数减一
i=1; //重新计数
} // if( )
p = p->next;
i++;
} // while()
*/
return 0;
}
参考一下吧