【数据结构】顺序队列的实现(c++)

本文详细介绍了如何使用C++实现顺序队列,包括头文件的引用,主函数的编写,清空队列,获取队头元素,计算队列长度,元素的出队和入队操作,以及程序的退出机制。
摘要由CSDN通过智能技术生成

头文件:


#pragma once

#include <iostream>
#include <assert.h>
using namespace std;

template<class Type>
class SeqQueue
{
public:
	SeqQueue(size_t sz = INIT_SZ);
	~SeqQueue();
public:
	bool empty()const;
	bool full()const;
	void show()const;
	bool push(const Type &x);
	bool pop();
	void gettop(Type &x);
	int length()const;
	void clear();
	void destory();
	void quit_system(Type &x);
private:
	enum{ INIT_SZ = 8 };
	Type *base;
	int capacity;
	int head;
	int tail;
};

template<class Type>
SeqQueue<Type>::SeqQueue(size_t sz = INIT_SZ)
{
	capacity = sz > INIT_SZ ? sz : INIT_SZ;
	base = new Type[capacity];
	assert(base != NULL);
	head = 0;
	tail = 0;
}

template<class Type>
SeqQueue<Type>::~SeqQueue()
{
	destory();
}

// 判断队列是否满了,顺序队列呈现虚满的状态
template<class Type>
bool SeqQueue<Type>::full()const
{
	return (tail >
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值