数据结构:链表应用

链表应用

#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>

typedef struct {
	char key[15];
	char addr[20];
	char telephone[15];
	char mobile[12];
}DATA;
typedef struct Node{
	DATA data;
	struct Node *next;
}ChainListType;


void ChainListAll(ChainListType *head);//显示所有节点
ChainListType *ChainListAddEnd(ChainListType *head, DATA data);//添加在链表结尾
ChainListType *ChainListAddFirst(ChainListType *head, DATA data);//添加节点在表头
ChainListType *ChainListFind(ChainListType *head, char *key);//按关键字查找内容
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data);//插入节点到链表指定位置
int ChainListDelete(ChainListType *head, char *key);//删除指定关键字的节点
int ChainListLength(ChainListType *head);

ChainListType *input(ChainListType *head);//向通讯录中输入的信息
void find(ChainListType *head);//查找联系人
void delete(ChainListType *head);//删除

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");
		printf("\n---------------------\n");

		select = getch();
		switch(select) {
		case '1':
			printf("\n添加联系人\n");
			head = input(head);
			break;
		case '2':
			printf("\n查找联系人\n");
			find(head);
			break;
		case '3':
			printf("\n显示联系人\n");
			ChainListAll(head);
			break;
		case '0':
			printf("\n退出\n");
			return 0;
		}
	} while (select != '0');

	getch();
	return 0;
}

void delete(ChainListType *head) {
	//删除
	int i;
	ChainListType *h = head;
	char name[15];
	printf("请输入要删除的姓名:");
	scanf("%s", name);

	i = ChainListDelete(head, name);

}
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\n", data.key);
		printf("地址:%s\n", data.addr);
		printf("电话:%s\n", data.telephone);
		printf("手机:%s\n", data.mobile);
	}

}

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);//调用添加函数

}

void ChainListAll(ChainListType *head)
{//显示所有节点
	ChainListType *h;
	DATA data;
	h = head;
	printf("链表所有数据如下:\n");
	while (h)
	{
		data = h->data;

		printf("姓名:%s\n", data.key);
		printf("地址:%s\n", data.addr);
		printf("电话:%s\n", data.telephone);
		printf("手机:%s\n", data.mobile);

		h = h->next;
	}
	return;


}
ChainListType *ChainListAddEnd(ChainListType *head, DATA data)
{
	//添加在链表结尾
	ChainListType *node, *h;
	if (!(node = (ChainListType *)malloc(sizeof(ChainListType))))
	{
		printf("为保存节点数据申请内存失败!\n");
		return NULL;
	}
	node->data = data;
	node->next = NULL;
	if (head == NULL)
	{
		head = node;
		return head;
	}
	h = head;
	while (h->next != NULL)
	{
		h = h->next;
	}
	h->next = node;
	return head;

}
ChainListType *ChainListAddFirst(ChainListType *head, DATA data)
{
	//添加节点在表头
	ChainListType *node, *h;
	if (!(node = (ChainListType *)malloc(sizeof(ChainListType))))
	{
		printf("为保存节点数据申请内存失败!\n");
		return NULL;
	}
	node->data = data;
	node->next = head;
	head = node;
	return head;

}
ChainListType *ChainListFind(ChainListType *head, char *key)
{
	//按关键字查找内容
	ChainListType *h;
	h = head;
	while (h) {
		if (strcmp(h->data.key, key) == 0)
		{
			return h;
		}
		h = h->next;
	}
	return NULL;
	
}
ChainListType *ChainListInsert(ChainListType *head, char *findkey, DATA data)
{

	//插入节点到链表指定位置
	ChainListType *node, *node1;
	if (!(node = (ChainListType *)malloc(sizeof(ChainListType))))
	{
		printf("分配内存失败!\n");
		return 0;
	}

	node->data = data;
	node1 = ChainListFind(head, findkey);

	if (node1)
	{
		node->next = node1->next;
		node1->next = node;
	}
	else {
		free(node);
		printf("未找到插入位置\n");
		return head;
	}

}
int ChainListDelete(ChainListType *head, char *key)
{
	//删除指定关键字的节点
	ChainListType *node, *h;
	node = h = head;
	while (h)
	{
		if (strcmp(h->data.key, key) == 0) {
			node->next = h->next;
			free(h);
			return 1;
		}
		else {
			node = h;
			h = h->next;
		}
	}
	return 0;

}
int ChainListLength(ChainListType *head)
{
	//链表长度
	ChainListType *h;
	int i = 0;
	h = head;
	while (h) {
		i++;
		h = h->next;
	}
	return i;
	
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值