/*
职工工资表
建立一个链表 每个节点包含的成员有:职工号和工资,用malloc函数开辟新结点,
要求链表包含5个结点,,从键盘输入结点的有效数据,然后把这些结点的数据打印
出来,用creat函数来建立结点,用list函数来输出数据,
这五个职工号码为1.2.3.4.5
*/
#include
#include
#define MAX 5 // 员工数量
//-- 类型声明 & 函数声明 --
typedef struct ls { // 链表结构
int Gong_Hao;
double Gong_Zi;
struct ls * next;
} ls;
ls * creat(int n); // 创建链表,同时creat函数调用key 函数进行数据输入
void key(ls * p); // 数据输入函数
void list(ls * p); // 显示链表数据
//-- Main 函数 --
int main(void)
{
ls * pl = creat(MAX);
list(pl);
getchar();
return 0;
}
//-- 函数定义 --
ls * creat(int n)
{
ls * pl = NULL;
if(n-- > 0)
{
pl = (ls *)malloc(sizeof(ls));
pl->Gong_Hao = MAX - n;
key(pl);
pl->next = creat(n);
}
return pl;
}
void key(ls * p)
{
printf("请输入员工 %d 的工资: ", p->Gong_Hao);
while(scanf("%lf", &p->Gong_Zi) != 1)
{
while(getchar() != '\n');
printf("[!]请正确输入员工 %d 的工资: ", p->Gong_Hao);
}
while(getchar() != '\n');
}
void list(ls * p)
{
printf("\n\n----------------------------------\n");
while(p)
{
printf("员工%3d 工资 %10.2lf\n", p->Gong_Hao, p->Gong_Zi);
p = p->next;
}
printf("----------------------------------\n\n");
}