编写一个程序实现如下功能:建立一个单链表,每个结点数据要有职工号、工资。用一个creat函数来建立链表,
#include<stdio.h>#include<alloc.h>structWorker{intnum;floatpay;structWorker*next;};in...
# include <stdio.h>
# include <alloc.h>
struct Worker
{ int num;
float pay;
struct Worker *next;
};
int n;
main()
{ struct Worker *head,*create(void);
void list(struct Worker*);
head=create();
list(head);
}
struct Worker *create(void)
{ struct Worker *head, *p1, *p2;
printf(“Create a linked list:\n”);
head=0;
n=0;
p1=p2=(struct Worker*)malloc(sizeof(struct Worker));
scanf(“%d%f”,&p1->num,&p1->pay);
while(p1->num!=0)
{ n++;
if(n==1)head=p1;
else p2->next=p1 ;
p2=p1;
p1=(struct Worker*)malloc(sizeof(struct Worker)) ;
scanf(“%d%f”,&p1->num,&p1->pay);
}
p2->next=0;
free(p1);
return head;
}
void list(struct Worker *p)
{ printf(“The linked list:\n”);
while( p!=0 )
{ printf(“\n%\t%f”,p->num,p->pay);
p=p->next;
}
}
以上是我找到的一段编译 但是运行之后会有错误提示# include <alloc.h>
是有问题的 另外数据自定 是不是指 要用TXT做个文件? 展开
# include <alloc.h>
struct Worker
{ int num;
float pay;
struct Worker *next;
};
int n;
main()
{ struct Worker *head,*create(void);
void list(struct Worker*);
head=create();
list(head);
}
struct Worker *create(void)
{ struct Worker *head, *p1, *p2;
printf(“Create a linked list:\n”);
head=0;
n=0;
p1=p2=(struct Worker*)malloc(sizeof(struct Worker));
scanf(“%d%f”,&p1->num,&p1->pay);
while(p1->num!=0)
{ n++;
if(n==1)head=p1;
else p2->next=p1 ;
p2=p1;
p1=(struct Worker*)malloc(sizeof(struct Worker)) ;
scanf(“%d%f”,&p1->num,&p1->pay);
}
p2->next=0;
free(p1);
return head;
}
void list(struct Worker *p)
{ printf(“The linked list:\n”);
while( p!=0 )
{ printf(“\n%\t%f”,p->num,p->pay);
p=p->next;
}
}
以上是我找到的一段编译 但是运行之后会有错误提示# include <alloc.h>
是有问题的 另外数据自定 是不是指 要用TXT做个文件? 展开
1个回答
展开全部
关于# include <alloc.h>问题你看看这个http://blog.sina.com.cn/s/blog_4066022e010008jh.html
我将你代码改了点就没的错误了,代码如下:
# include <stdio.h>
#include <malloc.h>
struct Worker
{
int num;
float pay;
struct Worker *next;
};
int n;
int main()
{
struct Worker *head,*create(void);
void list(struct Worker*);
head=create();
list(head);
return 0;
}
struct Worker *create(void)
{
struct Worker *head, *p1, *p2;
printf("create a linked list:\n");
head=0;
n=0;
p1=p2=(struct Worker*)malloc(sizeof(struct Worker));
scanf("%d%f",&p1->num,&p1->pay);
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1 ;
p2=p1;
p1=(struct Worker*)malloc(sizeof(struct Worker)) ;
scanf("%d%f",&p1->num,&p1->pay);
}
p2->next=0;
free(p1);
return head;
}
void list(struct Worker *p)
{
printf("The linked list:\n");
while( p!=0 )
{
printf("\n%\t%f",p->num,p->pay);
p=p->next;
}
}
我将你代码改了点就没的错误了,代码如下:
# include <stdio.h>
#include <malloc.h>
struct Worker
{
int num;
float pay;
struct Worker *next;
};
int n;
int main()
{
struct Worker *head,*create(void);
void list(struct Worker*);
head=create();
list(head);
return 0;
}
struct Worker *create(void)
{
struct Worker *head, *p1, *p2;
printf("create a linked list:\n");
head=0;
n=0;
p1=p2=(struct Worker*)malloc(sizeof(struct Worker));
scanf("%d%f",&p1->num,&p1->pay);
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else p2->next=p1 ;
p2=p1;
p1=(struct Worker*)malloc(sizeof(struct Worker)) ;
scanf("%d%f",&p1->num,&p1->pay);
}
p2->next=0;
free(p1);
return head;
}
void list(struct Worker *p)
{
printf("The linked list:\n");
while( p!=0 )
{
printf("\n%\t%f",p->num,p->pay);
p=p->next;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询