int menu(){
printf(\"请按提⽰输⼊完毕操作!\\n\"); printf(\"1.查询员⼯信息\\n\"); printf(\"2.统计员⼯数量\\n\"); printf(\"3.录⼊员⼯信息\\n\"); printf(\"4.删除员⼯信息\\n\"); printf(\"5.按id排序全部员⼯\\n\"); printf(\"6.打印全部员⼯信息\\n\");printf(\"7.退出系统\\n\"); return 0;}
如menu()函数所看到的,该系统⼀共同拥有7个功能
#include char name[50]; struct emp * next;// struct emp * prev;}; struct emp * initList(); struct emp * addListTailNode(struct emp * head);struct emp * deleteListNode(struct emp * head,int id);struct emp * searchEmp(struct emp * head,int id);int printList(struct emp * l);int printNode(struct emp * p); struct emp * sortList(struct emp * head);int getListLen(struct emp * head);int writeToDisk(struct emp * head);struct emp * readFromDisk();int menu(); int usage(struct emp * head); #include \"emp.h\"int main(){ struct emp * head; head=readFromDisk(); usage(head); return 0;} struct emp * initList(){ struct emp * head; head=(struct emp *)malloc(sizeof(struct emp)); head->next=NULL; return head;} struct emp * addListTailNode(struct emp * head){ int id; char name[50]; struct emp * p, * last , * check; last = head; while(last->next!=NULL){ last=last->next; } printf(\"依次输⼊:员⼯id号,姓名!\\n\"); scanf(\"%d%s\ check = head; while(check!=last){ //遍历 check=check->next; if(id==check->id){ printf(\"加⼊�失败!员⼯id号反复!\\n\"); return head; } } p=(struct emp *)malloc(sizeof(struct emp)); p->id=id; strcpy(p->name,name); // last->next=p; last=p; p->next=NULL; printf(\"%s员⼯信息已加⼊�!\\n\ return head; } struct emp * deleteListNode(struct emp * head,int id){ struct emp * p,* q; p = head->next; while(p!=NULL){ if(p->next->id==id){ break; } p=p->next; } if(head->next==NULL){ printf(\"书籍信息为空!删除失败!\\n\"); } else{ q = p->next; p->next = q->next; printf(\"%s书籍信息被删除!\\n\ free(q); } return head; } struct emp * searchEmp(struct emp * head,int id){//查询,返回节点信息 struct emp * p; p = head->next; while(p!=NULL){ if(p->id==id){ break; } p=p->next; } return p; } int printNode(struct emp * p){//打印节点信息 if(p!=NULL){ printf(\"员⼯id: %d 员⼯姓名:%s\\n\ } else{ printf(\"系统内⽆该员⼯信息!\\n\"); } return 0; } int printList(struct emp * head){ //打印整条链表 struct emp * p; p = head->next; while(p!=NULL){ printNode(p); p=p->next; } return 0; } struct emp * sortList(struct emp * head){//排序 struct emp * p,* q; int temp_id; char temp_name[50]; for(p=head->next;p!=NULL;p=p->next){ for(q=p->next;q!=NULL;q=q->next){ if(p->id>q->id){ temp_id = q->id; q->id = p->id; p->id = temp_id; // strcpy(temp_name,q->name); strcpy(q->name,p->name); strcpy(p->name,temp_name); } } } return head;} int getListLen(struct emp * head){ int len=0; struct emp * p; p=head->next; while(p!=NULL){ len++; p=p->next; } return len;} int writeToDisk(struct emp * head){ FILE * fp; struct emp * p; if((fp = fopen(\"D:\\\\emp.hhtx\ printf(\"写⼊失败……!\\n\"); return 0; } // p=head->next; while(p!=NULL){ fwrite(p,sizeof(struct emp),1,fp); printf(\"%d %s\\n\ p=p->next; } fclose(fp); return 0;} struct emp * readFromDisk(){ FILE * fp; struct emp * head,* last,* p,* temp; head = initList(); if((fp = fopen(\"D:\\\\emp.hhtx\ printf(\"载⼊失败……未找到存档数据!\\n\\n\"); return head; } // last = head; p=(struct emp *)malloc(sizeof(struct emp)); while(p!=NULL){ p=(struct emp *)malloc(sizeof(struct emp)); fread(p,sizeof(struct emp),1,fp); printf(\"读取数据: %d %s\\n\ // last->next=p; last=p; p=p->next; } fclose(fp); printf(\"系统数据初始化完毕!\"); return head;} int menu(){ printf(\"请按提⽰输⼊完毕操作!\\n\"); printf(\"1.查询员⼯信息\\n\"); printf(\"2.统计员⼯数量\\n\"); printf(\"3.录⼊员⼯信息\\n\"); printf(\"4.删除员⼯信息\\n\"); printf(\"5.按id排序全部员⼯\\n\"); printf(\"6.打印全部员⼯信息\\n\"); printf(\"7.退出系统\\n\"); return 0;} int usage(struct emp * head){ int x,id; struct emp * p; menu(); while(1){ printf(\"请输⼊序列号:\"); scanf(\"%d\ switch(x){ case 1: printf(\"输⼊所要查询的员⼯的id号:\"); scanf(\"%d\ p = searchEmp(head,id); printNode(p); printf(\"---------------------------------\\n\"); break; case 2: printf(\"系统中⼀共存在%d个员⼯\\n\ break; case 3: head=addListTailNode(head); printf(\"---------------------------------\\n\"); break; case 4: printf(\"输⼊所要删除的员⼯的id号:\"); scanf(\"%d\ head=deleteListNode(head,id); printf(\"---------------------------------\\n\"); break; case 5: printf(\"排序開始……\\n\"); head=sortList(head); printf(\"排序已完毕!\\n\"); printf(\"---------------------------------\\n\"); break; case 6: printList(head); printf(\"---------------------------------\\n\"); break; case 7: writeToDisk(head); printf(\"保存完毕……\\n\"); printf(\"已退出系统!\\n\"); printf(\"---------------------------------\\n\"); return 0; default: return 0; } } return 0;} 因篇幅问题不能全部显示,请点此查看更多更全内容