c队列操作

队列实际应用现场:先入先出
#include "queue.h"
#include "linkedlist.h"
#include <stdlib.h>
#include <stdio.h>

#ifdef DMALLOC
#include "dmalloc.h"
#endif
typedef struct {
    LinkedList *list;
} Queue;
Queue *queue_construct() {

    Queue *queue = NULL;

   	/*
     * Allocate space for the object and set all fields to zero.
     */
    queue = calloc(1, sizeof(Queue));

	if (queue == NULL) {
        return NULL;
    }
		
    /*
     * Instantiate an internal linked list to hold queue's elements.
     */
	queue->list = linked_list_construct();
	
	if (queue->list == NULL) {
        return NULL;
    }
	
    return queue;
}

void queue_destroy(Queue *queue) {

    if (queue == NULL) {
        return;
    }
    
    /*
     * Delete the linked list that holds the elements.
     */
	linked_list_destroy(queue->list);
	queue->list = NULL;
	
    free(queue);
}

void queue_enqueue(Queue *queue, const void *data) {
	
	LinkedListNode *list_node = NULL;
	
	if (queue == NULL) {
        return;
    }
	
	list_node = linked_list_node_construct(data);
	
    /*
     * Add the node to the end of this
     */
	linked_list_append_node(queue->list, list_node);
	
}

const void *queue_dequeue(Queue *queue) {

	LinkedListNode *list_node = NULL;
    const void *data = NULL;
	
	if (queue == NULL) {
        return NULL;
    }
	
	linked_list_seek_start(queue->list);
	
	list_node = linked_list_get_next_node(queue->list);
	
	if (list_node == NULL) {
        return NULL;
    }
		
	data = linked_list_node_get_data(list_node);
	
	linked_list_remove_node(queue->list, list_node);
	
	return data;

}

int queue_is_empty(Queue *queue) {
	
	if (queue == NULL) return 1;
		
	return linked_list_is_empty(queue->list);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值