鱼C论坛

 找回密码
 立即注册
查看: 3300|回复: 1

[已解决]数据结构约瑟夫问题求救,照着小甲鱼的视频写的

[复制链接]
发表于 2017-8-3 12:48:29 | 显示全部楼层 |阅读模式

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

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

x
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
typedef struct LNode* LinkList;

LNode* CreateList(int n)
{
LNode *p=NULL,*head;
head=(LinkList)malloc(sizeof(LNode));
p=head;
LNode *s;
int i = 1 ;
for(n != 0)
{
while( i <= n )
        {
        s=(LinkList)malloc(sizeof(LNode));
        s->data=i++;
        p->next=s;
        p=s;
        }       
s->next=head->next;
}
return s->next;
}



int main()
{
        int n=41;
        int m=3;
        int i;
        LNode *p=CreateList(n);
        LNode *temp;
        m=m%n;
        while(p!=p->next)
        {
        for(i=1;i<m-1;i++)
        {
        p=p-> next;
        }
        printf("%d",p->next->data);
       
        temp=p->next;
        p->next=temp->next;
        free(temp);
        p=p->next;

        }
        printf("%d",p->data);
return 0;

}







--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.c
C:\Users\LSZ\Desktop\1.c(15) : error C2275: 'LNode' : illegal use of this type as an expression
        C:\Users\LSZ\Desktop\1.c(7) : see declaration of 'LNode'
C:\Users\LSZ\Desktop\1.c(15) : error C2065: 's' : undeclared identifier
C:\Users\LSZ\Desktop\1.c(16) : error C2143: syntax error : missing ';' before 'type'
C:\Users\LSZ\Desktop\1.c(17) : error C2143: syntax error : missing ';' before ')'
C:\Users\LSZ\Desktop\1.c(17) : error C2143: syntax error : missing ';' before ')'
C:\Users\LSZ\Desktop\1.c(17) : warning C4552: '!=' : operator has no effect; expected operator with side-effect
C:\Users\LSZ\Desktop\1.c(19) : error C2065: 'i' : undeclared identifier
C:\Users\LSZ\Desktop\1.c(21) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct LNode *'
C:\Users\LSZ\Desktop\1.c(22) : error C2223: left of '->data' must point to struct/union
C:\Users\LSZ\Desktop\1.c(23) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
C:\Users\LSZ\Desktop\1.c(24) : warning C4047: '=' : 'struct LNode *' differs in levels of indirection from 'int '
C:\Users\LSZ\Desktop\1.c(26) : error C2223: left of '->next' must point to struct/union
C:\Users\LSZ\Desktop\1.c(28) : error C2223: left of '->next' must point to struct/union
C:\Users\LSZ\Desktop\1.c(28) : warning C4033: 'CreateList' must return a value
执行 cl.exe 时出错.

1.exe - 1 error(s), 0 warning(s)

最佳答案
2017-8-3 13:16:13
2017-08-03_131429.png

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct LNode
  4. {
  5.         int data;
  6.         struct LNode *next;
  7. }LNODE;

  8. typedef struct LNode *LinkList;

  9. LNODE *CreateList(int n)
  10. {
  11.         LNODE *p=NULL,*head;
  12.         LNODE *s;
  13.         int i = 1;

  14.         head=(LinkList)malloc(sizeof(LNODE));
  15.         p=head;

  16.         if(n != 0)
  17.         {
  18.                 while( i <= n )
  19.         {
  20.                         s=(LinkList)malloc(sizeof(LNODE));
  21.                         s->data=i++;
  22.                         p->next=s;
  23.                         p=s;
  24.         }        
  25.                 s->next=head->next;
  26.         }
  27.         return s->next;
  28. }



  29. int main()
  30. {
  31.         int n=41;
  32.         int m=3;
  33.         int i;

  34.         LNODE *p=CreateList(n);
  35.         LNODE *temp;
  36.         m=m%n;
  37.         while(p!=p->next)
  38.         {
  39.                         for(i=1;i<m-1;i++)
  40.                         {
  41.                                 p=p-> next;
  42.                         }
  43.                         printf("%d",p->next->data);
  44.         
  45.                         temp=p->next;
  46.                         p->next=temp->next;
  47.                         free(temp);
  48.                         p=p->next;

  49.         }
  50.         printf("%d",p->data);

  51.                 return 0;
  52. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-8-3 13:16:13 | 显示全部楼层    本楼为最佳答案   
2017-08-03_131429.png

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct LNode
  4. {
  5.         int data;
  6.         struct LNode *next;
  7. }LNODE;

  8. typedef struct LNode *LinkList;

  9. LNODE *CreateList(int n)
  10. {
  11.         LNODE *p=NULL,*head;
  12.         LNODE *s;
  13.         int i = 1;

  14.         head=(LinkList)malloc(sizeof(LNODE));
  15.         p=head;

  16.         if(n != 0)
  17.         {
  18.                 while( i <= n )
  19.         {
  20.                         s=(LinkList)malloc(sizeof(LNODE));
  21.                         s->data=i++;
  22.                         p->next=s;
  23.                         p=s;
  24.         }        
  25.                 s->next=head->next;
  26.         }
  27.         return s->next;
  28. }



  29. int main()
  30. {
  31.         int n=41;
  32.         int m=3;
  33.         int i;

  34.         LNODE *p=CreateList(n);
  35.         LNODE *temp;
  36.         m=m%n;
  37.         while(p!=p->next)
  38.         {
  39.                         for(i=1;i<m-1;i++)
  40.                         {
  41.                                 p=p-> next;
  42.                         }
  43.                         printf("%d",p->next->data);
  44.         
  45.                         temp=p->next;
  46.                         p->next=temp->next;
  47.                         free(temp);
  48.                         p=p->next;

  49.         }
  50.         printf("%d",p->data);

  51.                 return 0;
  52. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 14:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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