1117

来源:互联网 发布:电脑怎么删除软件 编辑:程序博客网 时间:2024/06/07 22:16
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct node
{
char name[20];
char sex[5];
char phone[20];
struct node *prior, *next;
};


typedef struct node Node;
typedef struct node * Dbllink;


void menu(Dbllink *head);                      //菜单函数声明


void is_malloc_ok(Dbllink new_node)            //判断内存分配是否成功
{
if (new_node == NULL)
{
printf("          malloc error!\n");
exit(-1);
}
}


void create_newnode(Dbllink *new_node)           //为新结点分配空间
{
*new_node = (Dbllink) malloc(sizeof(Node));
is_malloc_ok(*new_node);
}


void create_link(Dbllink *head)                 //创建链表
{
create_newnode(head);
(*head)->next = (*head)->prior = *head;
}


void insert_node_head(Dbllink *head)            //头插新输入的成员
{
Dbllink new_node = NULL;
create_newnode(&new_node);
Node node;
printf("          请输入姓名:");
scanf("%s",node.name);
strcpy(new_node->name,node.name);
printf("          请输入性别:");
scanf("%s",node.sex);
strcpy(new_node->sex, node.sex);
printf("          请输入电话:");
scanf("%s",node.phone);
strcpy(new_node->phone, node.phone);

new_node->prior = (*head)->prior;           //头插
new_node->next = *head;
(*head)->prior->next = new_node;
(*head)->prior = new_node;


menu(head);
}


void display_node(Dbllink *head)                  //显示
{
Dbllink tmp;
tmp = (*head)->next;
if (tmp == *head)
{
printf("          该通讯录为空!\n");
}
while(tmp != *head)
{
printf("          姓名:%3s,性别:%2s,电话:%s\n",tmp->name,tmp->sex,tmp->phone);
tmp = tmp->next;
}


menu(head);
}


void search_node(Dbllink *head)               //查找函数
{
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("          *              1.按姓名查找             *\n");
printf("          *              2.按电话查找             *\n");
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int i;
char nam[20];
char ph[20];
do
{
printf("\n");
printf("          请输入功能序号:");
scanf("%d",&i);
}while(i < 1 || i > 2);
if (i == 1)                             //按姓名查找
{
printf("          请输入姓名:");
scanf("%s",nam);
    Dbllink tmp;
    tmp  = (*head)->next;


   if(tmp == *head)
   {
   printf("          该通讯录为空!\n");
   }
else
{
       while (strcmp(tmp->name, nam) != 0 && tmp->next != *head)
    {
    tmp = tmp->next;
    }
      if (strcmp(tmp->name, nam) == 0)
    {
    printf("          姓名:%s,性别:%s电话:%s\n",tmp->name,tmp->sex,tmp->phone);
    }
    else
    {
    printf("          没有此人!\n");
    }
}
}
else                                     //按电话查找
{
printf("          请输入电话:");
scanf("%s",ph);
    Dbllink tmp;
    tmp  = (*head)->next;


   if(tmp == *head)
   {
   printf("          该通讯录为空!\n");
   }
else
{
       while (strcmp(tmp->phone, ph) != 0 && tmp->next != *head)
    {
    tmp = tmp->next;
    }
    if (strcmp(tmp->phone, ph) == 0)
    {
    printf("          姓名:%s,性别:%s电话:%s\n",tmp->name,tmp->sex,tmp->phone);
    }
    else
    {
    printf("          没有此人!\n");
    }
}
}
menu(head);
}


void change_node(Dbllink *head)               //修改函数
{
char nam[20];
Node node;
Dbllink tmp;
tmp = (*head)->next;
printf("          请输入需要修改成员姓名:");
scanf("%s",nam);
if(tmp == *head)
{
printf("          该通讯录为空!\n");
}
else
{
   while (strcmp(tmp->name, nam) != 0 && tmp->next != *head)
    {
    tmp = tmp->next;
    }
      if (strcmp(tmp->name, nam) == 0)
    {
    printf("          姓名:%s,性别:%s电话:%s\n",tmp->name,tmp->sex,tmp->phone);
        printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("          *               1.修改姓名              *\n");
printf("          *               2.修改性别              *\n");
printf("          *               3.修改电话              *\n");
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int i;
do
{
printf("\n");
printf("          请输入功能序号:");
scanf("%d",&i);
}while(i < 1 || i > 3);
if (i == 1)
{
printf("          请输入修改后姓名:");
scanf("%s",node.name);
strcpy(tmp->name,node.name);
}
else if (i == 2)
{
printf("          请输入修改后性别:");
scanf("%s",node.sex);
strcpy(tmp->sex,node.sex);
}
else
{
printf("          请输入修改后电话:");
scanf("%s",node.phone);
strcpy(tmp->phone,node.phone);
}
    }
    else
    {
    printf("          没有此人!\n");
    }
}
menu(head);
}




void delete_node(Dbllink *head)               //删除函数
{
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("          *              1.按姓名删除             *\n");
printf("          *              2.按电话删除             *\n");
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int i;
char nam[20];
char ph[20];
do
{
printf("\n");
printf("          请输入功能序号:");
scanf("%d",&i);
}while(i < 1 || i > 2);
if (i == 1)
{
printf("          请输入姓名:");
scanf("%s",nam);
    Dbllink tmp;
    tmp = (*head)->next;


   if(tmp == *head)
   {
   printf("          该通讯录为空!\n");
   }
else
{
       while (strcmp(tmp->name, nam) != 0 && tmp->next != *head)
    {
    tmp = tmp->next;
    }
      if (strcmp(tmp->name, nam) == 0)       //找到该结点后 链接前后结点并释放当前结点
    {
tmp->prior->next = tmp->next;
tmp->next->prior = tmp->prior;
free(tmp);
    }
    else
    {
    printf("          没有此人!\n");
    }
}
}
else
{
printf("          请输入电话:");
scanf("%s",ph);
    Dbllink tmp;
    tmp  = (*head)->next;


   if(tmp == *head)
   {
   printf("          该通讯录为空!\n");
   }
else
{
       while (strcmp(tmp->phone, ph) != 0 && tmp->next != *head)
    {
    tmp = tmp->next;
    }
    if (strcmp(tmp->phone, ph) == 0)
    {
tmp->prior->next = tmp->next;
tmp->next->prior = tmp->prior;
free(tmp);
    }
    else
    {
    printf("          没有此人!\n");
    }
}
}
menu(head);
}


void release(Dbllink *head)           //释放空间 退出程序
{
Dbllink tmp = NULL;
tmp = (*head)->next;
while (tmp != *head)
{
(*head)->next = tmp->next;
free(tmp);
tmp = (*head)->next;
}
exit(-1);
}


void menu(Dbllink *head)                        //菜单
{
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
printf("          *                 1.录入                *\n");
printf("          *                 2.显示                *\n");
printf("          *                 3.查询                *\n");
printf("          *                 4.修改                *\n");
printf("          *                 5.删除                *\n");
printf("          *                 6.退出                *\n");
printf("          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
int i;
do
{
printf("\n");
printf("          请输入功能序号:");
scanf("%d",&i);
}while(i < 1 || i > 6);
switch(i)
{
case 1: insert_node_head(head);break;
case 2: display_node(head);break;
case 3: search_node(head);break;
case 4: change_node(head);break;
case 5: delete_node(head);break;
case 6: release(head);break;
default:printf("          error!\n");
}
}


int main()
{
Dbllink head = NULL;


create_link(&head);


menu(&head);


    return 0;
}
0 0
原创粉丝点击