数据结构:链表

链表

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

typedef struct {
	char key[15];
	char name[20];
	int age;
}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);


int main() {
	ChainListType *node, *head = NULL;
	DATA data;
	char key[15], findkey[15];
	printf("输入链表中的数据,包括关键字、姓名、年龄、关键字输入0,则退出:\n");
	do {
		fflush(stdin);
		scanf("%s", data.key);
		if (strcmp(data.key, "0") == 0)
		{
			break;
		}
		scanf("%s%d", data.name, &data.age);

	} while (1);

	printf("该链表共有%d个节点。\n", ChainListLength(head));
	ChainListAll(head);

	printf("\n插入节点,输入插入位置的关键字:");
	scanf("%s", &findkey);//输入插入位置关键字
	printf("输入插入节点的数据(关键字  姓名  年龄):");
	scanf("%s%s%d", data.key, data.name, &data.age);
	head = ChainListInsert(head, findkey, data);

	ChainListAll(head);

	printf("\n在链表中查找,输入查找关键字:");
	fflush(stdin);
	scanf("%s", key);
	node = ChainListFind(head, key);
	if (node)
	{
		data = node->data;
		printf("关键字%s对应的节点数据为(%s,%s,%d)\n", key, data.key, data.name, data.age);
	}
	else {
		printf("在链表中未找到关键字为%s的节点!\n", key);
	}
	printf("\n在链表中删除节点,输入要删除的关键字:");
	fflush(stdin);
	scanf("%s", key);
	ChainListDelete(head, key);
	ChainListAll(head);

	getch();
	return 0;
}


void ChainListAll(ChainListType *head)
{//显示所有节点
	ChainListType *h;
	DATA data;
	h = head;
	printf("链表所有数据如下:\n");
	while (h)
	{
		data = h->data;
		printf("(%s,%s,%d)\n", data.key, data.name, data.age);
		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、付费专栏及课程。

余额充值