用〈〈数据结构〉〉中的双向链表作数据结构,结合C语言基本知识。编写一个通讯录管理系统。

1) 输入信息——enter();
2) 显示信息———display();
3) 查找以姓名作为关键字———search( );
4) 删除信息———delete();
5) 存盘———save ( );
6) 装入———load( ) ;

/*
目的:
 用〈〈数据结构〉〉中的双向链表作数据结构,结合C语言基本知识。编写一个通讯录管理系统。
功能:
 1) 输入信息——enter();
 2) 显示信息———display();
 3) 查找以姓名作为关键字———search( );
 4) 删除信息———delete();
 5) 存盘———save ( );
 6) 装入———load( ) ;
时间:
 2017年7月20日09:50:14
版本:
 1.0
环境:
 gcc 7.1 
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxNameLength 12
#define maxAddrLength 16
#define maxPhoneLength 12
#define maxCacheLength 64
/////////////////////////////////////////////////////////////////////////////////////// 
typedef struct person
{
 char name[maxNameLength];
 char address[maxAddrLength];
 char phone[maxPhoneLength];
 struct person *prior, *next;
} singlePerson;
typedef struct addressList
{
 singlePerson *head;
 int length;
} addressList;
//////////////////////////////////////////////////////////////////////////////////////
void initialization(addressList &record);
int getCommand(void);
void help(void);
void useradd(addressList &record);
void display(const addressList &record);
void showSingle(singlePerson *elem);
void userdel(addressList &record);
void search(const addressList &record);
void save(const addressList &record);
void load(addressList &record);
/////////////////////////////////////////////////////////////////////////////////////
int main(void)
{
 int engine = 1;
 addressList record;
 initialization(record);
 while (engine != -1)
 {
  fflush(stdin);
  printf("[enter command]#");
  engine = getCommand();
  switch(engine)
  {
   case 0:
    help();
    break;
   case 1:
    useradd(record);
    break;
   case 2:
    display(record);
    break;
   case 3:
    search(record);
    break;
   case 4:
    userdel(record);
    break;
   case 5:
    save(record);
    break;
   case 6:
    load(record);
    break;
   case -1:
    break;
   case 100:
    break;
   case 500:
    printf("NO such command\n");
    break;
   default:
    printf("Error\n"); 
  }
 }
 return 0;
}
/////////////////////////////////////////////////////////////////////////////////////
void initialization(addressList &record)
{
 record.head = NULL;
 record.length = 0;
}
int getCommand(void)
{
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 if (strcmp(cache, "help") == 0)
 {
  free(cache);
  return 0;
 }
 else if (strcmp(cache, "useradd") == 0)//添加 
 {
  free(cache);
  return 1;
 }
 else if (strcmp(cache, "display") == 0)//显示 
 {
  free(cache);
  return 2;
 }
 else if (strcmp(cache, "search") == 0)//查找 
 {
  free(cache);
  return 3;
 }
 else if (strcmp(cache, "userdel") == 0)//删除 
 {
  free(cache);
  return 4;
 }
 else if (strcmp(cache, "save") == 0)// 保存 
 {
  free(cache);
  return 5;
 }
 else if (strcmp(cache, "load") == 0)// 读取 
 {
  free(cache);
  return 6;
 }
 else if (strcmp(cache, "exit") == 0) 
 {
  free(cache);
  return -1;
 }
 else if (*cache == '\0') //NULL 
 {
  free(cache);
  return 100;
 }
 else
 {
  free(cache);
  return 500;
 }//default
}
void useradd(addressList &record)
{
 singlePerson *newNode = (singlePerson *)malloc(sizeof(singlePerson));
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(newNode->name + location++) = temp;
  temp = getchar();
 }
 *(newNode->name + location)='\0',location=0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(newNode->address + location++) = temp;
  temp = getchar();
 }
 *(newNode->address + location)='\0',location=0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(newNode->phone + location++) = temp;
  temp = getchar();
 }
 *(newNode->phone + location)='\0',location=0;
 newNode->next=record.head,newNode->prior=NULL;
 if(record.head!=NULL)
  record.head->prior=newNode;
 record.head=newNode;
 record.length++;
}
void userdel(addressList &record)
{
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 singlePerson *elem=record.head;
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 if(strcmp(cache,"all")==0)
 {
  if(elem==NULL)
   printf("there are no contacts in the list\n");
  else
  {
   while(elem->next!=NULL)
   {
    elem=elem->next;
    free(elem->prior);
   }
   free(elem);
   record.head=NULL,record.length=0;
  }
 }
 else
 {
  while(elem!=NULL)
  {
   if(strcmp(elem->name,cache)==0)
    break;
   elem=elem->next; 
  }
  if(elem!=NULL)
  {
   if(elem==record.head)
   {
    record.head=elem->next;
   }
   else
   {
    if(elem->next!=NULL)
     elem->next->prior=elem->prior;
    elem->prior->next=elem->next;
   }
   free(elem);
  }
  else
   printf("The contact called %s was not found\n",cache);
 }
 free(cache);
}
void search(const addressList &record)
{
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 singlePerson *elem=record.head;
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 while(elem!=NULL)
 {
  if(strcmp(elem->name,cache)==0)
   break;
  elem=elem->next; 
 }
 if(elem==NULL)
  printf("The contact called %s was not found\n",cache);
 else
  showSingle(elem);
 free(cache);
}
void save(const addressList &record)
{
 FILE *fileWrite;
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 singlePerson *elem=record.head;
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 if((fileWrite=fopen(cache,"wt"))==NULL)
  printf("Cant't create this file\n");
 else
 {
  while(elem!=NULL)
  {
   fprintf(fileWrite,"%s %s %s\n",elem->name,elem->address,elem->phone);
   elem=elem->next;
  }
  fclose(fileWrite);
 }
 free(cache);
}
void load(addressList &record)
{
 FILE *fileRead;
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 singlePerson *elem=NULL;
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 if((fileRead=fopen(cache,"rt"))==NULL)
  printf("Cant't open this file\n");
 else
 {
  temp=fgetc(fileRead);
  while(feof(fileRead)==0)
  {
   elem=(singlePerson *)malloc(sizeof(singlePerson));
   while (temp == ' ' || temp == '\t')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != '\t'&&temp != '\n')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '\0';
   strlwr(cache);
   strcpy(elem->name,cache);
   while (temp == ' ' || temp == '\t')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != '\t'&&temp != '\n')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '\0';
   strlwr(cache);
   strcpy(elem->address,cache);
   while (temp == ' ' || temp == '\t')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != '\t'&&temp != '\n')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '\0';
   strlwr(cache);
   strcpy(elem->phone,cache);
   elem->next=record.head,elem->prior=NULL;
   if(record.head!=NULL)
    record.head->prior=elem;
   record.head=elem; 
   record.length++;
   temp=fgetc(fileRead);
  }
  fclose(fileRead);
 }
 free(cache);
}
void display(const addressList &record)
{
 char *cache = (char *)malloc(maxCacheLength * sizeof(char));
 singlePerson *elem=record.head;
 char temp = getchar();
 int location = 0;
 while (temp == ' ' || temp == '\t')
  temp = getchar();
 while (temp != ' '&&temp != '\t'&&temp != '\n')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '\0';
 strlwr(cache);
 if(strcmp(cache,"all")==0)
 {
  if(elem==NULL)
   printf("there are no contacts in the list\n");
  else
  {
   while(elem!=NULL)
   {
    showSingle(elem);
    elem=elem->next; 
   }
  }
 }
 else
 {
  while(elem!=NULL)
  {
   if(strcmp(elem->name,cache)==0)
    break;
   elem=elem->next; 
  }
  if(elem!=NULL)
   showSingle(elem);
  else
   printf("The contact called %s was not found\n",cache);
 }
 free(cache);
}
void showSingle(singlePerson *elem)
{
 printf("name: %-10saddress: %-16sphone: %-12s\n",elem->name,elem->address,elem->phone);
}
void help(void)
{
 printf("useradd:\n"
  "\t功能:添加一个联系人.\n"
  "\t格式:useradd name address phone.\n"
  "\t例: useradd xiaoming shanghai 13590909090\n");
 printf("display:\n"
  "\t功能:显示联系人.\n"
  "\t格式:display name 或 display all.\n"
  "\t例: display xiaoming\n");
 printf("search:\n"
  "\t功能:查找联系人.\n"
  "\t格式:search name.\n"
  "\t例: search xiaoming\n");
 printf("userdel:\n"
  "\t功能:删除联系人.\n"
  "\t格式:useradd name 或 userdel all.\n"
  "\t例: userdel xiaoming\n");
 printf("save:\n"
  "\t功能:保存当前的通讯录.\n"
  "\t格式:save path.\n"
  "\t例: save d:\backup\addressList.txt\n");
 printf("load:\n"
  "\t功能:读取通讯录.\n"
  "\t格式:load path.\n"
  "\t例: load d:\backup\addressList.txt\n");
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-07-23
#include <stdio.h>
#include <stdlib.h>
#define Null 0
#define OverFlow -1
#define OK 0
#define Error -2
typedef int ElemType;
typedef struct node
{
ElemType data;
struct node *next;
}Node,*LinkList;
void Init_LinkList(LinkList *Head_pointer)
{//线性表初始化
*Head_pointer = Null;
}
int Insert_First(LinkList *Head_pointer,ElemType x)
{//采用头插法建立线性表
Node *p;
p=(Node *)malloc(sizeof Node);
if(p==NULL)
return OverFlow;
p->data=x;
p->next = *Head_pointer;
*Head_pointer = p;
return OK;
}
LinkList Location_LinkList(LinkList Head,ElemType x)
{//查询链表中某结点数据是否存在
LinkList p;
p=Head;
while(p!=Null)
{
if(p->data==x)
break;
p=p->next;
}
return p;
}
int Delete_LinkList(LinkList *Head_pointer,ElemType x)
{//删除链表中的某一结点
Node *p,*q;
p=*Head_pointer;
if(p->data==x)//考虑头结点就是要删除的元素
{
*Head_pointer =(*Head_pointer)->next;
free(p);
return OK;
}
else
{
q=p;p=p->next;
while(p!=Null)
{
if(p->data==x)
{
q->next = p->next;
free(p);
return OK;
}
q=p;p=p->next;
}
}
return Error;
}
void Show_LinkList(LinkList Head)
{//遍历线性表中的元素
LinkList p=Head;
int i=0;
printf("---链表打印---\n");
if(p==Null)
printf("空表\n");
while(p!=Null)
{
printf("[%d]:%d\t",i++,p->data);
p=p->next;
}
}
int Length_LinkList(LinkList Head)
{//求链表长度
LinkList p=Head;
int sum=0;
while(p!=Null)
{
sum++;
p=p->next;
}
return sum;
}
void SetNull_LinkList(LinkList *Head_pointer)
{//链表清空
LinkList p,q;
p=*Head_pointer;
while(p!=Null)
{
q=p;p=p->next;free(q);
}
}
int main(void)
{
LinkList Head;
int i;
Node *loca;
ElemType x;
Init_LinkList(&Head);
do
{
printf("\n");
printf("1---插入一个元素(Insert)\n");
printf("2---查询一个元素(Locate)\n");
printf("3---删除一个元素(Delete)\n");
printf("4---显示所有元素(Show)\n");
printf("5---计算表的长度(Length)\n");
printf("6---退出\n");
scanf("%d",&i);
switch(i)
{
case 1:printf("请输入要插入的分数:\n");
scanf("%d",&x);
if(Insert_First(&Head,x)!=OK)
printf("插入失败\n");
break;
case 2: printf("请输入要查询的分数:\n");
scanf("%d",&x);
loca = Location_LinkList(Head,x);
if(loca!=Null)
printf("查询成功\n");
else
printf("查询失败\n");
break;
case 3: printf("请输入要删除的分数:\n");
scanf("%d",&x);
if(Delete_LinkList(&Head,x)!=OK)
printf("删除失败\n");
else
printf("删除成功\n");
break;
case 4:Show_LinkList(Head);
break;
case 5:printf("表的长度是:%d",Length_LinkList(Head));
break;
case 6:break;
}
}while(i!=6);
SetNull_LinkList(&Head);
printf("链表已清空,程序退出...\n");
return 0;
}本回答被网友采纳
相似回答