不失微笑 发表于 2017-8-3 12:48:29

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

#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)
{:5_100:}

ba21 发表于 2017-8-3 13:16:13



#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;
        LNODE *s;
        int i = 1;

        head=(LinkList)malloc(sizeof(LNODE));
        p=head;

        if(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;
}
页: [1]
查看完整版本: 数据结构约瑟夫问题求救,照着小甲鱼的视频写的