约瑟夫环,请帮忙讲解一下 数据结构 10
#include<stdio.h>#include<stdlib.h>typedefstruct_Node{intinfo;struct_Node*next;}Node;...
#include<stdio.h>
#include<stdlib.h>
typedef struct _Node {
int info;
struct _Node* next;
} Node; //结构体info为节点编号,next 为指向下一个节点的指针
//删除node之后的第一个节点
Node* delNode(Node* node)
{ Node* temp = NULL; //定义一个临时变量并指向空
if(node != NULL && node->next != node) //判断线性表是否为空
{ temp = node->next;
node->next = node->next->next;
}
return temp;
} //在node之后添加一个节点
int addNode(Node* node, int info)
{ Node* temp;
if(node != NULL)
{ temp = (Node*)malloc(sizeof(Node));
temp->info = info;
temp->next = node->next;
node->next = temp;
return 1; }
return 0; } //构建一个长度为n的约瑟夫环
Node* buildLink(int n)
{ Node* head = (Node*)malloc(sizeof(Node)); //生成一个node型的头结点并为其动态分配一个node大小的存储空间
int i;
head->info = 1;
head->next = head;
for(i=n-1; i>0; i--)
{ addNode(head, i+1); }
return head;
}
int main()
{ int guyNum;
int countNum;
int beginnum;
int i;
Node* guys;
printf("Please input the number of the guy who take part in this game\n");
scanf("%d", &guyNum);
printf("And input the number you will count\n");
scanf("%d", &countNum);
printf("please input the beginning number\n");
scanf("%d",&beginnum);
guys = buildLink(guyNum);
for(i=1;i<beginnum;i++)
{ guys=guys->next; }
printf("The sequency is ");
while(countNum!=1&&guys != guys->next) { //数countNum下,i=1因为当前节点被数作1 //i<countNum-1因为单向链表只能删后一个节点,所以在数到最后一个之前删除
for(i=1; i<countNum-1; i++)
{ guys = guys->next; }
printf("%d ", delNode(guys)->info); //将在for循环中省去的1次补上
guys = guys->next; }
printf("%d ", guys->info);
if(countNum==1)
{
for(i=1;i<=guyNum-1;i++)
{ guys=guys->next;
printf("%4d",guys->info); }
}
return 0; } 展开
#include<stdlib.h>
typedef struct _Node {
int info;
struct _Node* next;
} Node; //结构体info为节点编号,next 为指向下一个节点的指针
//删除node之后的第一个节点
Node* delNode(Node* node)
{ Node* temp = NULL; //定义一个临时变量并指向空
if(node != NULL && node->next != node) //判断线性表是否为空
{ temp = node->next;
node->next = node->next->next;
}
return temp;
} //在node之后添加一个节点
int addNode(Node* node, int info)
{ Node* temp;
if(node != NULL)
{ temp = (Node*)malloc(sizeof(Node));
temp->info = info;
temp->next = node->next;
node->next = temp;
return 1; }
return 0; } //构建一个长度为n的约瑟夫环
Node* buildLink(int n)
{ Node* head = (Node*)malloc(sizeof(Node)); //生成一个node型的头结点并为其动态分配一个node大小的存储空间
int i;
head->info = 1;
head->next = head;
for(i=n-1; i>0; i--)
{ addNode(head, i+1); }
return head;
}
int main()
{ int guyNum;
int countNum;
int beginnum;
int i;
Node* guys;
printf("Please input the number of the guy who take part in this game\n");
scanf("%d", &guyNum);
printf("And input the number you will count\n");
scanf("%d", &countNum);
printf("please input the beginning number\n");
scanf("%d",&beginnum);
guys = buildLink(guyNum);
for(i=1;i<beginnum;i++)
{ guys=guys->next; }
printf("The sequency is ");
while(countNum!=1&&guys != guys->next) { //数countNum下,i=1因为当前节点被数作1 //i<countNum-1因为单向链表只能删后一个节点,所以在数到最后一个之前删除
for(i=1; i<countNum-1; i++)
{ guys = guys->next; }
printf("%d ", delNode(guys)->info); //将在for循环中省去的1次补上
guys = guys->next; }
printf("%d ", guys->info);
if(countNum==1)
{
for(i=1;i<=guyNum-1;i++)
{ guys=guys->next;
printf("%4d",guys->info); }
}
return 0; } 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏10(财富值+成长值)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询