应用实验 利用线性表实现一个通讯录管理,通讯录的数据格式如下: struct DataType int ID; //编号 char name[10]; //姓名 char ch; //性别 char phone[13]; //电话 char addr[31]; //地址 要求: ·实现通讯录的建立、增加、删除、修改、查询等功能; ·能够实现简单的菜单交互,即可以根据用户输入的命令,选择不同的操作; ·能够保存每次更新的数据(选做); ·能够进行通讯录分类,比如班级类、好友类、黑名单等(选做); ·编写main()函数测试线性表的正确性。
时间: 2023-05-18 19:05:23 浏览: 124
实验一 线性表的基本操作实现及其应用.pdf
以下是一个简单的通讯录管理程序的实现,使用线性表来存储通讯录数据:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 10
#define MAX_PHONE_LEN 13
#define MAX_ADDR_LEN 31
typedef struct {
int ID;
char name[MAX_NAME_LEN];
char ch;
char phone[MAX_PHONE_LEN];
char addr[MAX_ADDR_LEN];
} DataType;
typedef struct {
DataType *data;
int length;
int capacity;
} List;
void initList(List *list, int capacity) {
list->data = (DataType *)malloc(sizeof(DataType) * capacity);
list->length = 0;
list->capacity = capacity;
}
void destroyList(List *list) {
free(list->data);
list->data = NULL;
list->length = 0;
list->capacity = 0;
}
int insertList(List *list, int pos, DataType data) {
if (pos < 0 || pos > list->length) {
return 0;
}
if (list->length >= list->capacity) {
return 0;
}
for (int i = list->length - 1; i >= pos; i--) {
list->data[i + 1] = list->data[i];
}
list->data[pos] = data;
list->length++;
return 1;
}
int deleteList(List *list, int pos) {
if (pos < 0 || pos >= list->length) {
return 0;
}
for (int i = pos; i < list->length - 1; i++) {
list->data[i] = list->data[i + 1];
}
list->length--;
return 1;
}
int updateList(List *list, int pos, DataType data) {
if (pos < 0 || pos >= list->length) {
return 0;
}
list->data[pos] = data;
return 1;
}
int searchList(List *list, char *name) {
for (int i = 0; i < list->length; i++) {
if (strcmp(list->data[i].name, name) == 0) {
return i;
}
}
return -1;
}
void printList(List *list) {
printf("ID\tName\tSex\tPhone\t\tAddress\n");
for (int i = 0; i < list->length; i++) {
printf("%d\t%s\t%c\t%s\t%s\n", list->data[i].ID, list->data[i].name, list->data[i].ch, list->data[i].phone, list->data[i].addr);
}
}
void menu() {
printf("1. Add a contact\n");
printf("2. Delete a contact\n");
printf("3. Update a contact\n");
printf("4. Search a contact\n");
printf("5. Print all contacts\n");
printf("0. Exit\n");
}
int main() {
List list;
initList(&list, 100);
int choice = -1;
while (choice != 0) {
menu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1: {
DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter sex: ");
scanf(" %c", &data.ch);
printf("Enter phone: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
if (insertList(&list, list.length, data)) {
printf("Contact added successfully.\n");
} else {
printf("Failed to add contact.\n");
}
break;
}
case 2: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
if (deleteList(&list, pos)) {
printf("Contact deleted successfully.\n");
} else {
printf("Failed to delete contact.\n");
}
}
break;
}
case 3: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
DataType data;
printf("Enter ID: ");
scanf("%d", &data.ID);
printf("Enter name: ");
scanf("%s", data.name);
printf("Enter sex: ");
scanf(" %c", &data.ch);
printf("Enter phone: ");
scanf("%s", data.phone);
printf("Enter address: ");
scanf("%s", data.addr);
if (updateList(&list, pos, data)) {
printf("Contact updated successfully.\n");
} else {
printf("Failed to update contact.\n");
}
}
break;
}
case 4: {
char name[MAX_NAME_LEN];
printf("Enter name: ");
scanf("%s", name);
int pos = searchList(&list, name);
if (pos == -1) {
printf("Contact not found.\n");
} else {
printf("ID\tName\tSex\tPhone\t\tAddress\n");
printf("%d\t%s\t%c\t%s\t%s\n", list.data[pos].ID, list.data[pos].name, list.data[pos].ch, list.data[pos].phone, list.data[pos].addr);
}
break;
}
case 5: {
printList(&list);
break;
}
case 0: {
break;
}
default: {
printf("Invalid choice.\n");
break;
}
}
}
destroyList(&list);
return 0;
}
阅读全文