文章
18
粉丝
0
获赞
3
访问
10.8k
猴子报数(击鼓传花)用不带头结点的循环链表
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}node,*LinkList;
LinkList create(int n) {
node* head = (node*)malloc(sizeof(node));
head->data = 1;
head->next = head;//首结点
node* p = head;
for (int i = 1; i < n; i++) {
node* pnode = (node*)malloc(sizeof(node));
pnode->data = i + 1;
p->next = pnode;
pnode->next = head;
p = p->next;
}
return head;
}
void print(node *head,int len,int s,int m){
node* p = head;
while (p->data != s)//定位到起始点
p = p->next;
int count = 1;
while (p->next != p) {//自己指向自己则为最后一个结点
if (count == m - 1) {//p为待删除结点的前驱
count = 1;//重置
node* q = p->next;
printf("%d,", q->data);
p->next = q->next;
free(q);
}
else {
count++;
}
p = p->next;
}
printf("%d\n", p->data);//输出最后一个结点
}
int main() {
int n,s,m;
scanf("%d", &n);
while(n!= 0) {
node* hea...
登录后发布评论
暂无评论,来抢沙发