最近在学c的链表,但还不是很懂,看了很多代码都不能完全理解。想要将以下代码改为用链表存入信息,请问怎么样改呢?
这是.cpp文件:
#include<stdio.h>
#include<string.h>
#include "workers.h"
#include "function.h"
#include "input.h"
extern int count;
extern struct workers wor[100];
FILE *fp;
int menu_select()
{
char s;
int cn;
printf("1. 录入职工基本信息\n");
printf("2. 查询一个职工号的信息\n");
printf("3. 减少一个职工号\n");
printf("4. 增加一个职工号\n");
printf("5. 统计制定二级单位的职工人数、工资总额\n");
printf("6. 对职工的工资进行排序\n");
printf("7. 退出系统\n");
printf("Please input 1-7:");
do
{
s = getchar();
cn = (int)s - 48;
} while (cn < 1 || cn>7);
return cn;
}
void input()
{
char s;
do
{
printf("请输入职工号\n");
scanf("%s", wor[count].num);
printf("请输入职工的姓名\n");
scanf("%s", wor[count].name);
printf("请输入职工的性别\n");
scanf("%s",wor[count].sex);
printf("请输入职工所属二级单位\n");
scanf("%s",wor[count].unit);
printf("请输入职工所学专业\n");
scanf("%s",wor[count].major);
printf("请输入职工的职位名称\n");
scanf("%s",wor[count].manage);
printf("请输入该职工的月工资\n");
scanf("%d",&wor[count].salary);
printf("请输入该职工的电话号码\n");
scanf("%s",(wor[count].tel));
count++;
printf("是否继续输入? Y/N\n");
scanf("%c",&s);
if(s=='Y'||s=='y') continue;
else
{
printf("输入结束!");
fp =fopen("workers","w");
for(int i=0;i<count;i++)
{
fwrite(&wor[i],sizeof(struct workers),1,fp);
}
fclose(fp);
printf("已存入文件workers。\n");
break;
}
} while (true);
}
这里是部分头文件:
input.h
#ifndef USRE_INPUT_H
#define USER_INPUT_H
void input();
int menu_select();
#endif
workers.h
#ifndef WORKERS_H
#define WORKERS_H
struct workers
{
char num[20];
char name[20];
char sex[10];
char unit[20];
char major[20];
char manage[20];
int salary;
char tel[20];
};
#endif
struct workers wor[100];
可以改为: struct workers *wor_header;
struct workers
{
char num[20];
char name[20];
char sex[10];
char unit[20];
char major[20];
char manage[20];
int salary;
char tel[20];
struct workers *next; /* 指向下一个 */
};
拿去参考
#include<iostream>
#include<windows.h>
#include<cstdio>
#include<cstring>
#include<fstream>
#define MAX 100
#define len 10
using namespace std;
typedef struct staff
{
char name[MAX];
int id;
char sex[MAX];
char birth[MAX];
char work[MAX];
char edu[MAX];
char job[MAX];
char add[MAX];
char tel[MAX];
struct staff* next;
}stnode,*Tst;
typedef struct
{
Tst elem[MAX];
int count;
}HashTable;
Tst head=new stnode;
HashTable H;
char password[MAX]="1234abcd";
void add()
{
Tst p=new stnode;
Tst q=new stnode;
printf("请输入职工姓名: ");
scanf("%s",p->name);
printf("请输入职工工号: ");
scanf("%d",&p->id);
printf("请输入职工性别: ");
scanf("%s",p->sex);
printf("请输入职工出生年月: ");
scanf("%s",p->birth);
printf("请输入职工入职年月: ");
scanf("%s",p->work);
printf("请输入职工学历: ");
scanf("%s",p->edu);
printf("请输入职工职位: ");
scanf("%s",p->job);
printf("请输入职工地址: ");
scanf("%s",p->add);
printf("请输入职工电话: ");
scanf("%s",p->tel);
if(head->next==NULL)
head->next=p;
else
{
q=head->next;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
}
void seek()
{
Tst p=new stnode;
if(head->next==NULL)
{
printf("此时无职员信息,可以选择添加职员信息\n");
return;
}
else
{
p=p->next;
printf("请输入职工的姓名进行职工信息查找\n");
char x[20];
scanf("%s",x);
while(p)
{
if(strcmp(p->name,x)==0)
{
printf("职工的姓名:%s\n",p->name);
printf("职工的工号:%d\n",p->id);
printf("职工的性别:%s\n",p->sex);
printf("职工的出生年月:%s\n",p->birth);
printf("职工的入职年月:%s\n",p->work);
printf("职工的学历:%s\n",p->edu);
printf("职工的职位:%s\n",p->job);
printf("职工的地址:%s\n",p->add);
printf("职工的电话号码:%s\n",p->tel);
return;
}
else p=p->next;
}
printf("未找到职工信息\n");
}
}
void del()
{
int a;
Tst p=new stnode;
Tst q=new stnode;
p=head->next;
q=head;
if(head->next==NULL)
{
printf("没有可以删除的职员信息\n");
return ;
}
printf("想要删除的职员工号:");
scanf("%d",&a);
if(p->next==NULL&&p->id==a)
{
q->next=NULL;
delete p;
printf("删除成功!\n");
return;
}
else if(p->next==NULL&&p->id!=a)
{
printf("该职员不存在\n");
return;
}
else
{
while(p)
{
if(p->id==a)
{
q->next=p->next;
delete p;
printf("删除成功!\n");
return;
}
else
{
p=p->next;
q=q->next;
}
}
}
if(p==NULL)
{
printf("该职员信息不存在\n");
return;
}
}
void modify()
{
Tst p=new stnode;
p=head->next;
if(p==NULL)
{
printf("没有可以修改的信息!\n");
return;
}
else
{
printf("输入想要修改的职员工号\n");
int x;
int t;
scanf("%d",&x);
while(p)
{
if(p->id==x)
{
printf("输入想要修改的信息\n");
printf("0.退出 1.姓名 2.工号\n");
printf("3.性别 4.出生年月 5.入职年月\n");
printf("6.学历 7.职位 8.地址 9.电话\n");
while(1)
{
scanf("%d",&t);
if(t==0) return;
else if(t==1)
{
printf("请输入修改后的姓名:\n");
scanf("%s",p->name);
printf("修改成功\n");
}
else if(t==2)
{
printf("请输入修改后的工号:\n");
scanf("%d",&p->id);
printf("修改成功\n");
}
else if(t==3)
{
printf("请输入修改后的性别:\n");
scanf("%s",p->sex);
printf("修改成功\n");
}
else if(t==4)
{
printf("请输入修改后的出生年月:\n");
scanf("%s",p->birth);
printf("修改成功\n");
}
else if(t==5)
{
printf("请输入修改后的入职年月:\n");
scanf("%s",p->work);
printf("修改成功\n");
}
else if(t==6)
{
printf("请输入修改后的学历:\n");
scanf("%s",p->edu);
printf("修改成功\n");
}
else if(t==7)
{
printf("请输入修改后的职位:\n");
scanf("%s",p->job);
printf("修改成功\n");
}
else if(t==8)
{
printf("请输入修改后的地址:\n");
scanf("%s",p->add);
printf("修改成功\n");
}
else if(t==9)
{
printf("请输入修改后的电话:\n");
scanf("%s",p->tel);
printf("修改成功\n");
}
else return;
}
}
else
p=p->next;
}
if(p==NULL)
printf("未找到该职员信息!\n");
}
}
void sort()
{
Tst p=new stnode;
Tst q=new stnode;
p=head->next;
q=p->next;
if(p==NULL)
{
printf("无职工信息\n");
return;
}
else
{
while(p!=NULL)
{
while(q!=NULL)
{
if(strcmp(p->name,q->name)>0)
{
int id1;
char name1[MAX],sex1[MAX],birth1[MAX],work1[MAX];
char edu1[MAX],job1[MAX],add1[MAX],tel1[MAX];
strcpy(name1,p->name);
strcpy(p->name,q->name);
strcpy(q->name,name1);//name
id1=p->id;
p->id=q->id;
q->id=id1;
strcpy(sex1,p->sex);
strcpy(p->sex,q->sex);
strcpy(q->sex,sex1);//sex
strcpy(birth1,p->birth);
strcpy(p->birth,q->birth);
strcpy(q->birth,birth1);//birth
strcpy(work1,p->work);
strcpy(p->work,q->work);
strcpy(q->work,work1);//work
strcpy(edu1,p->edu);
strcpy(p->edu,q->edu);
strcpy(q->edu,edu1);//edu
strcpy(job1,p->job);
strcpy(p->job,q->job);
strcpy(q->job,job1);//job
strcpy(add1,p->add);
strcpy(p->add,q->add);
strcpy(q->add,add1);//add
strcpy(tel1,p->tel);
strcpy(p->tel,q->tel);
strcpy(q->tel,tel1);//tel
}
q=q->next;
}
p=p->next;
if(p!=NULL)
{
q=p->next;
}
else
{
return;
}
printf("排序成功\n");
return;
}
}
}
void power()
{
int count=0;
char x[MAX];
printf("欢迎访问职工管理系统\n");
while(count!=5)
{
printf("请输入密码继续!\n");
scanf("%s",x);
if(strcmp(password,x)==0)
{
printf("密码正确!\n");
Sleep(1500);
system("cls");
return;
}
else
{
printf("密码错误!请重新输入!\n");
count++;
}
}
printf("输入错误超过五次!正在退出...\n");
exit(0);
}
void print()
{
if(head->next==NULL)
{
printf("表格为空,不可打印!\n");
return ;
}
else
{
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n","姓名","工号","性别","出生年月","入职年月","学历","职位","地址","电话");
Tst p=new stnode;
p=head->next;
while(p)
{
printf("%-10s%-10d%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n",p->name,p->id,p->sex,p->birth,p->work,p->edu,p->job,p->add,p->tel);
p=p->next;
}
}
}
void to_file()
{
ofstream ofs;
ofs.open("file.txt",ios::out);
Tst p=new stnode;
p=head->next;
while(p!=NULL)
{
ofs<<p->name<<" "<<p->id<<" "<<p->sex<<" "<<p->birth<<" "<<p->work<<" "<<p->edu<<" "<<p->job<<" "<<p->add<<" "<<p->tel<<endl;
p=p->next;
}
printf("成功写入file.txt文件!\n");
ofs.close();
}
void from_file()
{
int i=1;
ifstream ifs;
ifs.open("file.txt",ios::in);
if(!ifs.is_open())
{
printf("文件打开失败!\n");
return;
}
char temp[MAX];
while(ifs>>temp)
{
Tst p=new stnode;
printf("正在导入第%d个数据\n",i);
strcpy(p->name,temp);
ifs>>p->id>>p->sex>>p->birth>>p->work>>p->edu>>p->job>>p->add>>p->tel;
if(head->next==NULL)
head->next=p;
else
{
Tst q=new stnode;
q=head->next;
while(q->next!=NULL)
q=q->next;
q->next=p;
}
i++;
}
printf("导入成功!\n");
}
void sort1()
{
Tst p=new stnode;
Tst q=new stnode;
p=head->next;
if(p==NULL)
{
printf("此时无职员信息\n");
return;
}
int l=0;
q=head->next;
while(q)
{
l++;
q=q->next;
}
Tst pt=new stnode;
Tst u=new stnode;
pt=head->next;
u=pt->next;
for (int i=0;i<l-1;i++)//排序采用冒泡排序,为保证指针部位空多次进行判断
{
for (int j=i;j<l;j++)
{
if((strcmp(pt->name,u->name)>0)&&(u->next))
{
char name1[MAX];
int id1;
char sex1[MAX];
char birth1[MAX];
char work1[MAX];
char edu1[MAX];
char job1[MAX];
char add1[MAX];
char tel1[MAX];
strcpy(name1,u->name);
strcpy(u->name,pt->name);
strcpy(pt->name,name1);//交换名字
id1=u->id;
u->id=pt->id;
pt->id=id1;
strcpy(u->sex,pt->sex);
strcpy(sex1,u->sex);
strcpy(pt->sex,sex1);//交换性别
strcpy(birth1,u->birth);
strcpy(u->birth,pt->birth);
strcpy(pt->birth,birth1);//交换出生日期
strcpy(work1,u->work);
strcpy(u->work,pt->work);
strcpy(pt->work,work1);//交换入职日期
strcpy(edu1,u->edu);
strcpy(u->edu,pt->edu);
strcpy(pt->edu,edu1);//交换学历
strcpy(job1,u->job);
strcpy(u->job,pt->job);
strcpy(pt->job,job1);//交换职位 */
strcpy(add1,u->add);
strcpy(u->add,pt->add);
strcpy(pt->add,add1);//交换地址 */
strcpy(tel1,u->tel);
strcpy(u->tel,pt->tel);
strcpy(pt->tel,tel1);//交换电话号码*/
//cout<<"1";
u=u->next;
}
else if((strcmp(pt->name,u->name)>0)&&(j==l-1))
{
int id1;
char name1[MAX];
char sex1[MAX];
char birth1[MAX];
char work1[MAX];
char edu1[MAX];
char job1[MAX];
char add1[MAX];
char tel1[MAX];
strcpy(name1,u->name);
strcpy(u->name,pt->name);
strcpy(pt->name,name1);//交换名字
id1=u->id;
u->id=pt->id;
pt->id=id1;
strcpy(sex1,u->sex);
strcpy(u->sex,pt->sex);
strcpy(pt->sex,sex1);//交换性别
strcpy(birth1,u->birth);
strcpy(u->birth,pt->birth);
strcpy(pt->birth,birth1);//交换出生日期
strcpy(work1,u->work);
strcpy(u->work,pt->work);
strcpy(pt->work,work1);//交换入职日期
strcpy(edu1,u->edu);
strcpy(u->edu,pt->edu);
strcpy(pt->edu,edu1);//交换学历 */
strcpy(job1,u->job);
strcpy(u->job,pt->job);
strcpy(pt->job,job1);//交换职位 */
strcpy(add1,u->add);
strcpy(u->add,pt->add);
strcpy(pt->add,add1);//交换地址 */
strcpy(tel1,u->tel);
strcpy(u->tel,pt->tel);
strcpy(pt->tel,tel1);//交换电话号码
}
else if(u->next)
u=u->next;
}
if(pt->next&&pt->next->next)
{
pt=pt->next;//继续遍历
u=pt->next;
}
}
printf("排序成功\n");
}
void Init()
{
for(int i=0;i<MAX;i++)
H.elem[i]=NULL;
H.count=0;
}
int Hash(int x)
{
return x%len;
}
void Insert()
{
Tst p=new stnode;
p=head->next;
while(p)
{
int addr=Hash(p->id);
while(H.elem[addr]!=NULL)
addr=(addr+1)%len;
H.elem[addr]=p;
H.count++;
p=p->next;
}
}
Tst Search(int k)
{
int addr=Hash(k);
while(H.elem[addr]->id!=k)
{
if(H.elem[addr]==NULL)
return NULL;
addr=(addr+1)%len;
}
return H.elem[addr];
}
void HashSearch()
{
Tst p=new stnode;
int temp;
Init();
Insert();
printf("请输入职工工号\n");
scanf("%d",&temp);
p=Search(temp);
if(p==NULL)
{
printf("查找的职工不存在!\n");
return ;
}
printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n","姓名","工号","性别","出生年月","入职年月","学历","职位","地址","电话");
printf("%-10s%-10d%-10s%-10s%-10s%-10s%-10s%-10s%-10s\n",p->name,p->id,p->sex,p->birth,p->work,p->edu,p->job,p->add,p->tel);
}
void menu()
{
int x;
printf("**********职工管理系统***********\n");
printf("*********1.打印职员信息**********\n");
printf("*********2.增加职员信息**********\n");
printf("*********3.删除职员信息**********\n");
printf("*********4.查询职员信息**********\n");
printf("*********5.修改职员信息**********\n");
printf("*****6.排序职员信息(选择)******\n");
printf("*****7.排序职员信息(冒泡)******\n");
printf("***********8.写入文件************\n");
printf("***********9.导出文件************\n");
printf("****10.按工号查找(哈希查找)****\n");
printf("*************0.退出**************\n");
while(1)
{
printf("请输入选项:\n");
scanf("%d",&x);
if(x==0) exit(0);
else if(x==1) print();
else if(x==2) add();
else if(x==3) del();
else if(x==4) seek();
else if(x==5) modify();
else if(x==6) sort();
else if(x==7) sort1();
else if(x==8) to_file();
else if(x==9) from_file();
else if(x==10) HashSearch();
else exit(0);
}
}
int main()
{
power();
printf("正在导入职工信息……\n");
from_file();
Sleep(1500);
system("cls");
menu();
}