#include#include#include#include#include#defineLEN sizeof(struct employee)typedefstructemployee
{intnum;floatsalary;structemployee*next;
}EMP;intn;/*插入表尾*/EMP*CreateEnd()
{
EMP*p,*last,*head;
n=0;
head=last=NULL;
p=(EMP*)malloc(LEN);/*创建第一个结点*/printf("员工号\t工资\n");
scanf("%d%f",&p->num,&p->salary);
p->next=NULL;while(p->num!=0)/*将结点加入链表*/{++n;if(n==1)/*是第一个结点,作表头*/head=p;else/*不是第一个结点,作表尾*/last->next=p;
last=p;
p=(EMP*)malloc(LEN);/*开辟下一个结点*/scanf("%d%f",&p->num,&p->salary);
p->next=NULL;
}
free(p);/*释放最后一个结点所占的内存*/returnhead;/*返回链表的头指针*/}/*插入表头*/EMP*CreateStart()
{
EMP*head,*p;
n=0;
head=NULL;
p=(EMP*)malloc(LEN);
printf("员工号\t工资\n");
scanf("%d%f",&p->num,&p->salary);
p->next=head;while(p->num!=0)
{++n;
head=p;
p=(EMP*)malloc(LEN);
scanf("%d%f",&p->num,&p->salary);
p->next=head;
}
free(p);returnhead;
}/*链表显示*/voidlist(EMP*head)
{
EMP*p;
printf("\n%d个职工信息如下:\n",n);
printf("员工号\t工资\n");
p=head;if(head!=NULL)
{do{
printf("%d\t%.2f\n",p->num,p->salary);
p=p->next;
}while(p!=NULL);
}elseprintf("无员工信息记录!\n");
}
EMP*insert_sort(EMP*head)//按照员工号从小到大顺序插入{
EMP*p0,*p1,*p2;
p1=p2=head;/*p1指向第一个结点*/printf("请输入要插入员工的信息:\n");
p0=(EMP*)malloc(LEN);
scanf("%d%f",&p0->num,&p0->salary);
p0->next=NULL;if(head==NULL)/*原来是空表*/{
head=p0;p0->next=NULL;/*使p0指向的结点作为链表第一个结点*/}else{while((p0->num>p1->num)&&(p1->next!=NULL))//防止空节点进行员工号比较,因此条件为p1->next!=NULL,而不是p1!=NULL{
p2=p1;p1=p1->next;/*找插入点*/}if(head==p1)/*如果只有一个头结点*/{if(p0->num>head->num)//如果待插入节点员工号大于头结点员工号head->next=p0;//待插入节点插入到头结点之后else//如果待插入节点员工号小于头结点员工号{
p0->next=head;head=p0;//待插入节点插入到头结点之前}
}else{if(p0->num>p1->num)//如果待插入节点员工号大于最后节点的员工号{
p1->next=p0;//待插入节点插入到最后}else//找到插入位置{
p2->next=p0;p0->next=p1;//插入到p2,p1之间}
}
}++n;returnhead;
}
EMP*insert_index(EMP*head)//按指定位置插入员工信息{inti=0,index;
EMP*p0,*p1,*p2;
p1=p2=head;
printf("请输入要插入员工的信息:\n");
p0=(EMP*)malloc(LEN);
scanf("%d%f",&p0->num,&p0->salary);
p0->next=NULL;
printf("请输入要插入的位置:");
scanf("%d",&index);if(head==NULL)//如果原来链表是空的,头结点指向新插入的结点{
head=p0;p0->next=NULL;
}else{while(p1!=NULL&&i
p2=p1;p1=p1->next;//找插入点i++;
}if(p1==head)//如果插入点是第一位{
p0->next=head;head=p0;
}else//插入到指定的位置{
p2->next=p0;p0->next=p1;
}
}++n;returnhead;
}
EMP*del(EMP*head)
{
EMP*p1,*p2;intnum;
printf("请输入要删除的员工编号:\n");
scanf("%d",&num);if(head==NULL)
{
printf("\n链表为空!\n");/*链表为空*/gotoend;
}
p1=p2=head;/*从头结点开始查找*/while(num!=p1->num&&p1->next!=NULL)/*p1指向的不是所要找的结点,并且没有到表尾*/{
p2=p1;p1=p1->next;/*后移一个结点*/}if(num==p1->num)/*找到需要删除的结点*/{if(p1==head)/*p1指向的是头结点*/head=p1->next;/*第二个结点成为新的头结点*/elsep2->next=p1->next;/*后继结点的地址赋给前一结点*/printf(员工号为%d的信息已经被删除\n",num);free(p1);
n--;
}elseprintf("没有发现 %d 号员工的信息!\n",num);
end:returnhead;
}intwriteInfo(EMP*head)
{
FILE*fp;
EMP*p;
p=head;if((fp=fopen("test.txt","w"))==NULL)
{
printf("打开文件失败!");
exit(0);
}while(p!=NULL)
{
fprintf(fp,"%d %.2f\n",p->num,p->salary);
p=p->next;
}
printf("链表保存成功!\n");
fclose(fp);
}
EMP*readInfo()
{
FILE*fp;
EMP*p,*last,*head;
n=0;
head=last=NULL;if((fp=fopen("test.txt","r"))==NULL)
{
printf("打开文件失败!");
exit(0);
}
p=(EMP*)malloc(LEN);while(fscanf(fp,"%d %f",&p->num,&p->salary)!=EOF)
{
p->next=NULL;++n;if(n==1)
head=p;elselast->next=p;
last=p;
p=(EMP*)malloc(LEN);
}
free(p);
printf("链表读入成功!\n");
fclose(fp);//关闭文件returnhead;/*返回链表的头指针*/}voidmain()
{
EMP*head,*emp;intdel_num,index;charletter;do{
printf("********************************\n");
printf("A 创建链表\n");
printf("B 插入员工信息(按员工号排序插入)\n");
printf("C 插入员工信息(按指定位置插入)\n");
printf("D 删除员工信息\n");
printf("E 读入链表内容\n");
printf("F 保存链表内容\n");
printf("G 显示链表内容\n");
printf("Q 退出\n");
printf("********************************\n");
printf("请根据编号选择功能:");
scanf("%c",&letter);//注意:%c前面要加空格,用来吸收多余的回车符,否则会重复打印菜单letter=toupper(letter);switch(letter)
{case'A':
system("cls");
head=CreateEnd();break;case'B':
system("cls");
head=insert_sort(head,emp);
list(head);break;case'C':
system("cls");
head=insert_index(head);
list(head);break;case'D':
system("cls");
head=del(head);
list(head);break;case'E':
system("cls");
head=readInfo();break;case'F':
system("cls");
writeInfo(head);break;case'G':
system("cls");
list(head);break;
}
}while(letter!='Q');
}