loop_queue.c

来自「操作系统课程设计」· C语言 代码 · 共 71 行

C
71
字号
#include "loop_queue.h"

#include <stdio.h>
#include <stdlib.h>

PLOOP_QUEUE CreateLQueue()
{
	PLOOP_QUEUE queue = (PLOOP_QUEUE)malloc(sizeof(LOOP_QUEUE));
	if(NULL == queue)
	{
		printf("memory overflow!\n");
		exit(1);
	}

	queue->front = queue->rear = 0;
	
	return queue;
}

BOOL DestroyLQueue(PLOOP_QUEUE * queue)
{
	if(NULL == *queue)
		return FALSE;
	else
	{
		free(*queue);
		*queue = NULL;
		return TRUE;
	}
}

BOOL EnLQueue(PLOOP_QUEUE queue, QUEUE_ELEMENT_TYPE elem)
{
	if((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front)
		return FALSE;

	queue->data[queue->rear] = elem;
	queue->rear = (queue->rear + 1) % MAX_QUEUE_SIZE;
	return TRUE;
}

BOOL DeLQueue(PLOOP_QUEUE queue, QUEUE_ELEMENT_TYPE * elem)
{
	if(queue->front == queue->rear)
		return FALSE;

	*elem = queue->data[queue->front];
	queue->front = (queue->front + 1) % MAX_QUEUE_SIZE;
	return TRUE;
}

BOOL LQueueEmpty(PLOOP_QUEUE queue)
{
	if(queue->front == queue->rear)
		return TRUE;
	else
		return FALSE;
}

BOOL LQueueFull(PLOOP_QUEUE queue)
{
	if((queue->rear + 1) % MAX_QUEUE_SIZE == queue->front)
		return TRUE;
	else
		return FALSE;
}

SIZE_T LQueueLength(PLOOP_QUEUE queue)
{
	return (SIZE_T)((queue->rear - queue->front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE);
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?