C++ 数组队列的一种实现方式

Queue.h

#ifndef QUEUE_H
#define QUEUE_H

#include <iostream>
#include <cstring>

#define Queue_MAXSize 10

using namespace std;

class Queue
{
private:
	int data[Queue_MAXSize];
	int front;
	int rear;
public:
	Queue();
	~Queue();

	bool Queue_Init(void);
	int Queue_Length(void);
	int Queue_Front(void);
	int Queue_Rear(void);
	bool Queue_Insert(int data);
	int Queue_Output(void);
	bool Queue_Delete(int data);
	bool Queue_Visit(void);
};

#endif

Queue.c

#include "Queue.h"

Queue::Queue()
{

}

Queue::~Queue()
{

}

bool Queue::Queue_Init()
{
	memset(data, 0, Queue_MAXSize * 4);
	front = 0;
	rear = 0;

	return true;
}

int Queue::Queue_Length()
{
	if ((this->front == this->rear) && this->data[front])
		return Queue_MAXSize;

	return ((this->rear - this->front + Queue_MAXSize) % Queue_MAXSize);
}

int Queue::Queue_Front()
{
	if ((this->front == this->rear) && (this->data[front] == 0))
		return false;

	return this->data[front];
}

int Queue::Queue_Rear()
{
	if ((this->front == this->rear) && (this->data[front] == 0))
		return false;

	return this->data[(rear - 1 + Queue_MAXSize) % Queue_MAXSize];
}

bool Queue::Queue_Insert(int data)
{
	if ((this->front == this->rear) && this->data[front])
		return false;

	this->data[rear] = data;
	this->rear = (this->rear + 1) % Queue_MAXSize;

	return true;
}

int Queue::Queue_Output()
{
	int data = 0;
	if ((this->front == this->rear) && (this->data[front] == 0))
		return false;

	data = this->data[this->front];
	this->data[this->front] = 0;
	this->front = (this->front + 1) % Queue_MAXSize;

	return data;
}

bool Queue::Queue_Delete(int data)
{
	int i = this->front;
	if ((this->front == this->rear) && (this->data[front] == 0))
		return false;
	while (i != this->rear)
	{
		if (this->data[i] == data)
			break;
		i = (i + 1) % Queue_MAXSize;
	}
	if (this->data[i] == data)
	{
		while (i != ((rear - 1 + Queue_MAXSize) % Queue_MAXSize))
		{
			this->data[i] = this->data[(i + 1) % Queue_MAXSize];
			i = (i + 1) % Queue_MAXSize;
		}
		this->data[i] = 0;
		this->rear = (this->rear - 1 + Queue_MAXSize) % Queue_MAXSize;
	}

	return true;
}

bool Queue::Queue_Visit()
{
	int i = this->front;
	if ((this->front == this->rear) && (this->data[front] == 0))
		return false;

	while (i != this->rear)
	{
		cout << "this->data[" << i << "] = " << this->data[i] << endl;
		i = (i + 1) % Queue_MAXSize;
	}

	return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值