首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

约瑟夫环~ 编译失误!

2013-10-14 
约瑟夫环~~ 编译出错!!!!#includestdio.h#includestdlib.hstruct node//运用结构体定义链表{int numbe

约瑟夫环~~ 编译出错!!!!
#include<stdio.h>
#include<stdlib.h>
struct node   //运用结构体定义链表
{
    int number;         //序号
int password;       //密码
    struct node *next; //指向本结构体的结点指针
}link,*p,*q,*head;     //node类变量link,结点p、q,头指针head
link *creat()  //创建循环链表
{
int n;
int i;
    for(i=1;i<=n;i++)
{
if(i==1)
{
head=p=(link*)malloc(sizeof(struct link));//开辟新空间
}
else
{
q=(struct link*)malloc(sizeof(struct link));
p->next=q;
p=q;     //连接结点
}
}

p->next=head;  //头尾相连,形成循环链表
p=head;
return head;
}
void Delete()  //实现某人出列后重新开始,只要删掉此人所在结点即可
{
int n,m;
for (int j=1;j<=n;j++)
{
for(int i=1;i<m;i++)
{
p=p->next;   //报数小于m时,指针后移
}
m=p->password;
p->number=p->next->number;
p->password=p->next->password;
q=p->next;
p->next=p->next->next; 
free(q);  //释放p的内存
printf("%d",p->number);  //输出编号
}
}
int main()
{
    int i,j;
int n; //人数
int m;//初始值m
    printf("请输入总人数\n");
    scanf("%d",&n);    //输入整型变量n
    link *creat();
    printf("输入这些人的密码\n");
    for (i=0;i<n;i++)
    {
        scanf("%d",&(p->password));
    }
    printf("请输入M的初值\n");
    scanf("%d",&m); //输入整型变量m
printf("出列顺序为:");
    Delete();
    printf("\n");
    return 0;
system("pause");
}



编译报错为:
C:\Users\xuqianqian\Desktop\yuesefu.cpp(9) : error C2143: syntax error : missing ';' before '*'
C:\Users\xuqianqian\Desktop\yuesefu.cpp(9) : error C2501: 'link' : missing storage-class or type specifiers
C:\Users\xuqianqian\Desktop\yuesefu.cpp(9) : error C2371: 'link' : redefinition; different basic types
        C:\Users\xuqianqian\Desktop\yuesefu.cpp(8) : see declaration of 'link'
C:\Users\xuqianqian\Desktop\yuesefu.cpp(10) : error C2501: 'creat' : missing storage-class or type specifiers
C:\Users\xuqianqian\Desktop\yuesefu.cpp(17) : error C2059: syntax error : ')'
C:\Users\xuqianqian\Desktop\yuesefu.cpp(21) : error C2027: use of undefined type 'link'
        C:\Users\xuqianqian\Desktop\yuesefu.cpp(21) : see declaration of 'link'
C:\Users\xuqianqian\Desktop\yuesefu.cpp(21) : error C2440: '=' : cannot convert from 'struct creat::link *' to 'struct node *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Users\xuqianqian\Desktop\yuesefu.cpp(29) : error C2440: 'return' : cannot convert from 'struct node *' to 'int'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
C:\Users\xuqianqian\Desktop\yuesefu.cpp(30) : error C2617: 'creat' : inconsistent return statement
        C:\Users\xuqianqian\Desktop\yuesefu.cpp(9) : see declaration of 'creat'
C:\Users\xuqianqian\Desktop\yuesefu.cpp(56) : error C2676: binary '*' : 'struct node' does not define this operator or a conversion to a type acceptable to the predefined operator
执行 cl.exe 时出错.

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

到底哪儿错了~~~
[解决办法]

struct node   //运用结构体定义链表
{
    int number;         //序号
int password;       //密码
    struct node *next; //指向本结构体的结点指针
}link,*p,*q,*head;     //node类变量link,结点p、q,头指针head


link *creat()  //创建循环链表,???变量名link, 不能当作类型名。



你把类型定义和变量定义混用了。
1)
typedef struct node   //运用结构体定义链表
{
    int number;         //序号
int password;       //密码
    struct node *next; //指向本结构体的结点指针
}*link,*pnode,node; 

link p,q,head;  
link creat()  //创建循环链表
.......

2)
struct node   //运用结构体定义链表
{
    int number;         //序号
int password;       //密码
    struct node *next; //指向本结构体的结点指针
}link,*p,*q,*head;     //node类变量link,结点p、q,头指针head
struct node *creat()  //创建循环链表
。。。
你可以任选
1) 2) 二者之一,不能二者混用。

热点排行