假设有100名员工,他们分别在10家公司中,现在要模拟他们的入职(插入)、信息查询(查找)、离职(删除)等管理操作行为,用数组来代替链表实现,具体代码如下:
#include "stdafx.h"
#include <stdlib.h>
#define EMPLOYEE_NUM 100
#define COMPANY_NUM 10
typedef struct Employee
{
int id; //身份证ID
char name[20];
}Employee;
typedef struct Company
{
Employee employee;
int next;
}Company;
int head[COMPANY_NUM] = {0}; //公司的头指针
int use = 1; //当前使用标记
Company g_company[EMPLOYEE_NUM+1]; //公司链表体
int g_employee_index[EMPLOYEE_NUM]; //对应所在的公司
void insert(int companyIndex, Employee employee)
{
g_company[use].employee = employee;
g_company[use].next = head[companyIndex];
head[companyIndex] = use++;
}
void del(int companyIndex, int id)
{
int pre = 0;
for (int i = head[companyIndex]; i != 0; i = g_company[i].next)
{
if (g_company[i].employee.id == id)
{