算法导论——队列

头文件queue.h如下:

#pragma once
#ifndef _QUEUE_H
#define _QUEUE_H
#define SIZE 100
class my_queue
{
private:
	using pos = char;
	typedef size_t poa;
public:
	void initqueue(my_queue &q);
	bool queueempty(my_queue &q);
	int enterqueue(my_queue &q, const pos &c);
	int deletequeue(my_queue &q, pos &c);
	int gethead(my_queue &q, pos &c) const;
	void clearqueue(my_queue &q);
	pos queue[SIZE] = { 0 };
	poa head=0;
	poa tail=0;
};

inline void my_queue::initqueue(my_queue &q)  //
{
	this->head = 0;
	this->tail = 0;
}

bool my_queue::queueempty(my_queue &q)
{
	if (this->head == this->tail)
		return 1;
	else
		return 0;
}

int my_queue::enterqueue(my_queue &q, const pos &c)
{
	if (this->tail >= SIZE-1)
		return 0;
	else
		this->queue[this->tail++] = c;
	return 1;
}

int my_queue::deletequeue(my_queue &q, pos &c)
{
	if (this->head==this->tail)
		return 0;
	else
		c = this->queue[this->head++];
	return 1;
}

int my_queue::gethead(my_queue &q, pos &c) const
{
	if (this->head <= 0)
		return 0;
	else
		c = this->queue[this->head];
	return 1;
}

void my_queue::clearqueue(my_queue &q)
{
	this->head = 0;
	this->tail = 0;
}
#endif

main函数如下:

#include<iostream>
#include"queue.h"

using namespace std;


int main()
{
	my_queue q1;
	int length = 8;
	char temp=0;
	char str[] = "abcdefgh";
	q1.initqueue(q1);
	for (size_t i = 0; i < length; ++i)
		q1.enterqueue(q1, str[i]);
	q1.deletequeue(q1, temp);
	cout << "出队列的元素是:" << temp << endl;
	cout << "队列中的元素是:" << "   ";
	if (q1.queueempty(q1))
		return -1;
	else
	{
		for (size_t i = q1.head; i < q1.tail; ++i)
			cout << q1.queue[i] << "   ";
		cout << endl;
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值