单向链表的增删改查

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
typedef struct
{
	char key[10];
	char name[20];
	int age;
}Data;
typedef struct Node
{
	Data nodeData;
	struct Node *nextNode;
}CLType;
/*追加结点*/
CLType *CLAddEnd(CLType *head, Data nodeData)
{
	CLType *node, *htemp;
	if (!(node = (CLType *)malloc(sizeof(CLType))))
	{
		printf("申请内存失败!");
		return 0;
	}
	else
	{
		node->nodeData = nodeData;
		node->nextNode = NULL;
		if (head == NULL)
		{
			head = node;
			return head;
		}
		htemp = head;
		while (htemp->nextNode != NULL)
		{
			htemp = htemp->nextNode;
		}
		htemp->nextNode = node;
		return head;
	}
}
/*插入结点*/
CLType *CLAddFirst(CLType*head, Data nodeData)
{
	CLType *node;
	if (!(node = (CLType *)malloc(sizeof(CLType))))
	{
		printf("申请内存失败!\n");
		return 0;
	}
	else
	{
		node->nodeData = nodeData;
		node->nextNode = head;//指向头指针所指结点
		head = node;//头指针指向新增结点
		return head;
	}
}

CLType *CLFindNode(CLType *head, char *key)
{
	CLType *htemp;
	htemp = head;
	while (htemp)
	{
		if(strcmp(htemp->nodeData.key, key)==0)
		{
			return htemp;
		}

		htemp = htemp->nextNode;
	}
	return NULL;
}


CLType *CLInsertNode(CLType *head, char *findkey, Data nodeData)
{
	CLType *node, *nodetemp;
	if (!(node= (CLType *)malloc(sizeof(CLType))))
	{
		printf("申请内存失败!");
		return 0;
	}
	node->nodeData = nodeData;
	nodetemp = CLFindNode(head, findkey);
	if (nodetemp)
	{
		node->nextNode = nodetemp->nextNode;
		nodetemp->nextNode = node;
	}
	else
	{
		printf("未找到正确的插入位置!");
		free(node);
	}
	return head;
}
/*删除结点*/
int CLDeleteNode(CLType *head, char *key)
{

	CLType	*node, *htemp;
	htemp = head;
	node = head;
	while (htemp)
	{
		if (strcmp(htemp->nodeData.key, key) == 0)
		{
			node->nextNode = htemp->nextNode;
			free(htemp);
			return 1;
		}
		else
		{
			node = htemp;
			htemp = htemp->nextNode;
		}
	}
	return 0;
}

int CLLength(CLType *head)
{
	CLType *htemp;
	int Len = 0;
	htemp = head;
	while (htemp)
	{
		
		htemp = htemp->nextNode;//处理下一结点
		Len++;//累加结点数量
	}
	return Len;//返回结点数量
}
void CLAllNode(CLType *head)
{
	CLType *htemp;
	Data nodeData;
	htemp = head;
	printf("当前链表共有%d个结点,链表所有数据如下:\n", CLLength(head));
	while (htemp)
	{
		nodeData = htemp->nodeData;
		printf("结点(%s,%s,%d)\n", nodeData.key, nodeData.name, nodeData.age);
		htemp = htemp->nextNode;//处理下一结点
	}
}

void main()
{
	CLType *node, *head = NULL;
	Data nodeData;

	char key[10], findkey[10];
	
	printf("链表测试,先输入链表中的数据,格式为:关键字 姓名 年龄\n");
	do
	{
		fflush(stdin);
		scanf("%s", nodeData.key);
		if (strcmp(nodeData.key, "0") == 0)
		{
			break;
		}
		else
		{
			scanf("%s%d", nodeData.name, &nodeData.age);
	
			head = CLAddEnd(head, nodeData);
		
		}

	} while (1);
	printf("显示所有结点:\n");
	CLAllNode(head);

	printf("\n请演示插入结点,输入插入位置的关键字\n");
	scanf("%s", findkey);
	printf("输入插入结点的数据(关键字 姓名 年龄)\n");
	scanf("%s%s%d", nodeData.key, nodeData.name, &nodeData.age);

	head = CLInsertNode(head, findkey, nodeData);
	CLAllNode(head);


	printf("\n演示删除结点:输入要删除的关键字!\n");
	fflush(stdin);
	scanf("%s", key);
	CLDeleteNode(head, key);
	CLAllNode(head);


	printf("\n演示在链表中的查找,输入查找关键字!\n");
	fflush(stdin);
	scanf("%s", key);
	node = CLFindNode(head, key);
	if (node)
	{
		nodeData = node->nodeData;
		printf("关键字%s对应的结点为(%s,%s,%d)\n", key, nodeData.key, nodeData.name, nodeData.age);

	}
	else
	{
		printf("在链表中未找到关键字为%s的结点!\n", key);
	}

	system("pause");


}

你可能感兴趣的:(c++)