错哪了.链表

latofi 2009-01-12 10:08:31

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define LEN sizeof(struct worker)

struct worker
{
int num;
char name[20];
int salary;
struct worker *next;
};

int main()
{
int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;

if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;

while(p1!=NULL)
{
printf("%d,%s,%d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}

printf("Please input new worker's information\n");
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);

n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
}



--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.c
D:\1.c(46) : error C2143: syntax error : missing ';' before 'type'
D:\1.c(47) : error C2065: 'newer' : undeclared identifier
D:\1.c(47) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct worker *'
D:\1.c(48) : error C2223: left of '->num' must point to struct/union
D:\1.c(49) : error C2223: left of '->name' must point to struct/union
D:\1.c(50) : error C2223: left of '->salary' must point to struct/union
D:\1.c(56) : warning C4047: '=' : 'struct worker *' differs in levels of indirection from 'int '
D:\1.c(57) : error C2223: left of '->next' must point to struct/union
D:\1.c(61) : error C2223: left of '->num' must point to struct/union
D:\1.c(66) : error C2223: left of '->num' must point to struct/union
D:\1.c(68) : warning C4047: '=' : 'struct worker *' differs in levels of indirection from 'int '
D:\1.c(69) : warning C4047: '=' : 'struct worker *' differs in levels of indirection from 'int '
D:\1.c(70) : error C2223: left of '->next' must point to struct/union
D:\1.c(74) : warning C4047: '=' : 'struct worker *' differs in levels of indirection from 'int '
D:\1.c(75) : error C2223: left of '->next' must point to struct/union
Error executing cl.exe.

1.obj - 10 error(s), 5 warning(s)
...全文
92 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baihacker 2009-01-12
  • 打赏
  • 举报
回复
d:\>gcc c.c

d:\>a
1 1 1
2 2 2
0 0 0
1,1,1
2,2,2
Please input new worker's information
1 1 1
xiaoyuer5222 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 latofi 的回复:]
1楼:你的是对的,你能告诉下原因吗?
2楼:对啊,又是我啊.
3楼:这是所有的代码了.
4楼: ^_^

不知道怎么回事,我把文件名改成1.cpp 就能编译通过,但是改回1.c 就提示上面的错误了.
[/Quote]

C中的声明只能在语句块的开头,不能像C++中在哪用在哪声明
xiaoyuer5222 2009-01-12
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


struct worker
{
int num;
char name[20];
int salary;
struct worker *next;
};
#define LEN sizeof(struct worker)
int main()
{
int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;

if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;
/*-----------------------------------------------------------------*/
struct worker*p; /* 这里该一下 */
p=head;
while(p!=NULL)
{
printf("%d,%s,%d\n",p->num,p->name,p->salary);
p=p->next;
}
/*-----------------------------------------------------------------*/
printf("Please input new worker's information\n");
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);

n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
/* 这里加一条输出的测试 */
p=head;
while(p!=NULL)
{
printf("%d,%s,%d\n",p->num,p->name,p->salary);
p=p->next;
}
return 0;
}
结果:
1 huang 1000
3 yu 2000
0
1,huang,1000
3,yu,2000
Please input new worker's information
2 xiao 3000
1,huang,1000
2,xiao,3000
3,yu,2000
Press any key to continue
latofi 2009-01-12
  • 打赏
  • 举报
回复
1楼:你的是对的,你能告诉下原因吗?
2楼:对啊,又是我啊.
3楼:这是所有的代码了.
4楼: ^_^

不知道怎么回事,我把文件名改成1.cpp 就能编译通过,但是改回1.c 就提示上面的错误了.
zhangxichao 2009-01-12
  • 打赏
  • 举报
回复
C语言中声明变量应该在函数的开头,struct worker *newer;应该放到函数的开头
nullah 2009-01-12
  • 打赏
  • 举报
回复
VS2005下试了也没错~~
baihacker 2009-01-12
  • 打赏
  • 举报
回复
    int n=0;
struct worker *p1,*p2,*head;
p1=p2=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
scanf("%s",p1->name);
scanf("%d",&p1->salary);
head=NULL;

if(p1->num!=0)
while(1)
{
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=(struct worker*)malloc(LEN);
scanf("%d",&p1->num);
if(p1->num == 0) break;
scanf("%s",p1->name);
scanf("%d",&p1->salary);
}
p2->next=NULL;
p1 = head;/*notice*/
while(p1!=NULL)
{
printf("%d,%s,%d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}

printf("Please input new worker's information\n");
{
struct worker *newer;
newer=(struct worker*)malloc(LEN);
scanf("%d",&newer->num);
scanf("%s",newer->name);
scanf("%d",&newer->salary);

n++;
p1=head;
if(head==NULL)
{
head=newer;
newer->next=NULL;
}
else
{
while((newer->num > p1->num)&&(p1->next != NULL))
{
p2=p1;
p1=p1->next;
}
if(newer->num <= p1->num)
{
if(p1==head) head=newer;
else p2->next=newer;
newer->next=p1;
}
else
{
p1->next=newer;
newer->next=NULL;
}
}
}
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ckt1120 的回复:]
d:\1.c  ->  d:\1.cpp

-_-

[/Quote]

1.c也没错啊,gcc通过编译
fibbery 2009-01-12
  • 打赏
  • 举报
回复
代码不全吧?没看见哪有tpye,47行是哪一行?
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
又是楼主。。。
这次编译没错啊
ckt 2009-01-12
  • 打赏
  • 举报
回复
d:\1.c -> d:\1.cpp

-_-

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧