鱼C论坛

 找回密码
 立即注册
查看: 2559|回复: 8

[已解决]这个问题还是不行,求各位大神帮忙指点一下,万分感谢

[复制链接]
发表于 2018-4-22 22:13:03 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是小甲鱼老师布置的那个关于约瑟夫问题的作业,一直找不出报错的在哪 ,在这里求各位路过的大神指点迷津,万分感谢,万分感谢!!!

#include<stdio.h>
#include<stdlib.h>
#define N 41
typedef struct LNode
{
      int data;
      int num;
      struct LNode * next;
}LNode, * Node;

Node  create(int n)
{
      Node p = NULL, head;
      head = (Node)malloc(sizeof(LNode));
      p = head;
      Node  k;
      int i=1;
      if( n != 0 )
      {
            while(i <= n)
            {
                  k = (Node)malloc(sizeof(LNode));
                  k->data = i++;
                  k->num = rand()%100;
                  p->next = k;
                  p = k;
            }
            k->next = head->next;
            free(head);
      }
            return (k->next);
}
      
      int main()
      {
            int n = N, m, i;
            Node p = create(n), temp;
            m = p->num;
            while( p != p->next)
            {
                  for(i=1; i<m; i++)
                      p = p->next;
                  printf("%d-->",p->next->data);
                  m = p->next->num;
                  temp = p->next;
                  p->next = temp->next;
                  free(temp);
                  p = p->next;
            }
            printf("%d",p->data);
            return 0;
      }


//下面是编译时的报错
Compiling...
约瑟夫.c
E:\VC程序\约瑟夫问题\约瑟夫.c(16) : error C2275: 'Node' : illegal use of this type as an expression
        E:\VC程序\约瑟夫问题\约瑟夫.c(9) : see declaration of 'Node'
E:\VC程序\约瑟夫问题\约瑟夫.c(16) : error C2146: syntax error : missing ';' before identifier 'k'
E:\VC程序\约瑟夫问题\约瑟夫.c(16) : error C2065: 'k' : undeclared identifier
E:\VC程序\约瑟夫问题\约瑟夫.c(17) : error C2143: syntax error : missing ';' before 'type'
E:\VC程序\约瑟夫问题\约瑟夫.c(20) : error C2065: 'i' : undeclared identifier
E:\VC程序\约瑟夫问题\约瑟夫.c(22) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct LNode *'
E:\VC程序\约瑟夫问题\约瑟夫.c(23) : error C2223: left of '->data' must point to struct/union
E:\VC程序\约瑟夫问题\约瑟夫.c(24) : error C2223: left of '->num' must point to struct/union
E:\VC程序\约瑟夫问题\约瑟夫.c(25) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
E:\VC程序\约瑟夫问题\约瑟夫.c(26) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
E:\VC程序\约瑟夫问题\约瑟夫.c(28) : error C2223: left of '->next' must point to struct/union
E:\VC程序\约瑟夫问题\约瑟夫.c(31) : error C2223: left of '->next' must point to struct/union
E:\VC程序\约瑟夫问题\约瑟夫.c(31) : warning C4033: 'create' must return a value
执行 cl.exe 时出错.

约瑟夫.obj - 1 error(s), 0 warning(s)
最佳答案
2018-4-22 23:14:19
试试这个
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 41
  4. typedef struct LNode
  5. {
  6.         int data;
  7.         int num;
  8.         struct LNode * next;
  9. }LNode, *Node;

  10. Node  create(int n)
  11. {
  12.         Node p = NULL, head;
  13.         Node  k;
  14.         int i = 1;

  15.         head = (Node)malloc(sizeof(LNode));
  16.         p = head;
  17.        
  18.         if(n != 0)
  19.         {
  20.                 while(i <= n)
  21.                 {
  22.                         k = (Node)malloc(sizeof(LNode));
  23.                         k->data = i++;
  24.                         k->num = rand() % 100;
  25.                         p->next = k;
  26.                         p = k;
  27.                 }
  28.                 k->next = head->next;
  29.                 free(head);
  30.         }
  31.         return (k->next);
  32. }

  33. int main()
  34. {
  35.         int n = N, m, i;
  36.         Node p = create(n), temp;
  37.         m = p->num;
  38.         while(p != p->next)
  39.         {
  40.                 for(i = 1; i<m; i++)
  41.                         p = p->next;
  42.                 printf("%d-->", p->next->data);
  43.                 m = p->next->num;
  44.                 temp = p->next;
  45.                 p->next = temp->next;
  46.                 free(temp);
  47.                 p = p->next;
  48.         }
  49.         printf("%d", p->data);
  50.         return 0;
  51. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-22 23:13:00 | 显示全部楼层
你用vc6 ?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-22 23:14:19 | 显示全部楼层    本楼为最佳答案   
试试这个
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define N 41
  4. typedef struct LNode
  5. {
  6.         int data;
  7.         int num;
  8.         struct LNode * next;
  9. }LNode, *Node;

  10. Node  create(int n)
  11. {
  12.         Node p = NULL, head;
  13.         Node  k;
  14.         int i = 1;

  15.         head = (Node)malloc(sizeof(LNode));
  16.         p = head;
  17.        
  18.         if(n != 0)
  19.         {
  20.                 while(i <= n)
  21.                 {
  22.                         k = (Node)malloc(sizeof(LNode));
  23.                         k->data = i++;
  24.                         k->num = rand() % 100;
  25.                         p->next = k;
  26.                         p = k;
  27.                 }
  28.                 k->next = head->next;
  29.                 free(head);
  30.         }
  31.         return (k->next);
  32. }

  33. int main()
  34. {
  35.         int n = N, m, i;
  36.         Node p = create(n), temp;
  37.         m = p->num;
  38.         while(p != p->next)
  39.         {
  40.                 for(i = 1; i<m; i++)
  41.                         p = p->next;
  42.                 printf("%d-->", p->next->data);
  43.                 m = p->next->num;
  44.                 temp = p->next;
  45.                 p->next = temp->next;
  46.                 free(temp);
  47.                 p = p->next;
  48.         }
  49.         printf("%d", p->data);
  50.         return 0;
  51. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-23 10:49:47 | 显示全部楼层

是啊  现在都用哪个版本了?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-23 10:55:22 | 显示全部楼层

这个可以,我在对比一下看看问题出在哪,谢谢大神的指点,感谢感谢,顺便问一下,老大你用的是哪个软件呀?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-23 16:12:47 | 显示全部楼层
NILL_X 发表于 2018-4-23 10:55
这个可以,我在对比一下看看问题出在哪,谢谢大神的指点,感谢感谢,顺便问一下,老大你用的是哪个软件呀 ...

vs2017
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-23 16:13:19 | 显示全部楼层
NILL_X 发表于 2018-4-23 10:55
这个可以,我在对比一下看看问题出在哪,谢谢大神的指点,感谢感谢,顺便问一下,老大你用的是哪个软件呀 ...

visual studio2017
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-23 16:34:39 | 显示全部楼层

你那安装软件能发一个给我吗
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-23 19:43:11 | 显示全部楼层
NILL_X 发表于 2018-4-23 16:34
你那安装软件能发一个给我吗

https://msdn.itellyou.cn/

无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 02:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表