你刚刚给出的代码中,prev程序好像无法识别:node* FindJoseph(node*, int)’: main2.c:61:21: error: ‘struct node’ has no member named ‘prev’ node *q = head->prev;,请修改代码
时间: 2024-03-18 07:39:07 浏览: 19
非常抱歉,代码中的 `prev` 确实是一个错误。在 `struct node` 中没有定义 `prev` 成员。要想访问链表的最后一个节点,可以使用 `head->prev`。需要将其修改为 `head->prev_node` 或者 `head->prev` 改为 `head->prev_node`。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
struct node {
int num;
struct node *next;
struct node *prev_node;
};
node* FindJoseph(node *head, int k) {
node *p = head;
while (p->next != p) {
for (int i = 1; i < k; i++) {
p = p->next;
}
// 删除第 k 个节点
node *q = p->next;
p->next = q->next;
q->next->prev_node = p;
printf("出圈人的编号:%d\n", q->num);
free(q);
}
return p;
}
int main() {
int n, k;
printf("请输入总人数n和报数k:\n");
scanf("%d %d", &n, &k);
node *head, *tail;
head = (node *) malloc(sizeof(node));
tail = head;
for (int i = 1; i <= n; i++) {
node *p = (node *) malloc(sizeof(node));
p->num = i;
tail->next = p;
p->prev_node = tail;
tail = p;
}
tail->next = head;
head->prev_node = tail;
node *winner = FindJoseph(head, k);
printf("胜出者的编号为:%d\n", winner->num);
free(winner);
return 0;
}
```
希望这次的修改能够解决您的问题。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)