C++应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
时间: 2023-05-18 14:05:32 浏览: 113
以下是一个简单的通讯录管理程序的实现,使用线性表来存储通讯录数据:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 10
#define MAX_PHONE_LEN 13
#define MAX_ADDR_LEN 31
struct DataType {
int ID;
char name[MAX_NAME_LEN];
char ch;
char phone[MAX_PHONE_LEN];
char addr[MAX_ADDR_LEN];
};
typedef struct Node {
struct DataType data;
struct Node *next;
} Node;
Node *createList() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = NULL;
return head;
}
void destroyList(Node *head) {
Node *p = head->next;
while (p != NULL) {
Node *q = p->next;
free(p);
p = q;
}
free(head);
}
void printList(Node *head) {
Node *p = head->next;
while (p != NULL) {
printf("%d %s %c %s %s\n", p->data.ID, p->data.name, p->data.ch, p->data.phone, p->data.addr);
p = p->next;
}
}
Node *findNode(Node *head, int ID) {
Node *p = head->next;
while (p != NULL) {
if (p->data.ID == ID) {
return p;
}
p = p->next;
}
return NULL;
}
void insertNode(Node *head, struct DataType data) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = data;
p->next = head->next;
head->next = p;
}
void deleteNode(Node *head, int ID) {
Node *p = head->next;
Node *prev = head;
while (p != NULL) {
if (p->data.ID == ID) {
prev->next = p->next;
free(p);
return;
}
prev = p;
p = p->next;
}
}
void updateNode(Node *head, int ID, struct DataType data) {
Node *p = findNode(head, ID);
if (p != NULL) {
p->data = data;
}
}
void queryNode(Node *head, char *name) {
Node *p = head->next;
while (p != NULL) {
if (strcmp(p->data.name, name) == 0) {
printf("%d %s %c %s %s\n", p->data.ID, p->data.name, p->data.ch, p->data.phone, p->data.addr);
}
p = p->next;
}
}
int main() {
Node *head = createList();
while (1) {
printf("1. Add a contact\n");
printf("2. Delete a contact\n");
printf("3. Update a contact\n");
printf("4. Query a contact\n");
printf("5. Print all contacts\n");
printf("6. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
if (choice == 1) {
struct DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter gender (M/F): ");
scanf(" %c", &data.ch);
printf("Enter phone number: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
insertNode(head, data);
} else if (choice == 2) {
int ID;
printf("Enter ID: ");
scanf("%d", &ID);
deleteNode(head, ID);
} else if (choice == 3) {
int ID;
printf("Enter ID: ");
scanf("%d", &ID);
struct DataType data;
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter gender (M/F): ");
scanf(" %c", &data.ch);
printf("Enter phone number: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
updateNode(head, ID, data);
} else if (choice == 4) {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
queryNode(head, name);
} else if (choice == 5) {
printList(head);
} else if (choice == 6) {
break;
} else {
printf("Invalid choice\n");
}
}
destroyList(head);
return 0;
}
注意,这个程序只是一个简单的示例,还有很多需要改进的地方,比如输入的合法性检查、数据的持久化存储等。
阅读全文
相关推荐
















