永发信息网

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

答案:2  悬赏:20  手机版
解决时间 2021-11-28 00:42
  • 提问者网友:蔚蓝的太阳
  • 2021-11-27 09:28
用〈〈数据结构〉〉中的双向链表作数据结构,结合C语言基本知识。编写一个通讯录管理系统。
最佳答案
  • 五星知识达人网友:青灯有味
  • 2021-11-27 10:46
#include
#include
#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;
}
全部回答
  • 1楼网友:时间的尘埃
  • 2021-11-27 11:48

#include 
#include 
#include 
#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 ");
    break;
   default:
    printf("Error "); 
  }
 }
 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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 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 == '') //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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(newNode->name + location++) = temp;
  temp = getchar();
 }
 *(newNode->name + location)='',location=0;
 while (temp == ' ' || temp == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(newNode->address + location++) = temp;
  temp = getchar();
 }
 *(newNode->address + location)='',location=0;
 while (temp == ' ' || temp == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(newNode->phone + location++) = temp;
  temp = getchar();
 }
 *(newNode->phone + location)='',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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 strlwr(cache);
 if(strcmp(cache,"all")==0)
 {
  if(elem==NULL)
   printf("there are no contacts in the list ");
  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 ",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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 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 ",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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 strlwr(cache);
 if((fileWrite=fopen(cache,"wt"))==NULL)
  printf("Cant't create this file ");
 else
 {
  while(elem!=NULL)
  {
   fprintf(fileWrite,"%s %s %s ",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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 strlwr(cache);
 if((fileRead=fopen(cache,"rt"))==NULL)
  printf("Cant't open this file ");
 else
 {
  temp=fgetc(fileRead);
  while(feof(fileRead)==0)
  {
   elem=(singlePerson *)malloc(sizeof(singlePerson));
   while (temp == ' ' || temp == ' ')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != ' '&&temp != ' ')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '';
   strlwr(cache);
   strcpy(elem->name,cache);
   while (temp == ' ' || temp == ' ')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != ' '&&temp != ' ')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '';
   strlwr(cache);
   strcpy(elem->address,cache);
   while (temp == ' ' || temp == ' ')
    temp = fgetc(fileRead); 
   location=0;
   while (temp != ' '&&temp != ' '&&temp != ' ')
   {
    *(cache + location++) = temp;
    temp = fgetc(fileRead);
   }
   *(cache + location) = '';
   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 == ' ')
  temp = getchar();
 while (temp != ' '&&temp != ' '&&temp != ' ')
 {
  *(cache + location++) = temp;
  temp = getchar();
 }
 *(cache + location) = '';
 strlwr(cache);
 if(strcmp(cache,"all")==0)
 {
  if(elem==NULL)
   printf("there are no contacts in the list ");
  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 ",cache);
 }
 free(cache);
}
void showSingle(singlePerson *elem)
{
 printf("name: %-10saddress: %-16sphone: %-12s ",elem->name,elem->address,elem->phone);
}
void help(void)
{
 printf("useradd: "
  " 功能:添加一个联系人. "
  " 格式:useradd name address phone. "
  " 例: useradd xiaoming shanghai 13590909090 ");
 printf("display: "
  " 功能:显示联系人. "
  " 格式:display name 或 display all. "
  " 例: display xiaoming ");
 printf("search: "
  " 功能:查找联系人. "
  " 格式:search name. "
  " 例: search xiaoming ");
 printf("userdel: "
  " 功能:删除联系人. "
  " 格式:useradd name 或 userdel all. "
  " 例: userdel xiaoming ");
 printf("save: "
  " 功能:保存当前的通讯录. "
  " 格式:save path. "
  " 例: save d:ackupaddressList.txt ");
 printf("load: "
  " 功能:读取通讯录. "
  " 格式:load path. "
  " 例: load d:ackupaddressList.txt ");
}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯