带有标志位的循环队列

#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10

typedef struct
{
    
    
	int data[MAXSIZE];
	int tag;
	int front;
	int rear;
}sqQueue;

int InitQueue(sqQueue *q)
{
    
    
	q->front = 0;
	q->rear = 0;
	q->tag = 0;
}

int EnQueue(sqQueue *q, int e)//进队
{
    
    
	if (q->tag)
		return 0;
	else
	{
    
    
		q->data[q->rear] = e;
		q->rear = (q->rear + 1) % MAXSIZE;//指向下一位置
		if (q->rear==q->front)
		{
    
    
			q->tag = 1;
		}
		return 1;
	}

}

int DeQueue(sqQueue *q, int *e)
{
    
    
	if (q->front==q->rear&&q->tag==0)
	{
    
    
		return 0;
	}
	else
	{
    
    
		*e = q->data[q->front];
		q->front = (q->front + 1) % MAXSIZE;//指向下一位置
		if (q->rear == q->front)
		{
    
    
			q->tag = 0;
		}
		return 1;

	}

}
int main()
{
    
    
	sqQueue Q;
	InitQueue(&Q);
	EnQueue(&Q, 1);
	EnQueue(&Q, 1);
	printf("%d \n", Q.data[0]);
}

猜你喜欢

转载自blog.csdn.net/qq_37422050/article/details/105475252
今日推荐