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;
}
C++ 数组队列C++ 数组队列 qq_38705421 发布了15 篇原创文章 · 获赞 0 · 访问量 159 私信 关注
上一篇:在oracle的连接(join)中使用using关键字


下一篇:环形队列