Advertisement
DaniDori

Untitled

Dec 2nd, 2019
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #define OK 1
  4. #define NotOk 0
  5. typedef struct s_Node
  6. {
  7. int Info;
  8. struct s_Node* Next;
  9. }t_Node;
  10. typedef struct s_Store
  11. {
  12. int N_Pack;
  13. t_Node* AllocatedBlocks;
  14. t_Node* FirstFree;
  15. int FreeCount;
  16. }t_Store;
  17. typedef struct s_List
  18. {
  19. t_Store* ps;
  20. t_Node* First;
  21. t_Node* Last;
  22. int count;
  23. }t_List;
  24. t_Node* MyAlloc(t_Store* pS)
  25. {
  26. t_Node* temp;
  27. int i;
  28. if (!pS)
  29. {
  30. printf("...");
  31. return NULL;
  32. }
  33. if (pS->FirstFree)
  34. {
  35. temp = pS->FirstFree;
  36. pS->FirstFree = temp->Next;
  37. pS->FreeCount--;
  38. return temp;
  39. }
  40. if (pS->N_Pack < 2)
  41. {
  42. printf("...");
  43. return NULL;
  44. }
  45. if (!(temp = (t_Node)malloc((pS->N_Pack) + sizeof(*temp))))
  46. {
  47. printf("..");
  48. return NULL;
  49. }
  50. temp->Next = pS->AllocatedBlocks;
  51. pS->FreeCount = pS->N_Pack - 1;
  52. pS->AllocatedBlocks = temp;
  53. for (i = 1; i < pS->N_Pack; i++)
  54. temp[i].Next = temp + i + 1;
  55. temp[pS->N_Pack].Next = NULL;
  56. pS->FirstFree = temp + 1;
  57. return MyAlloc(pS);
  58. }
  59. int MyFree(t_Store* pS, t_Node* Node)
  60. {
  61. if (!pS)
  62. {
  63. printf("...");
  64. return NULL;
  65. }
  66. Node->Next = pS->FirstFree;
  67. pS->FirstFree = Node;
  68. pS->FreeCount++;
  69. return OK;
  70. }
  71. int MyFinishFree(t_Store* pS)
  72. {
  73. t_Node* temp;
  74. if (!pS)
  75. {
  76. printf("...");
  77. return NotOk;
  78. }
  79. for (; pS->AllocatedBlocks;)
  80. {
  81. temp = pS->AllocatedBlocks;
  82. pS->AllocatedBlocks = temp->Next;
  83. }
  84. free(temp);
  85. pS->FirstFree = NULL;
  86. pS->FreeCount = 0;
  87. return OK;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement