1. #include<stdio.h>
    2. #include<string.h>
    3. #include<malloc.h>
    4. #include<stdlib.h>
    5. #define null 0
    6. typedef struct
    7. {
    8. char num[5]; /*员工编号*/
    9. char name[8]; /*员工姓名*/
    10. char phone[9]; /*办公室电话号码*/
    11. char call[12]; /*手机号码*/
    12. }DataType;
    13. /*通讯录单链表的结点类型*/
    14. typedef struct node
    15. { DataType data; /*结点的数据域*/
    16. struct node *next; /*结点的指针域*/
    17. }ListNode,*LinkList;
    18. LinkList CreatList(void) /*建立一个通讯录链表*/
    19. { LinkList head,s,p;
    20. char ch;
    21. head=(ListNode *)malloc(sizeof(ListNode)); /*创建头结点*/
    22. head->next=NULL;
    23. p=head;
    24. do
    25. {
    26. s=(ListNode *)malloc(sizeof(ListNode)); /*创建一个记录结点*/
    27. printf("请输入姓名:");
    28. scanf("%s",s->data.name);
    29. printf("请输入编号:");
    30. scanf("%s",s->data.num);
    31. printf("请输入电话号码:");
    32. scanf("%s",s->data.phone);
    33. printf("请输入手机号码:");
    34. scanf("%s",s->data.call);
    35. s->next=p->next;
    36. p->next=s;
    37. printf("继续输入下一记录吗(y/n)?");
    38. scanf("\n%c",&ch);
    39. } while(ch=='y'|| ch=='Y');
    40. return head; /*返回头结点指针*/
    41. }
    42. ListNode *SearchList(LinkList head) /*查询函数*/
    43. {
    44. ListNode *p;
    45. int x; /* 按照编号或姓名在链表中查找由x的值决定*/
    46. char num[5]; /*员工编号*/
    47. char name[8]; /*员工姓名*/
    48. printf("1. 按编号查询 2.按姓名查询\n");
    49. printf("请选择:");
    50. scanf("%d",&x);
    51. if(x==1)
    52. { printf("请输入待查人的编号:");
    53. scanf("%s",num);
    54. for(p=head;p!=NULL&&strcmp(p->data.num,num)!=0;p=p->next);
    55. }
    56. else if(x==2)
    57. { printf("请输入待查人的姓名:");
    58. scanf("%s",name);
    59. for(p=head;p!=NULL&&strcmp(p->data.name,name)!=0;p=p->next);
    60. }
    61. return p; /*返回查指向找到的记录的指针,若没查找到,返回的是空指针*/
    62. }
    63. void InsertList(LinkList head,ListNode *p)
    64. {
    65. p->next=head->next;
    66. head->next=p;
    67. }
    68. void DelNode(LinkList head)
    69. {
    70. LinkList p,q;
    71. char ch;
    72. p=SearchList(head);
    73. if(!p)
    74. { printf("没有找到此人的记录\n");
    75. return;
    76. }
    77. printf("确定要删除吗?(y/n)");
    78. scanf("\n%c",&ch);
    79. if(ch=='n') return;
    80. for(q=head;q!=NULL&&q->next!=p;q=q->next);
    81. q->next=p->next;
    82. free(p);
    83. printf("删除成功\n");
    84. }
    85. void PrintList(LinkList head)
    86. {
    87. LinkList p;
    88. p=head->next;
    89. printf(" 编号 姓名 电话号码 手机号码\n");
    90. while(p!=NULL)
    91. {
    92. printf(" %s %s %s %s\n",
    93. p->data.num,p->data.name,p->data.phone,p->data.call);
    94. p=p->next;
    95. }
    96. }
    97. void Menu() /*显示菜单函数*/
    98. {
    99. printf(" *********软件公司员工通讯录**********\n");
    100. printf("1-通讯录建立 2-查看全部记录 3-查询 4-插入新员工 5-删除离开员工 0-退出\n");
    101. printf("请选择(输入0--5):");
    102. }
    103. void main()
    104. {
    105. int t;
    106. LinkList head,p;
    107. while(1)
    108. {
    109. Menu();
    110. scanf("%d",&t); /*选择系统子功能*/
    111. switch(t)
    112. { case 1:
    113. printf("建立通讯录\n");
    114. head=CreatList();
    115. break;
    116. case 2:
    117. printf("查看全部记录\n");
    118. PrintList(head);
    119. break;
    120. case 3:
    121. printf("查询\n");
    122. p=SearchList(head);
    123. if(p)
    124. { printf("编号 姓名 电话号码 手机号码\n");
    125. printf(" %s %s %s %s\n",
    126. p->data.num,p->data.name,p->data.phone,p->data.call);
    127. }
    128. else printf("无此人\n");
    129. break;
    130. case 4:
    131. printf("插入\n");
    132. p=(ListNode *)malloc(sizeof(ListNode));
    133. printf("请输入姓名:");
    134. scanf("%s",p->data.name);
    135. printf("请输入编号:");
    136. scanf("%s",p->data.num);
    137. printf("请输入电话号码:");
    138. scanf("%s",p->data.phone);
    139. printf("请输入手机号码:");
    140. scanf("%s",p->data.call);
    141. InsertList(head,p);
    142. break;
    143. case 5:
    144. printf("删除\n");
    145. DelNode(head);
    146. break;
    147. case 0:
    148. printf("退出\n");
    149. return;
    150. }
    151. }
    152. }