十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
目录
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都企业网站建设,高端网页制作,对成都边坡防护网等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业seo优化优化,H5建站,响应式网站。功能目录
主函数
.h文件
功能实现
1.创建hash表
2.定义hash函数
3.添加功能
4.根据ID查找
5.显示所有人员
6.根据ID删除人员
7.根据ID修改人员信息
8.销毁hash表
9.从txt文件加载人员信息
10.将人员信息保存到txt文件中
功能演示
1.添加
2.显示
3.根据ID查找
4.根据ID删除
5.根据ID修改人员信息
功能目录 主函数#include "../include/info.h"
int main()
{
int op = 0;
data_type item;
Hash *pHash = createHash();
load(pHash,"info.txt");
while (1)
{
menu();
int res=0;
scanf("%d", &op);
if (op == 0)
break;
switch (op)
{
case 1:
printf("请输入人员ID:\n");
scanf("%d", &item.id);
printf("请输入人员姓名:\n");
scanf("%s", item.name);
printf("请输入人员年龄:\n");
scanf("%d", &item.age);
printf("请输入人员职位:\n");
scanf("%s", item.status);
res=add(pHash, item);
if(res==OK)
{
printf("-----------------------------------------\n");
printf("添加成功!\n");
printf("-----------------------------------------\n");
}
break;
case 2:
show(pHash);
break;
case 3:
printf("请输入要查找人员的ID:\n");
scanf("%d",&item.id);
res=find(pHash,item);
if(res!=OK)
{
printf("-----------------------------------------\n");
printf("没有ID为%d的人员!\n",item.id);
printf("-----------------------------------------\n");
}
break;
case 4:
printf("请输入要删除人员的ID:\n");
scanf("%d",&item.id);
del(pHash,item);
break;
case 5:
printf("请输入要修改人员的ID:\n");
scanf("%d",&item.id);
res=change(pHash,item);
if(res==OK)
{
printf("-----------------------------------------\n");
printf("ID为%d的人员修改信息成功!\n",item.id);
printf("-----------------------------------------\n");
}
break;
case 6:
res=Destroy(&pHash);
if(res==OK)
{
printf("-----------------------------------------\n");
printf("销毁成功!\n");
printf("-----------------------------------------\n");
}
break;
case 7:
res=save(pHash,"info.txt");
if(res==OK)
{
printf("-----------------------------------------\n");
printf("保存成功!\n");
printf("-----------------------------------------\n");
}
break;
}
}
}
.h文件#ifndef _INFO_H
#define _INFO_H
#include#include#include#include#include#include#include#define SIZE 30
typedef struct info
{
int id; // key
char name[SIZE];
int age;
char status[SIZE];
} data_type;
typedef struct node
{
data_type data;
struct node *next;
} Node;
typedef struct hash
{
Node *pArr[SIZE];
int count;
} Hash;
enum opt
{
OPENERROR = -7,
ERROR,
DOWNLOADERROR,
UPLOOADERROR,
EXIST,
NOEXIST,
MALLOCERROR,
OK
};
void menu();
Hash *createHash();
int HashFunc(int key);
int show(Hash *pHash);
int add(Hash *pHash, data_type pData);
int find(Hash *pHash, data_type pData);
int del(Hash *pHash, data_type pData);
int change(Hash *pHash,data_type pData);
int Destroy(Hash **pHash);
int namefind(Hash *pHash,data_type pData);
int save(Hash *pHash,char *filename);
int load(Hash *pHash,char *filename);
#endif
功能实现
1.创建hash表//创建一个hash表
Hash *createHash()
{
Hash *pHash = NULL;
pHash = (Hash *)malloc(sizeof(Hash));
if (NULL == pHash)
{
perror("malloc error");
return NULL;
}
memset(pHash, 0, sizeof(Hash));
return pHash;
}
2.定义hash函数//hash函数
int HashFunc(int key)
{
int pos;
pos=key%13;
return pos;
}
3.添加功能//添加
int add(Hash *pHash, data_type pData)
{
if (pHash == NULL)
{
perror("malloc error");
return NOEXIST;
}
int res=0;
res=find(pHash,pData);
//当前数据元素是否已经存在
if ( res==OK )
{
printf("-----------------------------------------\n");
printf("该ID对应人员已存在!\n");
printf("-----------------------------------------\n");
return EXIST;
}
//创建一个新结点
Node *pNew = NULL;
pNew = (Node *)malloc(sizeof(Node));
if (NULL == pNew)
{
perror("malloc error");
return MALLOCERROR;
}
//memset(pNew, 0, sizeof(Node));
pNew->data = pData;
int pos = HashFunc(pData.id);
//保护好后面的结点
pNew->next = pHash->pArr[pos];
pHash->pArr[pos] = pNew;
return OK;
}
4.根据ID查找int find(Hash *pHash, data_type pData)
{
if (NULL == pHash)
{
perror("malloc error");
return NOEXIST;
}
int pos = HashFunc(pData.id);
Node *pTmp = NULL;
pTmp = pHash->pArr[pos];
if (NULL == pTmp)
{
return NOEXIST;
}
while (pTmp != NULL)
{
if (pData.id == pTmp->data.id)
{
printf("ID为%d的人员信息如下:\n", pData.id);
printf("-----------------------------------------\n");
printf("ID:%d ", pTmp->data.id);
printf("姓名:%s ", pTmp->data.name);
printf("年龄:%d ", pTmp->data.age);
printf("职位:%s\n", pTmp->data.status);
printf("-----------------------------------------\n");
return OK;
}
}
}
5.显示所有人员int show(Hash *pHash)
{
if (NULL == pHash)
{
perror("malloc error");
return MALLOCERROR;
}
//遍历输出
int i = 0;
Node *pTmp = NULL;
for (i = 0; i< SIZE; i++)
{
//结构体指针指向链表首地址
pTmp = pHash->pArr[i];
while (pTmp != NULL)
{
printf("-----------------------------------------\n");
printf("ID:%d ", pTmp->data.id);
printf("姓名:%s ", pTmp->data.name);
printf("年龄:%d ", pTmp->data.age);
printf("职位:%s\n", pTmp->data.status);
printf("-----------------------------------------\n");
pTmp = pTmp->next;
}
}
return OK;
}
6.根据ID删除人员int del(Hash *pHash, data_type pData)
{
if (NULL == pHash)
{
perror("malloc error");
return MALLOCERROR;
}
int res = 0;
res = find(pHash, pData);
if (res != OK)
{
printf("-----------------------------------------\n");
printf("没有该ID为%d的人员!\n", pData.id);
printf("-----------------------------------------\n");
return ERROR;
}
if (pData.id< 0)
{
return ERROR;
}
int pos = HashFunc(pData.id);
Node *pTmp = NULL;
pTmp = pHash->pArr[pos];
Node *pPre = pTmp;
Node *pDel = pTmp->next;
if (pData.id = pPre->data.id)
{
pDel = pPre;
pHash->pArr[pos] = pPre->next;
printf("-----------------------------------------\n");
printf("删除成功!\n");
printf("-----------------------------------------\n");
free(pDel);
pHash->count--;
return OK;
}
else
{
while (NULL != pDel)
if (pData.id == pDel->data.id)
{
pPre->next = pDel->next;
printf("-----------------------------------------\n");
printf("删除成功!删除人员信息如下:\n");
printf("ID:%d ", pTmp->data.id);
printf("姓名:%s ", pTmp->data.name);
printf("年龄:%d ", pTmp->data.age);
printf("职位:%s\n", pTmp->data.status);
printf("-----------------------------------------\n");
free(pDel);
return OK;
}
else
{
pPre = pDel;
pDel = pDel->next;
}
}
if (NULL == pDel)
{
printf("-----------------------------------------\n");
printf("此ID对应人员信息不存在\n");
printf("-----------------------------------------\n");
}
return OK;
}
7.根据ID修改人员信息int change(Hash *pHash, data_type pData)
{
if (NULL == pHash)
{
perror("malloc error");
return MALLOCERROR;
}
if (pData.id< 0)
{
return ERROR;
}
int res = 0;
res = find(pHash, pData);
if (res != OK)
{
printf("-----------------------------------------\n");
printf("没有该ID为%d的人员!\n", pData.id);
printf("-----------------------------------------\n");
return ERROR;
}
int pos = HashFunc(pData.id);
Node *pTmp = NULL;
pTmp = pHash->pArr[pos];
while (pTmp->data.id != pData.id)
{
pTmp = pTmp->next;
}
printf("请输入ID为%d的人员修改后的名字:\n", pData.id);
scanf("%s", pTmp->data.name);
printf("请输入ID为%d的人员修改后的年龄:\n", pData.id);
scanf("%d", &pTmp->data.age);
printf("请输入ID为%d的人员修改后的职位:\n", pData.id);
scanf("%s", pTmp->data.status);
return OK;
}
8.销毁hash表int Destroy(Hash **pHash)
{
if (NULL == pHash)
{
return ERROR;
printf("-----------------------------------------\n");
printf("销毁成功!\n");
printf("-----------------------------------------\n");
}
int i;
Node *pDel = NULL;
Node *pTmp = NULL;
for (i = 0; i< SIZE; i++)
{
pTmp = (*pHash)->pArr[i];
while (pTmp != NULL)
{
//头删法
pDel = pTmp;
pTmp = pTmp->next;
free(pDel);
pDel = NULL;
}
}
free(*pHash);
*pHash = NULL;
return OK;
}
9.从txt文件加载人员信息int load(Hash *pHash, char *filename)
{
data_type infodata;
if (pHash == NULL)
{
perror("malloc error");
return ERROR;
}
int fp = open(filename, O_RDONLY);
if (fp< 0)
{
perror("open error");
return ERROR;
}
while (1)
{
int res = read(fp, &infodata, sizeof(data_type));
if (0 == res)
{
printf("信息已导入!\n");
break;
}
else if (res< 0)
{
printf("导入失败!\n");
return ERROR;
}
else
{
add(pHash, infodata);
pHash->count++;
}
}
close(fp);
return OK;
}
10.将人员信息保存到txt文件中int save(Hash *pHash, char *filename)
{
int fw = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fw< 0)
{
perror("open error");
return ERROR;
}
int i;
for (i = 0; i< SIZE; i++)
{
Node *pTmp = pHash->pArr[i];
while (pTmp != NULL)
{
// memset(&pTmp->data,0,sizeof(data_type));
//写入
int res = write(fw, &pTmp->data, sizeof(data_type));
if (res< 0)
{
printf("save error");
}
else if (0 == res)
{
printf("save error");
break;
}
pTmp = pTmp->next;
}
}
close(fw);
return OK;
}
功能演示
1.添加2.显示3.根据ID查找4.根据ID删除5.根据ID修改人员信息你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧