数据结构——链表实现通讯录

本文通过上文实现的链表,实现一个简单的通信录

通讯录功能:

1、添加联系人

2、查找联系人

3、删除联系人

4、显示所有联系人

 

(1)定义联系人信息结构

typedef struct {
	char key[15]; //名字
	char addr[20];
	char telephone[15];
	char mobile[12];
	//可根据需要增加QQ、E-mail等
} DATA;

(2)显示联系人模块

void ChainListAll(ChainListType *head)
{
	ChainListType *h;
	DATA data;
	h = head;
	int i = 0;

	printf("通信录所有联系人如下:\n");

	while (h) {
		//data = h->data;
		printf("联系人%d ", ++i);
		printf("姓名:%s ", h->data.key);
		printf("地址:%s ", h->data.addr);
		printf("电话:%s ", h->data.telephone);
		printf("手机:%s\n", h->data.mobile);
		h = h->next;
	}
	return;
}

(3)添加联系人模块

ChainListType *input(ChainListType *head)
{
	DATA data;
	printf("请输入联系人信息\n");
	printf("姓名:");
	scanf("%s", data.key);
	printf("地址:");
	scanf("%s", data.addr);
	printf("电话:");
	scanf("%s", data.telephone);
	printf("手机:");
	scanf("%s", data.mobile);

	return ChainListAddFirst(head, data);
}

(4)查找联系人模块

void find(ChainListType *head)
{
	ChainListType *h;
	DATA data;
	char name[15];

	printf("请输入查找姓名:");
	scanf("%s", name);
	h = ChainListFind(head, name);
	if (h) {
		data = h->data;
		printf("姓名:%s ", data.key);
		printf("地址:%s ", data.addr);
		printf("电话:%s ", data.telephone);
		printf("手机:%s ", data.mobile);
	} else {
		printf("未找到姓名为%s的联系人\n", name);
	}
}

(5)删除联系人模块

void delete(ChainListType *head)
{
	ChainListType *h = head;
	char name[15];
	int result = 0;

	printf("请输入要删除的姓名:");
	scanf("%s", name);

	result = ChainListDelete(head, name);
	if (result) {
		printf("删除成功!\n");
	} else {
		printf("删除失败!\n");
	}
}

(6)主函数模块

int main()
{
	ChainListType *head = NULL;
	int select;
	do {
		printf("\n________________________\n");
		printf("1.添加联系人\n");
		printf("2.查找联系人\n");
		printf("3.删除联系人\n");
		printf("4.显示所有联系人\n");
		printf("0.退出\n");
		scanf("%d", &select);
		switch (select) {
			case 1:
				printf("添加联系人\n");
				head = input(head);
				break;
			case 2:
				printf("查找联系人\n");
				find(head);
				break;
			case 3:
				printf("删除联系人\n");
				delete(head);
				break;
			case 4:
				printf("显示联系人\n");
				ChainListAll(head);
				break;
			case 0:
				return 0;
		}
	} while(1);

	return 0;
}

注:有兴趣的同学可以,结合MFC或者QT实现一个图形界面

转载于:https://my.oschina.net/wangande2014/blog/685387

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 C++ 实现双向链表通讯录的示例代码: ```c++ #include <iostream> #include <string> using namespace std; // 定义联系人结构体 struct Contact { string name; string phone; }; // 定义双向链表节点结构体 struct ListNode { Contact data; ListNode* prev; ListNode* next; ListNode(Contact value): data(value), prev(nullptr), next(nullptr) {} }; // 定义双向链表类 class DoubleLinkedList { private: ListNode* head; ListNode* tail; int size; public: DoubleLinkedList(): head(nullptr), tail(nullptr), size(0) {} ~DoubleLinkedList() { ListNode* p = head; while (p != nullptr) { ListNode* q = p->next; delete p; p = q; } head = tail = nullptr; size = 0; } void append(Contact value) { ListNode* node = new ListNode(value); if (tail == nullptr) { head = tail = node; } else { tail->next = node; node->prev = tail; tail = node; } size++; } void remove(ListNode* node) { if (node == nullptr) return; if (node == head) { head = head->next; if (head != nullptr) { head->prev = nullptr; } else { tail = nullptr; } } else if (node == tail) { tail = tail->prev; if (tail != nullptr) { tail->next = nullptr; } else { head = nullptr; } } else { node->prev->next = node->next; node->next->prev = node->prev; } delete node; size--; } void print() { ListNode* p = head; while (p != nullptr) { cout << "Name: " << p->data.name << ", Phone: " << p->data.phone << endl; p = p->next; } } int getSize() { return size; } }; int main() { DoubleLinkedList contacts; Contact c1 = {"Alice", "123456789"}; Contact c2 = {"Bob", "234567890"}; Contact c3 = {"Charlie", "345678901"}; contacts.append(c1); contacts.append(c2); contacts.append(c3); contacts.print(); cout << "Size: " << contacts.getSize() << endl; // 删除 Bob 的联系方式 ListNode* p = contacts.head; while (p != nullptr) { if (p->data.name == "Bob") { contacts.remove(p); break; } p = p->next; } contacts.print(); cout << "Size: " << contacts.getSize() << endl; return 0; } ``` 上述代码实现了一个基本的双向链表类,其中节点结构体 `ListNode` 包括了联系人结构体 `Contact` 和前驱节点和后继节点指针。在主函数中,我们创建了三个联系人并添加到双向链表中,然后打印出链表中所有联系人的信息。接着,我们使用循环遍历链表,找到名字为 Bob 的联系人并删除它,最后再次打印出链表中剩余的联系人信息和链表大小。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值