链表,错哪了?

latofi 2009-01-12 12:58:07

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

#define LEN sizeof(struct worker);
struct worker *creat(void);
struct worker *del(struct worker*,long);
void print(struct worker*);

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

int main()
{
struct worker *head;
long del_num;
scanf("%ld",&del_num);
head=creat();
head=del(head,del_num);
print(head);

return 0;
}

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

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

return head;
}

struct worker *del(struct worker *head,long num)
{
struct worker *p1,*p2;
if(head == NULL)
{
printf("the list is null\n");
goto end;
}

p1=head;
while(p1->num != num&&p1->next != NULL)
{
p2=p1;
p1=p1->next;
}

if(p1->num == num)
if(p1==head) head=p1->next;
else p2->next=p1->next;
else
printf("%ld has not been found\n",num);
end:;
}

void print(struct student *head)
{
struct worker *p1;
if(head != NULL)
{
p1=head;
while(p1!=NULL)
{
printf("%ld %s %d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}
}
else
printf("the list is null\n");
}



--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.c
D:\1.c(33) : error C2143: syntax error : missing ')' before ';'
D:\1.c(33) : error C2059: syntax error : ')'
D:\1.c(46) : error C2143: syntax error : missing ')' before ';'
D:\1.c(46) : error C2059: syntax error : ')'
D:\1.c(82) : warning C4028: formal parameter 1 different from declaration
D:\1.c(86) : warning C4133: '=' : incompatible types - from 'struct student *' to 'struct worker *'
Error executing cl.exe.

1.obj - 4 error(s), 2 warning(s)
...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
latofi 2009-01-12
  • 打赏
  • 举报
回复
哥们太感谢了..第一个错误这是我第二次犯了..我记得上次也是你帮我纠正的..

分全给你了.
xiaoyisnail 2009-01-12
  • 打赏
  • 举报
回复
两个地方错了,见注释

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

#define LEN sizeof(struct worker)//分号去掉
struct worker *creat(void);
struct worker *del(struct worker*,long);
void print(struct worker*);

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

int main()
{
struct worker *head;
long del_num;
scanf("%ld",&del_num);
head=creat();
head=del(head,del_num);
print(head);

return 0;
}

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

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

return head;
}

struct worker *del(struct worker *head,long num)
{
struct worker *p1,*p2;
if(head == NULL)
{
printf("the list is null\n");
goto end;
}

p1=head;
while(p1->num != num&&p1->next != NULL)
{
p2=p1;
p1=p1->next;
}

if(p1->num == num)
if(p1==head) head=p1->next;
else p2->next=p1->next;
else
printf("%ld has not been found\n",num);
end:;
}

void print(struct worker *head)//这里参数错了
{
struct worker *p1;
if(head != NULL)
{
p1=head;
while(p1!=NULL)
{
printf("%ld %s %d\n",p1->num,p1->name,p1->salary);
p1=p1->next;
}
}
else
printf("the list is null\n");
}

69,335

社区成员

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

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