当前位置:网站首页>Comprehensive calculation of employee information
Comprehensive calculation of employee information
2022-04-23 03:12:00 【A happy wild pointer D】
1. Problem description
Each employee record contains the employee number (no)、 full name (name)、 Department number (depno) And wages (salary). Design a program to complete the following functions :
Employee record data structure :
typedef struct
{
int no; // Employee number
char name[10]; // full name
int depno; // Department number
float salary; // Wages
} EmpType;
EmpType employee[10]={
{1001," Zhang San ",01,2605},{...}};
Establish employee records based on the above data structure , A single linked list of leading nodes is established accordingly L.
Single linked table data structure :
typedef struct node
{
EmpType data; // Store employee information
struct node *next; // Pointer to the next node
} EmpList; // Employee single linked list node type
Through a single linked list L Realization :
(1) Enter an employee record
(2) Show all employee records
(3) By number no Sort all employee records incrementally
(4) By department number depno Sort all employee records incrementally
(5) By salary salary Sort all employee records incrementally
(6) Delete the employee record with the specified employee number
(7) Delete the entire single linked list
2. Outline design
This procedure has 8 A function :
(1)create(): Create a single chain table , Allocate memory for the head node , And its next Domain empty .
(2)init(EmpList* head): Initialize linked list , Using head insertion method , add to 10 Employees are recorded in the linked list .
(3)input(EmpList* head): Create a new node , Store the employee information entered by the user in the data field , Then insert the node behind the head node , Complete the addition of an employee record .
(4)show(EmpList* head): Traverse each data node of the linked list , Print out all employee records .
(5)sort(EmpList* head,int op): according to op The value of is used to sort the linked list incrementally according to different standards .op=3 Sort by employee number ,op=4 Sort by department number ,op=5 Sort by salary . If there is only a header node or only one data node , You don't have to sort ; Otherwise, the bubble method is used to sort .
(6)delete_by_no(EmpList* head): Receive the employee number entered by the user , Traverse the data nodes of the linked list , Find the node corresponding to the employee number , The of the previous node next The domain points to the next node , Then release the space of the node , Delete the specified employee number record .
(7)delete_all(EmpList* head): Traverse the data nodes of the linked list , One by one, release the space of nodes , Complete the deletion of all employee records .
(8)menu(): Print menu .
3. Program realization
#include <stdio.h>
#include <stdlib.h>
// Employee record structure
typedef struct
{
int no; // Employee number
char name[10]; // full name
int depno; // Department number
int salary; // Wages
} EmpType;
// Employee single linked list node type
typedef struct node
{
EmpType data; // Store employee information
struct node *next; // Pointer to the next node
} EmpList;
// Create a chain header
EmpList* create()
{
EmpList* head = (EmpList*)malloc(sizeof(EmpList));
if (head!=NULL)
{
head->next=NULL;
return head;
}
printf(" Out of memory , Failed to create linked list !\n\n");
return NULL;
}
// Initialize linked list
void init(EmpList* head)
{
int i;
EmpList* p = head;
EmpType emps[10]=
{
{1009," waiter ",101,6270},
{1001," Li Si ",103,2605},
{1002," Laolu ",102,2500},
{1006," Zhang San ",102,7500},
{1004," Dried tangerine or orange peel ",103,6400},
{1005," Zhao Liu ",101,3200},
{1007," Xiao Wang ",101,1383},
{1008," Wang Wu ",103,2328},
{1003," Morpheme ",104,1800},
{1010," Cheng ",101,2328}
};
// The first interpolation
for (i=0; i<10; i++)
{
// Apply for memory for the node
EmpList* q = (EmpList*)malloc(sizeof(EmpList));
if (q==NULL)
{
printf(" Out of memory , Failed to initialize the linked list !\n\n");
return ;
}
// Insert the new node behind the head node
q->data = emps[i];
q->next = p->next;
p->next = q;
}
}
// Enter an employee record
void input(EmpList* head)
{
EmpType emp;
printf(" Please enter employee information ( Use space spacing ):");
scanf("%d %s %d %d",&emp.no,emp.name,&emp.depno,&emp.salary);
EmpList* p = (EmpList*)malloc(sizeof(EmpList));
if (p==NULL)
{
printf(" Out of memory , Failed to add record !\n\n");
return ;
}
p->data = emp;
p->next = head->next;
head->next = p;
printf(" Add success !\n\n");
}
// Print menu
void menu()
{
printf("-------------------- Employee information system --------------------\n");
printf(" 1: Add employee record \n");
printf(" 2: Show all employee records \n");
printf(" 3: Sort by job number increment \n");
printf(" 4: Sort by department number \n");
printf(" 5: Sort by salary increase \n");
printf(" 6: Delete the record of the specified job number \n");
printf(" 7: Delete all records \n");
printf(" 0: Exit the system \n");
printf("----------------------------------------------------\n\n");
}
// Show all records
void show(EmpList* head)
{
EmpType emp;
EmpList* p = head->next;
printf("\n");
printf(" Employee number \t full name \t\t department \t Wages \n");
while (p!=NULL)
{
emp = p->data;
p=p->next;
printf("%d\t%-12s\t%d\t%d\n",emp.no,emp.name,emp.depno,emp.salary);
}
printf("\n");
}
// Ascending sort
void sort(EmpList* head,int op)
{
EmpList* pre;// The previous node
EmpList* cur;// The current node
EmpList* nx;// The last node
EmpList* end=NULL;// Caudal node
// If the linked list has only one header node or only one data node , No sorting
if (head->next==NULL || head->next->next==NULL)
{
return ;
}
// Bubble sort
while(head->next!=end)
{
pre=head;
cur=head->next;
nx=cur->next;
while(nx!=end)
{
switch(op)
{
case 3:
// If the value of the current node is greater than the value of the next node , Then exchange them
if (cur->data.no>nx->data.no)
{
// Exchange the nodes they point to
cur->next=nx->next;
pre->next=nx;
nx->next=cur;
EmpList* temp=nx;
nx=cur;
cur=temp;
}
break;
case 4:
if (cur->data.depno>nx->data.depno)
{
cur->next=nx->next;
pre->next=nx;
nx->next=cur;
EmpList* temp=nx;
nx=cur;
cur=temp;
}
break;
case 5:
if (cur->data.salary>nx->data.salary)
{
cur->next=nx->next;
pre->next=nx;
nx->next=cur;
EmpList* temp=nx;
nx=cur;
cur=temp;
}
break;
}
pre=pre->next;
cur=cur->next;
nx=nx->next;
}
end=cur;
}
show(head);
}
// Delete the record of the specified job number
void delete_by_no(EmpList* head)
{
EmpList* p=head;
EmpList* q;
int no;
printf(" Please enter the employee number to delete the record :");
scanf("%d",&no);
while(p->next!=NULL)
{
// Let the of the previous node first next Point to the last node of the deleted node , Then release the space of the deleted node
if (p->next->data.no == no)
{
q=p->next;
p->next=q->next;
free(q);
printf(" Delete successful !\n\n");
return ;
}
p=p->next;
}
printf(" There is no employee information of this job number !\n\n");
}
// Delete all records
void delete_all(EmpList* head)
{
EmpList* p=head->next;
EmpList* q;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
head->next=NULL;
printf(" Delete all successfully !\n\n");
}
int main()
{
int op;
EmpList* link=create();
init(link);
menu();
do
{
printf(" Please select :");
scanf("%d",&op);
switch (op)
{
case 1:
input(link);
break;
case 2:
show(link);
break;
case 3:
case 4:
case 5:
sort(link,op);
break;
case 6:
delete_by_no(link);
break;
case 7:
delete_all(link);
break;
case 0:
exit(1);
default:
printf(" Input error , Please re-enter !\n\n");
break;
}
}
while(op!=-1);
return 0;
}
4. Implementation interface
版权声明
本文为[A happy wild pointer D]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220627538257.html
边栏推荐
- C language to achieve address book - (static version)
- [Mysql] LEFT函數 | RIGHT函數
- 2022 P cylinder filling training test questions and simulation test
- Source Generator实战
- 二进制文件版本控制工具选择难?看完这篇你会找到答案
- .NET7之MiniAPI(特别篇):.NET7 Preview3
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- 一套组合拳,打造一款 IDEA 护眼方案
- 类似Jira的十大项目管理软件
- Using positive and negative traversal to solve the problem of "the shortest distance of characters"
猜你喜欢
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
12.<tag-链表和常考点综合>-lt.234-回文链表
be based on. NETCORE development blog project starblog - (1) why do you need to write your own blog?
Passing object type parameters through openfeign
Array and collection types passed by openfeign parameters
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
Use of slice grammar sugar in C #
ASP.NET 6 中间件系列 - 执行顺序
ASP. Net and ASP NETCORE multi environment configuration comparison
[Mysql] LEFT函數 | RIGHT函數
随机推荐
What kind of experience is it to prepare for a month to participate in ACM?
2022A特种设备相关管理(电梯)上岗证题库及模拟考试
Two methods are used to solve the "maximum palindrome product" problem
Blazor University (11) component - replace attributes of subcomponents
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
C# 读写二进制文件
Top ten project management software similar to JIRA
准备一个月去参加ACM,是一种什么体验?
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
Find the number of leaf nodes of binary tree
Test experience data
Flink实时数仓项目—DWS层设计与实现
Openfeign details show
xutils3修改了我提报的一个bug,开心
一套关于 内存对齐 的C#面试题,做错的人很多!
2022a special equipment related management (elevator) work license question bank and simulation examination
Use of slice grammar sugar in C #
[Mysql] LEFT函數 | RIGHT函數
一文了解全面静态代码分析
Yes Redis using distributed cache in NE6 webapi