Cyclic queue 12

#include<stdio.h>
#include<stdlib.h>
#define SIZE 6
typedef int data;
struct sq
{
	data *Q;
	int front,rear;
};
void begin(struct sq *A);//初始化 
int enquque(struct sq*A,int e);//入队
int dequque(struct sq*A,int *e);//出队 
int gethead(struct sq*A,int *e);//取队头元素 
int ququelength(struct sq*A,int *e);//求队列的长度 
//////////////////////////////////
int main(void)
{
	int i=1,*e;
	e=&i;
	struct sq *A;
	A=(struct sq*)malloc(sizeof(struct sq));
	if(!A)
	return -1;
	begin(A);//初始化申请SIZE大小的int整型空间
	if(!A->Q)
	return -1; 
	if(!enquque(A,*e))//入队 
	return -1;
	*e=2; 
	if(!dequque(A,e))//出队 
	return -1;
	if(!gethead(A,e))//取队头元素 
	return -1;
	if(!ququelength(A,e))//求队列的长度 
	return -1;
	return 0;
}
void begin(struct sq *A)//初始化 
{
	A->Q=(data*)malloc(SIZE*sizeof(data));
}
int  enquque(struct sq*A,int e)//入队 
{
	if(A->rear+1%SIZE==A->front)//队满 
	{
	return 0;	
	}
	*(A->Q+A->rear)=e;
	A->rear=(A->rear+1)%SIZE;
	return 1;
}
int dequque(struct sq*A,int *e)
{
	if(A->front==A->rear)//队空
	return 0;
	*e=*(A->Q+A->front);//保存队头元素
	A->front=(A->front+1)%SIZE;//队头后移一位
	return 1; 	
}
int gethead(struct sq*A,int *e)//取队头元素 
{
	if(A->front==A->rear)
	return 0;
	*e =*(A->Q+A->front); 
	return 1;	
} 
int ququelength(struct sq*A,int *e)//求队列的长度 
{
	*e=(SIZE-A->front+A->rear)%SIZE;
	return 1;	
} 

Published 25 original articles · won praise 0 · Views 84

Guess you like

Origin blog.csdn.net/qq_45812941/article/details/104413538