C 内存池的简单实现

内存池可以优化内存分配的效率,这里在单链表的基础上增加内存池功能:
pool.c

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

#define MAX 1024

struct node *pool = NULL;
static int count = 0;

struct node {
	int value;
	struct node *next;
};

void insert_node(struct node **head, int value);
struct node *find_node(struct node *head, int target);
void delete_node(struct node *head, int target);
void head_insert_node(struct node **head, int value);
void end_insert_node(struct node **head, int value);
void free_node(struct node **head);
void free_pool(void);

#if 0
void end_insert_node(struct node **head, int value)
{
	struct node *new;
	struct node *current;

	current = *head;

	new = (struct node *)malloc(sizeof(struct node));
	if(new == NULL) {
		printf("%s: malloc memory failed!\n", __func__);
		exit(1);
	}

	new->value = value;
	
	if(*head == NULL) {
		*head = new;
		new->next = NULL;
	} else {
		while(current->next != NULL) {
			current = current->next;
		}

		current->next = new;
		new->next = NULL;
	}
}
#endif

void end_insert_node(struct node **head, int value)
{
	struct node *new;
	static struct node *tail;

	if(pool != NULL) {
		new = pool;
		pool = pool->next;
		count--;
	} else {
		new = (struct node *)malloc(sizeof(struct node));
		if(new == NULL) {
			printf("%s: malloc memory failed!\n", __func__);
			exit(1);
		}
	}

	new->value = value;
	
	if(*head == NULL) {
		*head = new;
		new->next = NULL;
	} else {
		tail->next = new;
		new->next = NULL;
	}

	tail = new;
}

void print_node(struct node *head)
{
	struct node *current;

	current = head;
	while(current != NULL) {
		printf("%d ", current->value);
		current = current->next;
	}

	putchar('\n');
}

void free_node(struct node **head)
{
	struct node *current, *temp;
	
	current = *head;

	while(current->next != NULL) {
		temp = current;
		free(current);
		current = temp->next;
	}
}

void free_pool(void)
{
	struct node *temp;

	while(pool != NULL) {
		temp = pool;
		pool = pool->next;
		free(temp);
	}
}

void insert_node(struct node **head, int value)
{
	struct node *prev;
	struct node *current;
	struct node *new;

	current = *head;
	prev = NULL;

	while(current != NULL && current->value < value) {
		prev = current;
		current = current->next;
	}

	new = (struct node *)malloc(sizeof(struct node));
	if(new == NULL) {
		printf("%s: molloc memory for new failed!\n", __func__);
		exit(1);
	}

	new->value = value;
	new->next = current;

	if(prev == NULL) {
		*head = new;
	} else {
		prev->next = new;
	}
}

struct node *find_node(struct node *head, int target)
{
	struct node *current = NULL;

	current = head;

	while(current != NULL) {
		
		if(current->value == target) {
			break;
		}

		current = current->next;
	}

	return current;
}

void head_insert_node(struct node **head, int value)
{
	struct node *new;
	struct node *temp;

	new = (struct node *)malloc(sizeof(struct node));
	if(new == NULL) {
		printf("%s: molloc memory failed.\n", __func__);
		exit(1);
	}

	new->value = value;
	
	if(*head == NULL) {
		*head = new;
		new->next = NULL;
	} else {
		temp = *head;
		*head = new;
		new->next = temp;
	}
	
}

void delete_node(struct node *head, int target)
{
	struct node *prev;
	struct node *current;
	struct node *temp;

	current = head;
	prev = NULL;
	while(current != NULL) {
		
		if(current->value == target) {
			break;
		}

		prev = current;
		current = current->next;
	}

	prev->next = current->next;

	if(count < MAX) {
		if(pool != NULL) {
			temp = pool;
			pool = current;
			current->next = pool;
		} else {
			pool = current;
			current->next = NULL;
		}

		count ++;
		printf("%s: the count is %d\n", __func__, count);
	} else {

		free(current);
	}
}

int main(void)
{
	struct node *head = NULL, *result = NULL;
	int input;
	int search_value;
	int delete_value;

	while(1) {
		printf("%s: please input a int value(-1 means end)\n", __func__);
		scanf("%d", &input);

		if(input == -1) {
			break;
		}
			
//		insert_node(&head, input);
		end_insert_node(&head, input);
		print_node(head);
	}

	printf("%s: please input the value what you want find.\n", __func__);
	scanf("%d", &search_value);

	result = find_node(head, search_value);
	if(result == NULL) {
		printf("%s: Can not find result.\n", __func__);
	} else {
		printf("%s: Have found result %d.\n", __func__, result->value);
	}

	printf("%s: please input the value you want delete.\n", __func__);
	scanf("%d", &delete_value);

	delete_node(head, delete_value);
	print_node(head);

	free_node(&head);

	free_pool();
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值