Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- http#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;
- }
- //Alternative
- int InsertAfter(t_List* pL, t_Node* Node, int Info)
- {
- t_Node* temp;
- if (!pL)
- {
- printf("...");
- return NotOk;
- }
- if(!(temp=MyAlloc(pL->ps)))
- {
- printf("...");
- return NotOk;
- }
- temp->Info = Info;
- if (!Node)
- {
- temp->Next = pL->First;
- pL->First = temp;
- }
- else
- {
- temp->Next = Node->Next;
- Node->Next = temp;
- }
- pL->count++;
- if (!temp->Next)
- pL->Last = temp;
- return OK;
- }
- int InsertToTale(t_List* pL, int Info)
- {
- if (!pL)
- {
- printf("...");
- return NotOk;
- }
- return InsertAfter(pL, pL->Last, Info);
- }
- int DeletetAfter(t_List* pL, t_Node* Node, int Info)
- {
- t_Node* temp;
- if (!pL)
- {
- printf("...");
- return NotOk;
- }
- if(!pL->First)
- {
- printf("...");
- return NotOk;
- }
- if (!Node)
- {
- temp = pL->First;
- pL->First = temp->Next;
- pL->count--;
- if (!pL->First)pL->Last = NULL;
- return MyFree(pL->ps, temp);
- }
- temp = Node->Next;
- if (!temp)
- {
- printf("...");
- return NotOk;
- }
- Node->Next = temp->Next;
- pL->count--;
- if (!Node->Next)
- pL->Last = Node;
- return OK;
- }
- int DeleteNode(t_List* pL, t_Node* Node)
- {
- t_Node* cur, * pr;
- if(!pL)
- {
- printf("...");
- return NotOk;
- }
- for (cur = pL->First, pr = NULL; cur; cur = (pr = cur)->Next)
- if (cur == Node)
- return DeletetAfter(pL, pr);
- printf("..");
- return NotOk;
- }
- int ClearList(t_List* pL)
- {
- t_Node* cur, * next;
- if (!pL)
- {
- printf("...");
- return NotOk;
- }
- for (cur = pL->First; cur; cur=next)
- {
- next = cur->Next;
- MyFree(pL, cur);
- }
- pL->First = pL->Last = NULL;
- pL->count = 0;
- return OK;
- }
- void PrintList(t_List* pL)
- {
- t_Node* cur;
- if (!pL)
- {
- printf("...");
- return;
- }
- printf("\nList info: First=%p, Count = %d, Last=%p, Store=%p", pL->First, pL->count, pL->Last, pL->ps);
- if (pL->count <= 0)
- return;
- printf("\nNodes:");
- for (cur = pL->First; cur; cur = cur->Next)
- printf("...",cur, cur->Info, cur->Next);
- }
- t_List MyList;
- t_List MyStore;
- void main()
- {
- MyList.ps = &MyStore;
- MyStore.N_pack = 3;
- PrintList(&MyList);
- InsertAfter(&MyList, NULL, 10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement