C语言编写的电话簿小程序
有创建,添加,删除,显示四个功能
#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> int lenth = 1; typedef struct PhoneBook { char phoneNum[20]; char name[20]; struct PhoneBook *next; }phoneBook; void Creat(phoneBook **); void Add(phoneBook **); void Delete(phoneBook **); void Display(phoneBook *); int main() { int chioce; phoneBook *head = NULL; printf("\n请选择\n"); printf("1.创建电话簿\n"); printf("2.添加号码\n"); printf("3.删除号码\n"); printf("4.显示电话簿\n"); printf("5.退出程序\n"); printf("\n"); while(1) { scanf("%d",&chioce); switch(chioce) { case 1: Creat(&head); break; case 2: Add(&head); break; case 3: Delete(&head); break; case 4: Display(head); break; case 5: exit(0); break; default: break; } printf("\n请选择\n"); printf("1.创建电话簿\n"); printf("2.添加号码\n"); printf("3.删除号码\n"); printf("4.显示电话簿\n"); printf("5.退出程序\n"); printf("\n"); } return 0; } void Creat(phoneBook ** head) { *head = (phoneBook *)malloc(sizeof(phoneBook)); if(*head == NULL) { printf("分配内存失败,程序自动退出\n"); exit(0); } (*head)->next = NULL; printf("创建完毕\n\n"); } void Add(phoneBook ** head) { phoneBook *q, *p; int n, i; q = (phoneBook *)malloc(sizeof(phoneBook)); if(q == NULL) { printf("分配内存失败,程序自动退出\n"); exit(0); } q->next = NULL; printf("请输入姓名电话号码: "); scanf("%s %s", q->name, q->phoneNum); printf("\n请输入要添加到电话簿的位置: "); scanf("%d",&n); while(n > lenth || n <= 0) { printf("\n无效的输入,请重新输入: "); scanf("%d",&n); } lenth++; p = *head; for(i = 1; i < n; i++) { p = p->next; } q->next = p->next; p->next = q; printf("\n添加完毕\n\n"); } void Delete(phoneBook **head) { int n; int i; phoneBook *p = *head, *q; printf("请输入要删除的信息的位置: "); scanf("%d",&n); while(n > lenth || n <= 0) { printf("\n无效的输入,请重新输入: "); scanf("%d",&n); } for(i = 1; i < n; i++) { p = p->next; } q = p->next; p->next = q->next; free(q); lenth--; printf("\n删除完毕\n\n"); } void Display(phoneBook * head) { phoneBook *q; int i = 1; if(head->next == NULL) { printf("电话簿上没有任何信息\n"); exit(0); } q = head->next; printf("\n 姓名\t\t号码\n"); while(q != NULL) { printf("%d. %s\t\t%s\n", i, q->name, q->phoneNum); q = q->next; i++; } }