动态链表的创建、节点内存空间申请以及释放

  • Post author:
  • Post category:其他


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);

}

}



版权声明:本文为zxccaoya原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。