📄 dce2_list.c
字号:
********************************************************************/void * DCE2_QueueLast(DCE2_Queue *queue){ if (queue == NULL) return NULL; queue->current = queue->tail; if (queue->current != NULL) return queue->current->data; return NULL;}/******************************************************************** * Function: DCE2_QueuePrev() * * Puts the current pointer in the queue to the previous node in * the queue and returns the data associated with it. This in * combination with DCE2_QueueLast is useful in a for loop to * iterate over the items in a queue in backwards order. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: * void * * The data in the previous node in the queue. * NULL if the queue object passed in is NULL, or we are at * the beginning of the queue and there are no previous nodes. * ********************************************************************/void * DCE2_QueuePrev(DCE2_Queue *queue){ if (queue == NULL) return NULL; if (queue->current != NULL) { queue->current = queue->current->prev; if (queue->current != NULL) return queue->current->data; } return NULL;}/******************************************************************** * Function: DCE2_QueueEmpty() * * Removes all of the nodes in a queue. Does not delete the queue * object itself. Calls data free function for data if it is * not NULL. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: None * ********************************************************************/void DCE2_QueueEmpty(DCE2_Queue *queue){ DCE2_QueueNode *n; if (queue == NULL) return; n = queue->head; while (n != NULL) { DCE2_QueueNode *tmp = n->next; if (queue->data_free != NULL) queue->data_free(n->data); DCE2_Free((void *)n, sizeof(DCE2_QueueNode), queue->mtype); n = tmp; } queue->head = queue->tail = queue->current = NULL; queue->num_nodes = 0;}/******************************************************************** * Function: DCE2_QueueDestroy() * * Destroys the queue object and all of the data associated with it. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: None * ********************************************************************/void DCE2_QueueDestroy(DCE2_Queue *queue){ if (queue == NULL) return; DCE2_QueueEmpty(queue); DCE2_Free((void *)queue, sizeof(DCE2_Queue), queue->mtype);}/******************************************************************** * Function: DCE2_StackNew() * * Creates and initializes a new stack object. * * Arguments: * DCE2_StackDataFree * An optional free function for the data inserted into * the stack. If NULL is passed in, the user will be * responsible for freeing data left in the stack. * DCE2_MemType * The type of memory to associate dynamically allocated * memory with. * * Returns: * DCE2_Stack * * Pointer to a new stack object. * NULL if unable to allocate memory for the object. * ********************************************************************/DCE2_Stack * DCE2_StackNew(DCE2_StackDataFree df, DCE2_MemType mtype){ DCE2_Stack *stack; stack = (DCE2_Stack *)DCE2_Alloc(sizeof(DCE2_Stack), mtype); if (stack == NULL) return NULL; stack->data_free = df; stack->mtype = mtype; return stack;}/******************************************************************** * Function: DCE2_StackPush() * * Inserts data onto the stack. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * void * * Pointer to the data to insert onto the stack. * * Returns: * DCE2_Ret * DCE2_RET__ERROR if memory cannot be allocated for a new * stack node or the stack object passed in is NULL. * DCE2_RET__SUCCESS if the data is successfully added to * the stack. * ********************************************************************/DCE2_Ret DCE2_StackPush(DCE2_Stack *stack, void *data){ DCE2_StackNode *n; if (stack == NULL) return DCE2_RET__ERROR; n = (DCE2_StackNode *)DCE2_Alloc(sizeof(DCE2_StackNode), stack->mtype); if (n == NULL) return DCE2_RET__ERROR; n->data = data; if (stack->tail == NULL) { stack->tail = stack->head = n; n->prev = NULL; n->next = NULL; } else { stack->tail->next = n; n->prev = stack->tail; stack->tail = n; } stack->num_nodes++; return DCE2_RET__SUCCESS;}/******************************************************************** * Function: DCE2_StackPop() * * Removes and returns the data in the last node in the stack. * Note that the user will have to free the data returned. The * data free function only applies to data that is in the stack * when it is emptied or destroyed. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: * void * * The data in the last node in the stack. * NULL if there are no items in the stack or the stack object * passed in is NULL. * ********************************************************************/void * DCE2_StackPop(DCE2_Stack *stack){ DCE2_StackNode *n; if (stack == NULL) return NULL; n = stack->tail; if (n != NULL) { void *data = n->data; stack->tail = stack->tail->prev; if (stack->tail == NULL) stack->head = NULL; DCE2_Free((void *)n, sizeof(DCE2_StackNode), stack->mtype); stack->num_nodes--; return data; } return NULL;}/******************************************************************** * Function: DCE2_StackFirst() * * Returns a pointer to the data of the first node in the stack. * Sets a current pointer to the first node in the stack for * iterating over the stack. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: * void * * The data in the first node in the stack. * NULL if the stack object passed in is NULL, or there are * no items in the stack. * ********************************************************************/void * DCE2_StackFirst(DCE2_Stack *stack){ if (stack == NULL) return NULL; stack->current = stack->head; if (stack->current != NULL) return stack->current->data; return NULL;}/******************************************************************** * Function: DCE2_StackNext() * * Increments the current pointer in the stack to the next node in * the stack and returns the data associated with it. This in * combination with DCE2_StackFirst is useful in a for loop to * iterate over the items in a stack. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: * void * * The data in the next node in the stack. * NULL if the stack object passed in is NULL, or we are at * the end of the stack and there are no next nodes. * ********************************************************************/void * DCE2_StackNext(DCE2_Stack *stack){ if (stack == NULL) return NULL; if (stack->current != NULL) { stack->current = stack->current->next; if (stack->current != NULL) return stack->current->data; } return NULL;}/******************************************************************** * Function: DCE2_StackLast() * * Returns a pointer to the data of the last node in the stack. * Sets a current pointer to the last node in the stack for * iterating over the stack backwards. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: * void * * The data in the last node in the stack. * NULL if the stack object passed in is NULL, or there are * no items in the stack. * ********************************************************************/void * DCE2_StackLast(DCE2_Stack *stack){ if (stack == NULL) return NULL; stack->current = stack->tail; if (stack->current != NULL) return stack->current->data; return NULL;}/******************************************************************** * Function: DCE2_StackPrev() * * Puts the current pointer in the stack to the previous node in * the stack and returns the data associated with it. This in * combination with DCE2_StackLast is useful in a for loop to * iterate over the items in a stack in backwards order. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: * void * * The data in the previous node in the stack. * NULL if the stack object passed in is NULL, or we are at * the beginning of the stack and there are no previous nodes. * ********************************************************************/void * DCE2_StackPrev(DCE2_Stack *stack){ if (stack == NULL) return NULL; if (stack->current != NULL) { stack->current = stack->current->prev; if (stack->current != NULL) return stack->current->data; } return NULL;}/******************************************************************** * Function: DCE2_StackEmpty() * * Removes all of the nodes in a stack. Does not delete the stack * object itself. Calls data free function for data if it is * not NULL. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: None * ********************************************************************/void DCE2_StackEmpty(DCE2_Stack *stack){ DCE2_StackNode *n; if (stack == NULL) return; n = stack->head; while (n != NULL) { DCE2_StackNode *tmp = n->next; if (stack->data_free != NULL) stack->data_free(n->data); DCE2_Free((void *)n, sizeof(DCE2_StackNode), stack->mtype); n = tmp; } stack->head = stack->tail = stack->current = NULL; stack->num_nodes = 0;}/******************************************************************** * Function: DCE2_StackDestroy() * * Destroys the stack object and all of the data associated with it. * * Arguments: * DCE2_Stack * * A pointer to the stack object. * * Returns: None * ********************************************************************/void DCE2_StackDestroy(DCE2_Stack *stack){ if (stack == NULL) return; DCE2_StackEmpty(stack); DCE2_Free((void *)stack, sizeof(DCE2_Stack), stack->mtype);}/******************************************************************** * Function: DCE2_CQueueNew() * * Creates and initializes a new circular queue object. The * circular queue uses a fixed size array and uses indexes to * indicate the start and end of the queue. This type of * queue can become full since it is a fixed size. Used for * performance reasons since new nodes do not need to be * allocated on the fly. * * Arguments: * int * The size that should be allocated for the circular * queue storage. * DCE2_CQueueDataFree * An optional free function for the data inserted into * the queue. If NULL is passed in, the user will be * responsible for freeing data left in the queue. * DCE2_MemType * The type of memory to associate dynamically allocated * memory with. * * Returns: * DCE2_CQueue * * Pointer to a new queue object. * NULL if unable to allocate memory for the object or the * array for storage. * ********************************************************************/DCE2_CQueue * DCE2_CQueueNew(int size, DCE2_CQueueDataFree df, DCE2_MemType mtype){ DCE2_CQueue *cqueue; if (size <= 0) return NULL; cqueue = (DCE2_CQueue *)DCE2_Alloc(sizeof(DCE2_CQueue), mtype); if (cqueue == NULL) return NULL; cqueue->data_free = df; cqueue->mtype = mtype; cqueue->queue = DCE2_Alloc(size * sizeof(void *), mtype); if (cqueue->queue == NULL) { DCE2_Free(cqueue, sizeof(DCE2_CQueue), mtype); return NULL; } cqueue->size = size; cqueue->head_idx = 0; cqueue->tail_idx = DCE2_SENTINEL; cqueue->cur_idx = DCE2_SENTINEL; return cqueue;}/******************************************************************** * Function: DCE2_CQueueEnqueue() * * Inserts data into the circular queue. * * Arguments: * DCE2_CQueue * * A pointer to the queue object.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -