内存池可以优化内存分配的效率,这里在单链表的基础上增加内存池功能:
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;
}