|
发表于 2022-12-3 17:36:53
|
显示全部楼层
【对管理员说的话:别删这个帖子,好吗 】
你把所有的 node 前面都加上 struct,就像这样:
- #include <stdio.h>
- #include <stdlib.h>
- struct node{
- int date;
- struct node *next;
- };
- int main(){
- int n,m;
- scanf("%d %d",&n,&m);
- struct node *head,*p,*now,*prev;
- head = (struct node *)malloc(sizeof(struct node));
- head->date = 1;head->next = NULL;
- now = head;
- for(int i=2;i<=n;i++){
- p = (struct node *)malloc(sizeof(struct node));
- p->date=i;p->next = NULL;
- now->next = p;
- now = p;
- }
- now->next = head;
- now = head,prev = head;
- while((n--)>1){
- for(int i=1;i<m;i++){
- prev = now;
- now = now->next;
- }
- printf("%d ",now->date);
- prev->next = now->next;
- free(now);
- now = prev->next; //这里应该是 prev -> next,不应还是 prev -> date
- }
- printf("%d",now->date);
- free(now);
- return 0;
- }
复制代码
这是C语言的语法,没办法,但是你还可以这么做,就是用typedef关键字:
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node{
- int date;
- struct node *next; //但是这里还要加 struct
- } node;
- int main(){
- int n,m;
- scanf("%d %d",&n,&m);
- node *head,*p,*now,*prev;
- head = (node *)malloc(sizeof(node));
- head->date = 1;head->next = NULL;
- now = head;
- for(int i=2;i<=n;i++){
- p = (node *)malloc(sizeof(node));
- p->date=i;p->next = NULL;
- now->next = p;
- now = p;
- }
- now->next = head;
- now = head,prev = head;
- while((n--)>1){
- for(int i=1;i<m;i++){
- prev = now;
- now = now->next;
- }
- printf("%d ",now->date);
- prev->next = now->next;
- free(now);
- now = prev->next;
- }
- printf("%d",now->date);
- free(now);
- return 0;
- }
复制代码
|
评分
-
查看全部评分
|