队列(Queue ADT)C语言实现

q->size来判断队列满或者空

#ifndef _Queue_h

struct QueueRecord;
typedef struct QueueRecord *Queue;

int IsEmpty(Queue q);
int IsFull(Queue q);
Queue CreateQueue(int MaxElements);
void DisposeQueue(Queue q);
void MakeEmpty(Queue q);
void Enqueque(int x,Queue q);
int Front(Queue q);
void Dequeue(Queue q);
int FrontAndDequeue(Queue q);

#endif

#define MinQueueSize 5

struct QueueRecord{
	int capacity;
	int front;
	int rear;
	int size;
	int *a;
};

int IsEmpty(Queue q){
	return q->size == 0;
}

int IsFull(Queue q){
	return q->size == q->capacity;
}

Queue CreateQueue(int MaxElements){
	Queue q;
	if(MaxElements<MinQueueSize){
		printf("The queue should be large\n");
		return NULL;
	}
	q = (Queue)malloc(sizeof(struct QueueRecord));
	if(q == NULL){
		printf("Out of Space\n");
	}
	q->a = (int *)malloc(sizeof(int)*MaxElements);
	if(q->a == NULL){
		printf("Out of Space\n");
		return NULL;
	}
	q->capacity = MaxElements;
	q->size = 0;
	q->front = 1;
	q->rear = 0;
	return q;
}

void DisposeQueue(Queue q){
	free(q->a);
	free(q);
}

void MakeEmpty(Queue q){
	q->size = 0;
	q->front = 1;
	q->rear = 0;
}

void Enqueque(int x,Queue q){
	if(IsFull(q)){
		printf("The queue is full\n");
		return;
	}
	q->size++;
	q->rear++;
	if(q->rear == q->capacity)
		q->rear = 0;
	q->a[q->rear] = x;
}

int Front(Queue q){
	if(IsEmpty(q)){
		printf("The queue is empty\n");
		return -1;
	}
	return q->a[q->front];
}

void Dequeue(Queue q){
	if(IsEmpty(q)){
		printf("The queue is empty\n");
		return ;
	}
	q->size--;
	q->front++;
	if(q->front == q->capacity)
		q->front = 0;
}

int FrontAndDequeue(Queue q){
	int n;
	if(IsEmpty(q)){
		printf("The queue is empty\n");
		return -1;
	}
	n = q->[q->front];
	q->size--;
	q->front--;
	if(q->front<0)
		q->front = q->capacity - 1;
	return n;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值