队列的实现

1.顺序实现循环队列

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
const int MAX = 20;

class Myqueue
{
private:
	int Queue[MAX] = { 0 };
	int front = 0;
	int rear = 0;
	int size = 0;
public:
	void Inque(int n)
	{
		if (Isfull())
			return;
		else
		{
			size++;
			Queue[rear] = n;
			rear = (rear + 1) % MAX;
		}
	}
	int Deque()
	{
		if (this->Isempty())
			return 0;
		else
		{
			size--;
			int temp = Queue[front];
			Queue[front] = 0;
			front = (front + 1) % MAX;
			return temp;
		}
	}
	bool Isempty()
	{
		if (size == 0)
			return true;
		else
			return false;
	}
	bool Isfull()
	{
		if (size == MAX)
			return true;
		else
			return false;
	}
	int Getsize()
	{
		return size;
	}
	void Print()
	{
		if (Isempty())
		{
			cout << "队列为空!" << endl;
			return;
		}
		else
		{
			int temp = front;
			cout << "当前队列为:";
			if(Isfull())
			{
				cout << Queue[temp] << ' ';
				temp = (temp + 1) % MAX;
			}
			while (temp != rear)
			{
				cout << Queue[temp] << ' ';
				temp = (temp + 1) % MAX;
			}
			cout << endl;

		}
	}
};
int main()
{
	Myqueue test;
	srand(time(0));
	for (int i = 0; i < MAX; i++)
	{
		test.Inque(rand() % 99+1);
		test.Print();
	}
	int num = 0;
	cout << "想要出队几个?:";
	cin >> num;
	for (int i = 0; i < num; i++)
	{
		test.Deque();
		test.Print();
	}
	cout << "填满吗?(y/Y):";
	char ch;
	cin >> ch;
	if (ch == 'y' || ch == 'Y')
	{
		while (!test.Isfull())
		{
			test.Inque(rand() % 99+1);
			test.Print();
		}
	}
	return 0;
}

2.链式实现

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

struct Node
{
	int num;
	Node* next;
};
class Myqueue
{
private:
	Node* head = nullptr;
	Node* rear = nullptr;
	int size = 0;
public:
	~Myqueue()
	{
		while (!Isempty())
			this->Deque();
	}
	void Inque(int n)
	{
		Node* temp = new Node;
		temp->num = n;
		temp->next = nullptr;
		if (Isempty())
		{
			head = temp;
			rear = temp;
			size++;
		}
		else
		{
			rear->next = temp;
			rear = temp;
			size++;
		}
	}
	void Deque()
	{
		if (Isempty())
		{
			cout << "队列已空!" << endl;
			return;
		}
		else if (size == 1)
		{
			Node* temp = head;
			head = nullptr;
			rear = nullptr;
			delete temp;
			size--;
		}
		else
		{
			Node* temp = head;
			head = head->next;
			delete temp;
			size--;
		}
	}
	bool Isempty()
	{
		if (size == 0 && head == nullptr && rear == nullptr)
			return true;
		else
			return false;
	}
	void Print()
	{
		if (Isempty())
		{
			cout << "队列已空!" << endl;
			return;
		}
		else
		{
			cout << "此队列为:";
			Node* temp = head;
			do {
				cout << temp->num << ' ';
				temp = temp->next;
			} while (temp != nullptr);
			cout << endl;
		}
	}
};
int main()
{
	Myqueue one;
	srand(time(0));
	int number = rand() % 10 + 1;
	cout << "进行" << number << "次循环" << endl;
	for (int i = 0; i < number; i++)
	{
		one.Inque(rand() % 99 + 1);
	}
	one.Print();
	int num = 0;
	cout << "出队几次?:";
	cin >> num;
	for (int i = 0; i < num; i++)
		one.Deque();
	one.Print();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值