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,6:1
请输入name:A
请输入num:111111
请输入1,2,3,4,5,6:1
请输入name:B
请输入num:2222222
请输入1,2,3,4,5,6:1
请输入name:C
请输入num:3333333
请输入1,2,3,4,5,6:2
请输入要查询的name或num:A
查找到name:A, num:111111
请输入1,2,3,4,5,6:2
请输入要查询的name或num:D
未查询到!
请输入1,2,3,4,5,6:3
请输入要修改的name或num:A
查找到name:A, num:111111
请输入name:A
请输入num:555555
请输入1,2,3,4,5,6:5
name:C num:3333333
name:B num:2222222
name:A num:555555
请输入1,2,3,4,5,6:4
请输入要删除name:A
查找到name:A, num:555555
请输入1,2,3,4,5,6:5
name:C num:3333333
name:B num:2222222
请输入1,2,3,4,5,6:6

2019

被折叠的 条评论
为什么被折叠?



