数据结构双向链表设计学生通讯录管理系统
1.确定数据结构,定义的结构体类型应包括:学号、姓名、性别、班级、
专业和家庭住址等。
2 .程序应具有以下基本功能:
功能模块:学生信息的录入模块、浏览学生信息模块、查询学生信息模块(学号)、插入、删除学生信息模块。各个功能模块用函数的形式来实现。
设计要求:
1. 数据结构是双向链表。
2. 使用文件进行存储和管理。程序启动时可从文件中读取通信信息,或从
键盘输入通信信息;运行过程中如添加或删除记录时也可对文件进行存取;
退出前可选择将通信信息保存到文件中。
3. 不同的功能使用不同的函数实现(模块化),对每个函数的功 展开
#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 _class[50];
char major[50];
char address[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.insert item\n");
printf(" 2.delete item\n");
printf(" 3.search item\n");
printf(" 4.print item\n");
printf(" 5.modify item\n");
printf(" 6.quit\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->_class, "0");
strcpy(ptr->major, "0");
strcpy(ptr->address, "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", ptr->stu_id, ptr->name, ptr->sex, ptr->_class, ptr->major, ptr->address) != 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\n", current->stu_id, current->name, current->sex, current->_class, current->major, current->address);
current = current->next;
}
fclose(fptr);
}
void insert_item(void)
{
system("cls");
ptr = (link)malloc(sizeof *ptr);
printf(" Student ID:");
gets(ptr->stu_id);
printf(" Name:");
gets(ptr->name);
printf(" Sex:");
gets(ptr->sex);
printf(" _class:");
gets(ptr->_class);
printf(" Major:");
gets(ptr->major);
printf(" Address:");
gets(ptr->address);
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 _class: %s\n", current->_class);
printf(" Student major: %s\n", current->major);
printf(" Student address: %s\n", current->address);
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 _class: %s\n", current->_class);
printf(" Student major: %s\n", current->major);
printf(" Student address: %s\n", current->address);
printf(" Please enter new _class:");
gets(current->_class);
printf(" Please enter new major:");
gets(current->major);
printf(" Please enter new address:");
gets(current->address);
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(" STU_ID NAME SEX _CLASS MAJOR ADDRESS\n");
printf("-------------------------------------------------------------------------------\n");
current = head->next;
while(current != head)
{
printf(" %-7s %-12s %-6s %-12s %-12s %-s\n", current->stu_id, current->name, current->sex, current->_class, current->major, current->address);
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();
}