Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- #define OK 1
- #define NotOk 0
- typedef struct s_Node
- {
- int Info;
- struct s_Node* Next;
- }t_Node;
- typedef struct s_Store
- {
- int N_Pack;
- t_Node* AllocatedBlocks;
- t_Node* FirstFree;
- int FreeCount;
- }t_Store;
- typedef struct s_List
- {
- t_Store* ps;
- t_Node* First;
- t_Node* Last;
- int count;
- }t_List;
- t_Node* MyAlloc(t_Store* pS)
- {
- t_Node* temp;
- int i;
- if (!pS)
- {
- printf("...");
- return NULL;
- }
- if (pS->FirstFree)
- {
- temp = pS->FirstFree;
- pS->FirstFree = temp->Next;
- pS->FreeCount--;
- return temp;
- }
- if (pS->N_Pack < 2)
- {
- printf("...");
- return NULL;
- }
- if (!(temp = (t_Node)malloc((pS->N_Pack) + sizeof(*temp))))
- {
- printf("..");
- return NULL;
- }
- temp->Next = pS->AllocatedBlocks;
- pS->FreeCount = pS->N_Pack - 1;
- pS->AllocatedBlocks = temp;
- for (i = 1; i < pS->N_Pack; i++)
- temp[i].Next = temp + i + 1;
- temp[pS->N_Pack].Next = NULL;
- pS->FirstFree = temp + 1;
- return MyAlloc(pS);
- }
- int MyFree(t_Store* pS, t_Node* Node)
- {
- if (!pS)
- {
- printf("...");
- return NULL;
- }
- Node->Next = pS->FirstFree;
- pS->FirstFree = Node;
- pS->FreeCount++;
- return OK;
- }
- int MyFinishFree(t_Store* pS)
- {
- t_Node* temp;
- if (!pS)
- {
- printf("...");
- return NotOk;
- }
- for (; pS->AllocatedBlocks;)
- {
- temp = pS->AllocatedBlocks;
- pS->AllocatedBlocks = temp->Next;
- }
- free(temp);
- pS->FirstFree = NULL;
- pS->FreeCount = 0;
- return OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement