C语言循环队列

stack.h

#define N 5
typedef struct Queue_s {
	int data[N];
	int front;
	int rear;
} Queue_t, *pQueue_t;

void init(pQueue_t pQueue);
int isFull(pQueue_t pQueue);
void enQueue(pQueue_t pQueue, int num);
int isEmpty(pQueue_t pQueue);
int deQueue(pQueue_t pQueue);

main.c

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include "queue.h"
int main() {
	Queue_t queue;
	init(&queue);
	for (int i = 0; i < 5; ++i) {
		printf("enQueue, i = %d\n", i);
		enQueue(&queue, i);
	}
	for (int i = 0; i < 6; ++i) {
		printf("deQueue, out = %d\n", deQueue(&queue));
	}
}
void init(pQueue_t pQueue) {
	memset(pQueue, 0, sizeof(Queue_t));
}
int isFull(pQueue_t pQueue) {
	return (pQueue->rear + 1) % N == pQueue->front;
}
void enQueue(pQueue_t pQueue, int num) {
	if (isFull(pQueue)) {
		fprintf(stderr, "Queue is full!\n");
		return;
	}
	pQueue->data[pQueue->rear] = num;
	pQueue->rear = (pQueue->rear + 1) % N;
}
int isEmpty(pQueue_t pQueue) {
	return pQueue->rear == pQueue->front;
}
int deQueue(pQueue_t pQueue) {
	if (isEmpty(pQueue)) {
		fprintf(stderr, "Queue is empty!\n");
		return -1;
	}
	int ret = pQueue->data[pQueue->front];
	pQueue->front = (pQueue->front + 1) % N;
	return ret;
}

你可能感兴趣的:(C++,c语言,算法,开发语言)