【队列】队列的静态实现 c++代码

#include 
using namespace std;

class Queue
{
public:
	Queue(int size); //初始化队列
	~Queue();
	bool IsEmpty();
	bool IsFull();
	bool Enqueue(double x);
	bool Dequeue(double& x);
	bool DisplayQueue();

private:
	int front;
	int rear;
	int count;
	int maxsize;
	double* values;
};


Queue::Queue(int size)
{
	values = new double[size];
	maxsize = size;
	front = 0;
	rear = -1;
	count = 0;
}

Queue::~Queue()
{
	delete[] values;
}

bool Queue::IsEmpty()
{
	if (this->count)
	{
		return false;
	}
	else
		return true;
}


bool Queue::IsFull()
{
	if (countvalues[rear] = x;
		count++;
		return true;
	}
}

bool Queue::Dequeue(double& x)
{
	if (IsEmpty())
	{
		cout << "队列是空的" << endl;
		return false;
	}
	else
	{
		x = values[front];
		front = (front + 1) % maxsize;
		count--;
		return true;
	}
}

bool Queue::DisplayQueue()
{
	if (IsEmpty())
	{
		cout << "当前队列是空的" << endl;
		return false;
	}

	else
	{
		for (int i = front; i <= this->rear; i++)
		{
			cout << this->values[i] << " ";
			if (i == this->count)
			{
				cout << endl;
			}
		}
	}
}


int main()
{
	Queue my_queue(100);
	for (int i = 0; i < 5; i++)
	{
		my_queue.Enqueue(i);
	}
	my_queue.DisplayQueue();
	my_queue.Enqueue(6);
	double x = 0;
	my_queue.Dequeue(x);
	cout << "出队列的是 " << x << " " << endl;
	my_queue.DisplayQueue();
	my_queue.Enqueue(6);
	my_queue.DisplayQueue();
	return 0;
}

你可能感兴趣的:(算法与数据结构,c++,算法)