|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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)
试试这个
- #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;
- Node k;
- int i = 1;
- head = (Node)malloc(sizeof(LNode));
- p = head;
-
- 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;
- }
复制代码
|
|