動態連結串列的建立、節點記憶體空間申請以及釋放
1.動態連結串列的初始化:
typedef struct _STACK{
void* data;int size;
struct _STACK* next;
struct _STACK* pre;
} STACK;
STACK *stack;
STACK *_stack;void initStack()
{
stack = (STACK*)malloc(sizeof(STACK));
stack->data = (DATA*)malloc(sizeof(DATA));
stack->size = sizeof(DATA);
stack->next = NULL;
stack->pre = NULL;
_stack = NULL;
}
2.建立和記憶體申請:
void creatStack()
{
if(_stack == NULL) {
_stack = stack;
}else {
STACK* pStack = (STACK*)malloc(sizeof(STACK));
pStack->data = (DATA*)malloc(sizeof(DATA));
pStack->size = sizeof(STACK);
pStack->next = NULL;
_stack->next = pStack;
pStack->pre = _stack;
_stack = _stack->next;
}
}
3.釋放記憶體
void releaseStack()
{
DATA* data;
while(NULL != stack)
{
STACK* sta = stack;
stack = stack->next;
data = (DATA*)sta->data;
if(data->type == Array)
free(data->t.a.valp);
free(sta->data);
free(sta);
}
}