📄 dce2_list.c
字号:
list->current = list->tail; if (list->current != NULL) return list->current->data; return NULL;}/******************************************************************** * Function: DCE2_ListPrev() * * Puts the current pointer in the list to the previous node in * the list and returns the data associated with it. This in * combination with DCE2_ListLast is useful in a for loop to * iterate over the items in a list in backwards order. * * Arguments: * DCE2_List * * A pointer to the list object. * * Returns: * void * * The data in the previous node in the list. * NULL if the list object passed in is NULL, or we are at * the beginning of the list and there are no previous nodes. * ********************************************************************/void * DCE2_ListPrev(DCE2_List *list){ if (list == NULL) return NULL; if (list->prev != NULL) { list->current = list->prev; list->prev = NULL; return list->current->data; } else if (list->current != NULL) { list->current = list->current->prev; if (list->current != NULL) return list->current->data; } return NULL;}/******************************************************************** * Function: DCE2_ListRemoveCurrent() * * Removes the current node pointed to in the list. This is set * when a call to DCE2_ListFirst or DCE2_ListNext is called. For * either of these if data is returned and the user want to remove * that data from the list, this function should be called. * Sets a next pointer, so a next call to DCE2_ListNext will point * to the node after the deleted one. * * Arguments: * DCE2_List * * A pointer to the list object. * * Returns: None * ********************************************************************/void DCE2_ListRemoveCurrent(DCE2_List *list){ if (list == NULL) return; if (list->current == NULL) return; list->next = list->current->next; list->prev = list->current->prev; if (list->current == list->head) list->head = list->current->next; if (list->current == list->tail) list->tail = list->current->prev; if (list->current->prev != NULL) list->current->prev->next = list->current->next; if (list->current->next != NULL) list->current->next->prev = list->current->prev; if (list->key_free != NULL) list->key_free(list->current->key); if (list->data_free != NULL) list->data_free(list->current->data); DCE2_Free((void *)list->current, sizeof(DCE2_ListNode), list->mtype); list->current = NULL; list->num_nodes--;}/******************************************************************** * Function: DCE2_ListEmpty() * * Removes all of the nodes in a list. Does not delete the list * object itself. Calls data free and key free functions for * data and key if they are not NULL. * * Arguments: * DCE2_List * * A pointer to the list object. * * Returns: None * ********************************************************************/void DCE2_ListEmpty(DCE2_List *list){ DCE2_ListNode *n; if (list == NULL) return; n = list->head; while (n != NULL) { DCE2_ListNode *tmp = n->next; if (list->data_free != NULL) list->data_free(n->data); if (list->key_free != NULL) list->key_free(n->key); DCE2_Free((void *)n, sizeof(DCE2_ListNode), list->mtype); n = tmp; } list->head = list->tail = list->current = NULL; list->num_nodes = 0;}/******************************************************************** * Function: DCE2_ListDestroy() * * Destroys the list object and all of the data associated with it. * * Arguments: * DCE2_List * * A pointer to the list object. * * Returns: None * ********************************************************************/void DCE2_ListDestroy(DCE2_List *list){ if (list == NULL) return; DCE2_ListEmpty(list); DCE2_Free(list, sizeof(DCE2_List), list->mtype);}/******************************************************************** * Function: DCE2_ListInsertTail() * * Private function for inserting a node at the end of the list. * * Arguments: * DCE2_List * * A pointer to the list object. * DCE2_ListNode * * A pointer to the list node to insert. * * Returns: None * ********************************************************************/static void DCE2_ListInsertTail(DCE2_List *list, DCE2_ListNode *n){ if ((list == NULL) || (n == NULL)) { DCE2_Log("%s(%d) => List and/or list node passed in was NULL\n", __FILE__, __LINE__); return; } if (list->tail == NULL) { list->tail = list->head = n; n->prev = n->next = NULL; } else { n->prev = list->tail; n->next = NULL; list->tail->next = n; list->tail = n; } list->num_nodes++;}/******************************************************************** * Function: DCE2_ListInsertHead() * * Private function for inserting a node at the front of the list. * * Arguments: * DCE2_List * * A pointer to the list object. * DCE2_ListNode * * A pointer to the list node to insert. * * Returns: None * ********************************************************************/static void DCE2_ListInsertHead(DCE2_List *list, DCE2_ListNode *n){ if ((list == NULL) || (n == NULL)) { DCE2_Log("%s(%d) => List and/or list node passed in was NULL\n", __FILE__, __LINE__); return; } if (list->head == NULL) { list->head = list->tail = n; n->prev = n->next = NULL; } else { n->prev = NULL; n->next = list->head; list->head->prev = n; list->head = n; } list->num_nodes++;}/******************************************************************** * Function: DCE2_ListInsertBefore() * * Private function for inserting a node before a given node in * the list. * * Arguments: * DCE2_List * * A pointer to the list object. * DCE2_ListNode * * A pointer to the list node to insert. * DCE2_ListNode * * A pointer to the list node to insert this node before. * * Returns: None * ********************************************************************/static void DCE2_ListInsertBefore(DCE2_List *list, DCE2_ListNode *insert, DCE2_ListNode *front){ if ((list == NULL) || (insert == NULL) || (front == NULL)) { DCE2_Log("%s(%d) => List, insert node and/or front node passed in was NULL\n", __FILE__, __LINE__); return; } if (front == list->head) { DCE2_ListInsertHead(list, insert); } else { insert->prev = front->prev; insert->next = front; front->prev->next = insert; front->prev = insert; list->num_nodes++; }}/******************************************************************** * Function: DCE2_QueueNew() * * Creates and initializes a new queue object. * * Arguments: * DCE2_QueueDataFree * 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_Queue * * Pointer to a new queue object. * NULL if unable to allocate memory for the object. * ********************************************************************/DCE2_Queue * DCE2_QueueNew(DCE2_QueueDataFree df, DCE2_MemType mtype){ DCE2_Queue *queue; queue = (DCE2_Queue *)DCE2_Alloc(sizeof(DCE2_Queue), mtype); if (queue == NULL) return NULL; queue->data_free = df; queue->mtype = mtype; return queue;}/******************************************************************** * Function: DCE2_QueueEnqueue() * * Inserts data into the queue. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * void * * Pointer to the data to insert into the queue. * * Returns: * DCE2_Ret * DCE2_RET__ERROR if memory cannot be allocated for a new * queue node or the queue object passed in is NULL. * DCE2_RET__SUCCESS if the data is successfully added to * the queue. * ********************************************************************/DCE2_Ret DCE2_QueueEnqueue(DCE2_Queue *queue, void *data){ DCE2_QueueNode *n; if (queue == NULL) return DCE2_RET__ERROR; n = (DCE2_QueueNode *)DCE2_Alloc(sizeof(DCE2_QueueNode), queue->mtype); if (n == NULL) return DCE2_RET__ERROR; n->data = data; if (queue->tail == NULL) { queue->head = queue->tail = n; n->next = NULL; } else { queue->tail->next = n; n->prev = queue->tail; queue->tail = n; } queue->num_nodes++; return DCE2_RET__SUCCESS;}/******************************************************************** * Function: DCE2_QueueDequeue() * * Removes and returns the data in the first node in the queue. * Note that the user will have to free the data returned. The * data free function only applies to data that is in the queue * when it is emptied or destroyed. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: * void * * The data in the first node in the queue. * NULL if there are no items in the queue or the queue object * passed in is NULL. * ********************************************************************/void * DCE2_QueueDequeue(DCE2_Queue *queue){ DCE2_QueueNode *n; if (queue == NULL) return NULL; n = queue->head; if (n != NULL) { void *data = n->data; if (queue->head == queue->tail) { queue->head = queue->tail = NULL; } else { queue->head->next->prev = NULL; queue->head = queue->head->next; } DCE2_Free((void *)n, sizeof(DCE2_QueueNode), queue->mtype); queue->num_nodes--; return data; } return NULL;}/******************************************************************** * Function: DCE2_QueueFirst() * * Returns a pointer to the data of the first node in the queue. * Sets a current pointer to the first node in the queue for * iterating over the queue. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: * void * * The data in the first node in the queue. * NULL if the queue object passed in is NULL, or there are * no items in the queue. * ********************************************************************/void * DCE2_QueueFirst(DCE2_Queue *queue){ if (queue == NULL) return NULL; queue->current = queue->head; if (queue->current != NULL) return queue->current->data; return NULL;}/******************************************************************** * Function: DCE2_QueueNext() * * Increments the current pointer in the queue to the next node in * the queue and returns the data associated with it. This in * combination with DCE2_QueueFirst is useful in a for loop to * iterate over the items in a queue. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: * void * * The data in the next node in the queue. * NULL if the queue object passed in is NULL, or we are at * the end of the queue and there are no next nodes. * ********************************************************************/void * DCE2_QueueNext(DCE2_Queue *queue){ if (queue == NULL) return NULL; if (queue->current != NULL) { queue->current = queue->current->next; if (queue->current != NULL) return queue->current->data; } return NULL;}/******************************************************************** * Function: DCE2_QueueLast() * * Returns a pointer to the data of the last node in the queue. * Sets a current pointer to the last node in the queue for * iterating over the queue backwards. * * Arguments: * DCE2_Queue * * A pointer to the queue object. * * Returns: * void * * The data in the last node in the queue. * NULL if the queue object passed in is NULL, or there are * no items in the queue. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -