上篇博客是用顺序表实现队列, 现在用双向带头结点带环链表实现对队列的出队列, 入队列, 取队首元素, 以及销毁队列的相关操作
1.初始化链表
void DLinkQueInit(DLinkQue** q)
{if (q == NULL){return ;}if (*q == NULL){return ;}*q = (DLinkQue*)malloc (sizeof (DLinkQue));(*q) -> next = *q;(*q) -> prev = *q;(*q) -> data = 0 ;
}
2.入队列
void DLinkQuePush(DLinkQue* q, DLinkQueType value)
{if (q == NULL){return ;}DLinkQue* new_node = DLinkQueCreatNewNode(value);DLinkQue* prev = q -> prev;prev -> next = new_node;new_node -> prev = prev;new_node -> next = q;q -> prev = new_node;
}
3. 出队列
void DLinkQuePop(DLinkQue* q)
{if (q == NULL){return ;}if (q -> next == NULL){return ;}if (q -> next == q){return ;}DLinkQue* to_delete = q -> next;DLinkQue* next = to_delete -> next;q -> next = next;next -> prev = q;DLinkQueDestroyNode(to_delete);to_delete = NULL;
}
4. 取队首元素
int DLinkStackGetFront(DLinkStack* stack , DLinkStackType* value)
{if (stack == NULL){return -1 ;}if (stack -> next == stack ){return -1 ;}*value = (stack -> prev) -> data;return 0 ;
}int DLinkStackSize(DLinkStack* stack )
{if (stack == NULL){return 0 ;}if (stack -> next == stack ){return 0 ;}DLinkStack* cur = stack -> next;int size = 0 ;for (; cur != stack ; cur = cur -> next){size++;}return size;
}
5. 销毁队列
void DLinkQueDestroy(DLinkQue** q)
{if (q == NULL){return ;}if (*q == NULL){return ;}int i = DLinkQueSize(*q);DLinkQue* to_delete;DLinkQue* next;for (; i > 0 ; i--){to_delete = (*q) -> next;next = to_delete -> next;(*q) -> next = next;next -> prev = *q;DLinkQueDestroyNode(to_delete);to_delete = NULL;}DLinkQueDestroyNode(*q);*q = NULL;
}