✍小甲鱼C语言 P48 内存池

✍小甲鱼C语言 P48 内存池

P48 内存池

1.通讯录管理程序

在这里插入图片描述
本人写的程序:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX 1024

struct Node 
{
	char name[10];
	char num[10];
	struct Node* next;
};

struct Node* pool = NULL;
int count;

void addNode(struct Node **head)
{
	struct Node* new,*temp;

	//如果内存池不为空,则直接从里面获取空间
	if (pool!=NULL)
	{
		new = pool;
		pool = pool->next;
		count--;
	}
	else//如果内存池为空,则调用malloc函数申请新的内存空间
	{
		new = (struct Node*)malloc(sizeof(struct Node));
		if (new == NULL)
		{
			printf("内存分配失败!");
			exit(1);
		}
	}
	
	printf("请输入name:");
	scanf("%s", new->name);
	printf("请输入num:");
	scanf("%s", new->num);
	if (*head==NULL)
	{
		*head = new;
		new->next = NULL;
	}
	else
	{
		temp = *head;
		*head = new;
		new->next = temp;
	}
}

void  findNode(struct Node *head,char temp[])
{
	struct Node *target;
	target = head;

	while (target!=NULL && strcmp(target->name, temp) && strcmp(target->num, temp))
	{
		target = target->next;
	}
	if (target==NULL)
	{
		printf("未查询到!\n");
	}
	else
	{
		printf("查找到name:%s, num:%s\n", target->name,target->num);
	}

}

void changeNode(struct Node** head,char temp[10])
{
	struct Node *current,*previous;
	
	current = *head;
	previous = NULL;
	while (current != NULL && strcmp(current->name, temp)&& strcmp(current->num, temp))//strcmp相同返回0 不同返回1
	{
		previous = current;
		current = current->next;
	}
	if (current == NULL)
	{
		printf("未查询到要修改的Node!\n");
	}
	else
	{
		printf("查找到name:%s, num:%s\n", current->name, current->num);
		struct Node* new = NULL;
		new = (struct Node*)malloc(sizeof(struct Node));
		if (new == NULL)
		{
			printf("内存分配失败!");
			exit(1);
		}
		printf("请输入name:");
		scanf("%s", new->name);
		printf("请输入num:");
		scanf("%s", new->num);
		
		new->next = current->next;
		previous->next = new;
	}
}
void deleteNode(struct Node** head, char temp[10])
{
	struct Node* current, * previous,*temp1;

	current = *head;
	previous = NULL;
	while (current != NULL && strcmp(current->name, temp) && strcmp(current->num, temp))//strcmp相同返回0 不同返回1
	{
		previous = current;
		current = current->next;
	}
	if (current == NULL)
	{
		printf("未查询到要修改的Node!\n");
	}
	else
	{
		printf("查找到name:%s, num:%s\n", current->name, current->num);
		if (previous ==NULL)//第一个节点
		{
			*head = current->next;
		}
		else
			previous->next = current->next;
		//判断内存池是不是有空位
		if (count<MAX)
		{
			if (pool!=NULL)
			{
				temp1 = pool;
				pool = current;
				current->next = temp1;
			}
			else
			{
				pool = current;
				current->next = NULL;
			}
		}
		else
		{
			free(current);
		}
		count++;	
	}
}

void printNode(struct Node *head)
{
	struct Node* temp;
	temp = head;

	while (temp!=NULL)
	{
		printf("name:%s  ",temp->name);
		printf("num:%s\n", temp->num);
		temp = temp->next;
	}
}

void releaseNode(struct Node **head)
{
	struct Node* temp;
	while (*head!=NULL)
	{
		temp = *head;
		*head = (*head)->next;
		free(temp);
	}
}

void releasePool()
{
	struct Node* temp;
	while (pool!= NULL)
	{
		temp = pool;
		pool =pool->next;
		free(temp);
	}
}

int main()
{
	printf("输入1.创建新的联系人\n");
	printf("输入2.查找联系人\n");
	printf("输入3.更改联系人\n");
	printf("输入4.删除联系人\n");
	printf("输入5.显示联系人\n");
	printf("输入6.退出\n");
	
	struct Node* head = NULL;
	int input;
	char name[10];
	char temp[10];
	while (1)
	{
		printf("请输入1,2,3,4,5,6:");
		scanf("%d",&input);
		if (input==6)
		{
			releaseNode(&head);
			releasePool();
			break;
		}
		switch (input)
		{
		case 1:		
			addNode(&head);
			break;
		case 2:
			printf("请输入要查询的name或num:");
			scanf("%s",temp);
			findNode(head,temp);
			break;
		case 3:
			printf("请输入要修改的name或num:");
			scanf("%s", temp);
			changeNode(&head, temp);
			break;
		case 4:
			printf("请输入要删除name:");
			scanf("%s", temp);
			deleteNode(&head, temp);
			break;
		case 5:
			printNode(head);
			break;
		}
	}
	return 0;
}

结果测试:

输入1.创建新的联系人
输入2.查找联系人
输入3.更改联系人
输入4.删除联系人
输入5.显示联系人
输入6.退出
请输入1,2,3,4,5,61
请输入name:A
请输入num:111111
请输入1,2,3,4,5,61
请输入name:B
请输入num:2222222
请输入1,2,3,4,5,61
请输入name:C
请输入num:3333333
请输入1,2,3,4,5,62
请输入要查询的name或num:A
查找到name:A, num:111111
请输入1,2,3,4,5,62
请输入要查询的name或num:D
未查询到!
请输入1,2,3,4,5,63
请输入要修改的name或num:A
查找到name:A, num:111111
请输入name:A
请输入num:555555
请输入1,2,3,4,5,65
name:C  num:3333333
name:B  num:2222222
name:A  num:555555
请输入1,2,3,4,5,64
请输入要删除name:A
查找到name:A, num:555555
请输入1,2,3,4,5,65
name:C  num:3333333
name:B  num:2222222
请输入1,2,3,4,5,66
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值