求大神帮忙编个C语言代码

2025-05-12 21:44:03
推荐回答(1个)
回答1:

花了点时间写出来了
用户输入一个整数控制职工的个数
创建:creat(职工数)

输出:list(表的指针 和职工数)

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
struct emp
{
char num[4];
int pay;
struct emp *next;
};
struct emp *creat(int i)
{
int j;
struct emp *p,*head,*q;
if((p=(struct emp*)(malloc(sizeof(struct emp))))==NULL)
return 0;
head=p;
for(j=0;j {
if((q=(struct emp*)(malloc(sizeof(struct emp))))==NULL)
return 0;
p->next=q;
p=p->next;

printf("Enter the %demp\'s num&pay:\n",j+1);
scanf("%3s%d",p->num,&p->pay);

}
p->next='\0';
return head;
}
void list(struct emp *head,int i)
{
int j=0;
struct emp *p=head;
p=head->next;
for(j=0;j {
printf("%d.the num is %s& the pay is %d\n",j+1,p->num,p->pay);
p=p->next;
}
}
int main()
{
int i;
struct emp *head;
printf("Please input the number of emp:\n");
scanf("%d",&i);
head=creat(i);
list(head,i);
return 0;

}