学生通讯录管理系统采用链表实现

#include "stdio.h"
#include
#include "stdlib.h"
typedef struct student{ //通讯录结点类型
char num[10]; //学号
char name[20]; //姓名
char major[20]; //专业
char phone[20]; //电话
char addr[30]; //地址
} DataType;
typedef struct node { //结点类型定义
DataType data; //结点数据域
struct node *next; //结点指针域
} ListNode,*LinkList;
//*************************菜单显示*****************************//
void menu_select( )
{
system("cls");
printf(" 通讯录管理系统 \n");
printf(" ============================================= \n");
printf(" 1.通讯者结点的插入 \n");
printf(" 2.通讯者结点的删除 \n");
printf(" 3.通讯者结点的查询 \n");
printf(" 4.通讯录链表的输出 \n");
printf(" 5.返回原菜单 \n");
printf(" 0.退出通讯录管理系统 \n");
printf(" ============================================= \n");
printf(" 请选择0-5:");
}
//**************************通讯链表的建立*************************//
void CreateList(LinkList &head){
head=(LinkList)malloc(sizeof(ListNode));
head->next=NULL;
}
//**************************通讯录结点的插入*************************//
void InsertNode(LinkList &head)
{
system("cls");
printf("**************************通讯录结点的插入*************************\n");
char flag ;
do{
LinkList p,q=head->next,r=head;
printf("请输入节点信息:\n");
p=(LinkList)malloc(sizeof(ListNode )); //申新结点
printf("请输入学号:");
scanf("%s",p->data.num);
printf("请输入姓名:");
scanf("%s",p->https://www.360docs.net/doc/ba9571115.html,);
printf("请输入专业:");
scanf("%s",p->data.major);
printf("请输入电话:");
scanf("%s",p->data.phone);
printf("请输入地址:");
scanf("%s",p->data.addr);
while(q&& !(strcmp(p->data.num,q->data.num)<0))
{
r=q;
q=q->next;
}
r->next=p;
p->next=q;
printf("插入成功\n");

printf("Would you like to continue to insert (y/n)");
scanf("%*c%c",&flag);
}while(flag=='y'||flag=='Y');
printf("input 0:exit 5:return the menu : ");

}
//**************************通讯者结点的查询**************************//
ListNode *ListFind(LinkList head)
{ // 有序通讯录链表上的查找
LinkList p=head->next;
char num[10];
char name[9];
int xz;
printf("===================================================================

==\n");
printf(" 1. 按编号查询 \n");
printf(" 2. 按姓名查询 \n");
printf("=====================================================================\n");
printf(" 请选择: ");
scanf("%d",&xz);
if (xz==1)
{
printf(" 请输入要查找者的编号:");
scanf("%s",num);
while (p&&strcmp(p->data.num,num)!=0)
p=p->next;
}
else if(xz==2)
{
printf(" 请输入要查找者的姓名:");
scanf("%s",name);
while(p&&strcmp(p->https://www.360docs.net/doc/ba9571115.html,,name)!=0)
p=p->next;
}
return p;
}
//***************************通讯者结点的删除****************************//
void DelNode(LinkList &head)
{
system("cls");
printf("********************* 通讯录信息的删除 **********************\n");
char jx;
char flag;
LinkList p,q=head;
if(!head->next)
printf("该链表为空,请向链表中插入记录!\n");
else
{
do{
p=ListFind(head); //调用查找函数
if (p==NULL)
{
printf(" 没有查到要删除的通讯者!\n");
goto jump;
}
printf(" 真的要删除该结点吗?(y/n):");
scanf("%*c%c",&jx);
if (jx=='y'||jx=='Y')
{
while (q &&q->next!=p)
q=q->next;
q->next=p->next; //删除结点
free(p); //释放被删结点空间
printf(" 通讯者已被删除!\n");
}
jump: printf("Would you like to continue to delete (y/n)");
scanf("%*c%c",&flag);
}while(flag=='Y'||flag=='y');
}
printf("input 0:exit 5:return the menu : ");



}
//***************************通讯录链表的输出*****************************//
void PrintList(LinkList head)
{
system("cls");
LinkList p;
p=head->next;
if(!head->next)
printf("该链表为空,请向链表中插入记录!\n");
else
{
printf(" 编号 姓名 专业 电话 地址 \n");
printf(" \n");
while (p!=NULL)
{
printf("%-12s%-10s%-11s%-14s%-8s\n",p->data.num,p->https://www.360docs.net/doc/ba9571115.html,,p->data.major,p->data.phone,p->data.addr);
printf(" \n");
p=p->next; //后移一个结点
}
}
printf("input 0:exit 5:return the menu : ");
}
void print(DataType e)
{
printf(" 编号 姓名 专业 电话 地址 \n");
printf("%-12s%-10s%-11s%-14s%-8s\n",e.num,https://www.360docs.net/doc/ba9571115.html,,e.major,e.phone,e.addr);
}
void SearchNode(LinkList head,void (*vi)(DataType ))
{
system("cls");
printf("********************* 通讯录信息的查询 **********************\n");
char flag;
LinkList p;
if(!head->next)
printf("该链表为空,请向链表中插入记录!\n");
else
{
do{
p=ListFind(head);
if(p==NULL)
{
printf(" 没有查到要查询的通讯者! \n");
}
else
{
vi(p->data);
}


printf("Would you like to search?(y/n)");
scanf("%*c%c",&flag);
}while(flag=='y'||flag=='Y');
}
printf("input 0:exit 5:return the menu : ");

}
void main()
{ int i;
LinkList head;
CreateList(head);
menu_select();
scanf("%d",&i);
while(i)
{
switch(i)
{
case 1:InsertNode(head);break;////通讯录信息的插入
case 2:DelNode(head);break;//通讯录信息的删除
case 3:SearchNode(head,print);break; //通讯录信息的查询
case 4:PrintList(head);break;//通讯录链表的输出
case 5:menu_select();break;
case 0:break;
}
scanf("%d",&i);
}
printf("\t** 您已选择退出文件管理系统,谢谢您的使用!** \n");
}


相关文档
最新文档