Advertisement
DaniDori

Structs(Vasil)

Dec 2nd, 2019
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. http#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. }
  89. //Alternative
  90.  
  91. int InsertAfter(t_List* pL, t_Node* Node, int Info)
  92. {
  93. t_Node* temp;
  94. if (!pL)
  95. {
  96. printf("...");
  97. return NotOk;
  98. }
  99. if(!(temp=MyAlloc(pL->ps)))
  100. {
  101. printf("...");
  102. return NotOk;
  103. }
  104. temp->Info = Info;
  105. if (!Node)
  106. {
  107. temp->Next = pL->First;
  108. pL->First = temp;
  109. }
  110. else
  111. {
  112. temp->Next = Node->Next;
  113. Node->Next = temp;
  114. }
  115. pL->count++;
  116. if (!temp->Next)
  117. pL->Last = temp;
  118. return OK;
  119. }
  120. int InsertToTale(t_List* pL, int Info)
  121. {
  122. if (!pL)
  123. {
  124. printf("...");
  125. return NotOk;
  126. }
  127. return InsertAfter(pL, pL->Last, Info);
  128. }
  129.  
  130.  
  131. int DeletetAfter(t_List* pL, t_Node* Node, int Info)
  132. {
  133. t_Node* temp;
  134. if (!pL)
  135. {
  136. printf("...");
  137. return NotOk;
  138. }
  139. if(!pL->First)
  140. {
  141. printf("...");
  142. return NotOk;
  143. }
  144. if (!Node)
  145. {
  146. temp = pL->First;
  147. pL->First = temp->Next;
  148. pL->count--;
  149. if (!pL->First)pL->Last = NULL;
  150. return MyFree(pL->ps, temp);
  151. }
  152. temp = Node->Next;
  153. if (!temp)
  154. {
  155. printf("...");
  156. return NotOk;
  157. }
  158. Node->Next = temp->Next;
  159. pL->count--;
  160. if (!Node->Next)
  161. pL->Last = Node;
  162. return OK;
  163. }
  164. int DeleteNode(t_List* pL, t_Node* Node)
  165. {
  166. t_Node* cur, * pr;
  167. if(!pL)
  168. {
  169. printf("...");
  170. return NotOk;
  171. }
  172. for (cur = pL->First, pr = NULL; cur; cur = (pr = cur)->Next)
  173. if (cur == Node)
  174. return DeletetAfter(pL, pr);
  175. printf("..");
  176. return NotOk;
  177. }
  178. int ClearList(t_List* pL)
  179. {
  180. t_Node* cur, * next;
  181. if (!pL)
  182. {
  183. printf("...");
  184. return NotOk;
  185. }
  186. for (cur = pL->First; cur; cur=next)
  187. {
  188. next = cur->Next;
  189. MyFree(pL, cur);
  190. }
  191. pL->First = pL->Last = NULL;
  192. pL->count = 0;
  193. return OK;
  194. }
  195. void PrintList(t_List* pL)
  196. {
  197. t_Node* cur;
  198. if (!pL)
  199. {
  200. printf("...");
  201. return;
  202. }
  203. printf("\nList info: First=%p, Count = %d, Last=%p, Store=%p", pL->First, pL->count, pL->Last, pL->ps);
  204. if (pL->count <= 0)
  205. return;
  206. printf("\nNodes:");
  207. for (cur = pL->First; cur; cur = cur->Next)
  208. printf("...",cur, cur->Info, cur->Next);
  209. }
  210.  
  211. t_List MyList;
  212. t_List MyStore;
  213. void main()
  214. {
  215. MyList.ps = &MyStore;
  216. MyStore.N_pack = 3;
  217. PrintList(&MyList);
  218. InsertAfter(&MyList, NULL, 10);
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement