单链表的应用———简单的通讯录

本文介绍如何利用单链表数据结构设计并实现一个基本的通讯录系统,包括添加联系人、查找联系人和删除联系人等操作。通过实例阐述了单链表在实际问题中的应用。
摘要由CSDN通过智能技术生成

本人初学数据结构,代码还不够完善,还望各位大神,可以指点一二。<img alt="奋斗" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif" />
#include <stdio.h>//建立通讯录
#include <stdlib.h>
#include <string.h>
typedef struct Node{
     char name[20];
	 char  Iphonedata[11];
	 int  qq;
	 struct Node *next;
}Node,*LinkList;
void CreatList(LinkList &L)
{ 
    int i;
	LinkList r,p;
	L=(LinkList)malloc(sizeof(Node));
	r=L;
	
	printf("进行通信录的输入,1 or 0?\n");
	scanf("%d",&i);
	getchar();
	while(i)
	{
		p=(LinkList)malloc(sizeof(Node));
		printf("请输入姓名:\n");
		gets(p->name);
		//getchar();
		printf("请输入电话号码:\n");
		gets(p->Iphonedata);//为什么连着是char类型的   下面的输入会存在这个存入呢???
		//getchar();
		//scanf("%ld",&p->Iphonedata);
		printf("请输入qq号:\n");
		//gets(p->qq);
		scanf("%d",&
  • 7
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单通讯录系统的单链表实现,使用C语言编写: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> /* 定义通讯录结构体 */ typedef struct contact { char name[20]; char phone[20]; struct contact *next; } Contact; /* 创建通讯录 */ Contact *create_contact() { Contact *head, *tail, *pnew; head = tail = (Contact*)malloc(sizeof(Contact)); printf("请输入姓名:"); scanf("%s", tail->name); printf("请输入电话:"); scanf("%s", tail->phone); while (strcmp(tail->name, "0") != 0) { pnew = (Contact*)malloc(sizeof(Contact)); printf("请输入姓名:"); scanf("%s", pnew->name); if (strcmp(pnew->name, "0") == 0) { break; } printf("请输入电话:"); scanf("%s", pnew->phone); tail->next = pnew; tail = pnew; } tail->next = NULL; return head; } /* 显示通讯录 */ void show_contact(Contact *head) { Contact *p = head; printf("姓名\t\t电话\n"); while (p != NULL) { printf("%s\t\t%s\n", p->name, p->phone); p = p->next; } } /* 查找联系人 */ Contact *find_contact(Contact *head, char *name) { Contact *p = head; while (p != NULL) { if (strcmp(p->name, name) == 0) { return p; } p = p->next; } return NULL; } /* 添加联系人 */ Contact *add_contact(Contact *head, char *name, char *phone) { Contact *pnew = (Contact*)malloc(sizeof(Contact)); strcpy(pnew->name, name); strcpy(pnew->phone, phone); pnew->next = head; head = pnew; return head; } /* 删除联系人 */ Contact *delete_contact(Contact *head, char *name) { Contact *p = head, *pre = NULL; while (p != NULL) { if (strcmp(p->name, name) == 0) { if (pre == NULL) { head = p->next; } else { pre->next = p->next; } free(p); return head; } pre = p; p = p->next; } return head; } /* 修改联系人 */ void modify_contact(Contact *head, char *name, char *phone) { Contact *p = find_contact(head, name); if (p != NULL) { strcpy(p->phone, phone); } } int main() { Contact *head, *p; char name[20], phone[20]; int choice; head = create_contact(); do { printf("========================\n"); printf("1. 显示所有联系人\n"); printf("2. 查找联系人\n"); printf("3. 添加联系人\n"); printf("4. 删除联系人\n"); printf("5. 修改联系人\n"); printf("0. 退出\n"); printf("========================\n"); printf("请输入选项:"); scanf("%d", &choice); switch (choice) { case 1: show_contact(head); break; case 2: printf("请输入姓名:"); scanf("%s", name); p = find_contact(head, name); if (p != NULL) { printf("姓名\t\t电话\n"); printf("%s\t\t%s\n", p->name, p->phone); } else { printf("未找到该联系人\n"); } break; case 3: printf("请输入姓名:"); scanf("%s", name); printf("请输入电话:"); scanf("%s", phone); head = add_contact(head, name, phone); printf("添加成功!\n"); break; case 4: printf("请输入姓名:"); scanf("%s", name); head = delete_contact(head, name); printf("删除成功!\n"); break; case 5: printf("请输入姓名:"); scanf("%s", name); printf("请输入电话:"); scanf("%s", phone); modify_contact(head, name, phone); printf("修改成功!\n"); break; case 0: break; default: printf("输入有误,请重新输入!\n"); break; } } while (choice != 0); return 0; } ``` 在这个例子中,我们使用单链表来实现一个简单通讯录系统,包括创建通讯录、显示通讯录、查找联系人、添加联系人、删除联系人、修改联系人等功能。通过这个例子,您可以了解单链表在实际应用中的使用方法,并掌握基本的链表操作技巧。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值