数据结构作业(第二题)
2015-01-09 16:16
543 查看
作业代码是从网上进行参考的,对代码进行了修改之后的到的。
![](http://img.blog.csdn.net/20150109161850432?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmlhX0Jpbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150109161947809?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmlhX0Jpbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
![](http://img.blog.csdn.net/20150109161955110?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvSmlhX0Jpbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
经过验证,程序能够正常运行。
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> void init_dlink(void); void read_file(void); void write_file(void); void insert_item(void); void search_item(void); void sort_item(void); void delete_item(void); void print_item(void); void modify_item(void); void anykey(void); typedef struct node *link; struct node { char stu_id[10]; char name[20]; char sex[10]; char _zip[50]; char age[50]; char address[100]; char phone[100]; char qq[50]; char wechat[100]; char birth[100]; link prev, next; }; link ptr, head, tail, current; bool fFlag; int main(void) { char option1, option2; system("cls"); init_dlink(); read_file(); while(1) { system("cls"); printf("**********************************\n"); printf(" 1.新建联系人\n"); printf(" 2.删除联系人\n"); printf(" 3.查找联系人\n"); printf(" 4.显示联系人\n"); printf(" 5.修改联系人\n"); printf(" 6.退出\n"); printf("**********************************\n"); printf(" Please enter your choice (1-5)..."); option1 = getche(); switch(option1) { case '1': fFlag = 1; insert_item(); break; case '2': delete_item(); break; case '3': search_item(); break; case '4': print_item(); break; case '5': modify_item(); break; case '6': if (fFlag) { printf("\n"); printf("Save changes? (Y or N)"); option2 = getche(); if(option2 == 'Y') { write_file(); exit(0); } if(option2 = 'N') exit(0); } else { write_file(); exit(0); } } } } void init_dlink(void) { ptr = (link)malloc(sizeof *ptr); strcpy(ptr->stu_id, "0"); strcpy(ptr->name, "0"); strcpy(ptr->sex, "0"); strcpy(ptr->_zip, "0"); strcpy(ptr->age, "0"); strcpy(ptr->address, "0"); strcpy(ptr->phone, "0"); strcpy(ptr->qq, "0"); strcpy(ptr->wechat, "0"); strcpy(ptr->birth, "0"); ptr-> prev = ptr; ptr->next = ptr; head = ptr; tail = ptr; } void read_file(void) { FILE *fptr; if((fptr = fopen("dlist.dat", "r")) == NULL) { printf(" Data file not exist!\n"); printf(" Press any key to edit first record...\n"); getch(); insert_item(); } else { ptr = (link)malloc(sizeof *ptr); while(fscanf(fptr, "%s %s %s %s %s %s %s %s %s %s %s ", ptr->stu_id, ptr->name, ptr->sex, ptr->_zip, ptr->age, ptr->address ,ptr->phone, ptr->qq, ptr->wechat, ptr->birth) != EOF) { if(strcmp(ptr->stu_id, " ") != 0) { sort_item(); ptr = (link)malloc(sizeof *ptr); } else free(ptr); } fclose(fptr); } } void write_file(void) { FILE *fptr; fptr = fopen("dlist.dat", "w"); current = head->next; while(current != head) { fprintf(fptr, "%s %s %s %s %s %s %s %s %s %s\n", current->stu_id, current->name, current->sex, current->_zip, current->age, current->address, current->phone, current->qq, current->wechat, current->birth); current = current->next; } fclose(fptr); } void insert_item(void) { system("cls"); ptr = (link)malloc(sizeof *ptr); printf(" Student ID:"); gets(ptr->stu_id); printf(" 姓名:"); gets(ptr->name); printf(" 性别:"); gets(ptr->sex); printf(" 邮编:"); gets(ptr->_zip); printf(" 年龄:"); gets(ptr->age); printf(" 地址:"); gets(ptr->address); printf(" 电话号码:"); gets(ptr->phone); printf(" 微信账号:"); gets(ptr->wechat); printf(" QQ:"); gets(ptr->qq); printf(" 生日:"); gets(ptr->birth); sort_item(); } void sort_item(void) { current = head->next; while(current != head) { if(strcmp(ptr->stu_id, current->stu_id) < 0) { ptr->next = current; ptr->prev = current->prev; current->prev->next = ptr; current->prev = ptr; break; } current = current->next; } if(head->next == head || current == head) { ptr->next = head; ptr->prev = head->prev; head->prev->next = ptr; head->prev = ptr; tail = ptr; } } void delete_item(void) { char del_id[10]; int count = 0; link clear; system("cls"); if(head->next == head) printf(" No student record!\n"); else { printf(" Delete student ID:"); gets(del_id); current = head->next; while(current->next != head) { if(strcmp(del_id, current->stu_id)==0) { count++; clear = current; current->prev->next = current->next; current->next->prev = current->prev; current = current->next; free(clear); } current = current->next; } if(strcmp(del_id, current->stu_id) == 0) { count++; clear = current; current->prev->next = head; head->prev = current->prev; tail = current->prev; free(clear); } if(count > 0) printf(" %s student record(s) deleted\n", del_id); else printf(" Student %s not found\n", del_id); } anykey(); } void search_item(void) { char search_id[10]; system("cls"); if(head->next == head) printf(" No student record\n"); else { printf(" Search student ID:"); gets(search_id); current = head->next; while((current != head) && (strcmp(search_id, current->stu_id)!=0)) current = current->next; if (current != head) { printf("--------------------------------------\n"); printf(" Student ID: %s\n", current->stu_id); printf(" Student name: %s\n", current->name); printf(" Student sex: %s\n", current->sex); printf(" Student _zip: %s\n", current->_zip); printf(" Student age: %s\n", current->age); printf(" Student address: %s\n", current->address); printf(" Student phone: %s\n", current->phone); printf(" Student qq: %s\n", current->qq); printf(" Student wechat: %s\n", current->wechat); printf(" Student birth: %s\n", current->birth); printf("--------------------------------------\n"); } else printf(" Student %s not found\n", search_id); } anykey(); } void modify_item(void) { int count = 0; char modify_id[10]; system("cls"); if(head->next == head) printf(" No student record\n"); else { printf(" Modify student ID:"); gets(modify_id); current = head->next; while(current != head) { if(strcmp(modify_id, current->stu_id) == 0) { printf("******************************\n"); printf(" Student ID: %s\n", current->stu_id); printf(" Student name: %s\n", current->name); printf(" Student sex: %s\n", current->sex); printf(" Student _zip: %s\n", current->_zip); printf(" Student age: %s\n", current->age); printf(" Student address: %s\n", current->address); printf(" Student phone: %s\n", current->phone); printf(" Student qq: %s\n", current->qq); printf(" Student wechat: %s\n", current->wechat); printf(" Student birth: %s\n", current->birth); printf(" Please enter new _zip:"); gets(current->_zip); printf(" Please enter new age:"); gets(current->age); printf(" Please enter new address:"); gets(current->address); printf(" Please enter new phone:"); gets(current->phone); printf(" Please enter new qq:"); gets(current->qq); printf(" Please enter new wechat:"); gets(current->wechat); printf(" Please enter new birth:"); gets(current->birth); count++; } current = current->next; } if(count > 0) printf(" %d student record(s) modified\n", count); else printf(" Student %s not found\n", modify_id); } anykey(); } void print_item(void) { int count = 0; system("cls"); if(head->next == head) printf(" No student record\n"); else { printf(" 序号 姓名 性别 邮编 年龄 地址 电话 QQ 微信账号 生日\n"); printf("-------------------------------------------------------------------------------\n"); current = head->next; while(current != head) { printf(" %-7s %-12s %-6s %-12s %-12s %-s %-6s %-12s %-12s %-s \n", current->stu_id, current->name, current->sex, current->_zip, current->age, current->address, current->phone, current->qq, current->wechat, current->birth); count++; current = current->next; if(count % 20 == 0) getch(); } printf("-------------------------------------------------------------------------------\n"); printf(" Total %d record(s) found\n", count); } anykey(); } void anykey(void) { printf(" Press any key to continue..."); getch(); }
经过验证,程序能够正常运行。