这道题好难,根本不知道什么意思,请各位大佬帮忙解一下
参考stack,设计一个类,能够实现对环形队列的读写。其中包括对队列类型、队列总长度的定义、初始化、为空判断、为满判断、读取、写入、获取队列有效内容长度等。
参考stack,设计一个类,能够实现对环形队列的读写
你这个队列指的是 正常人口中先进先出的那个队列 吗?那为什么是参考stack,而不是参考std::queue?想杀猪不向屠夫学,向快递员学?
当然如果是“队列”的话,“环形”结构是毫无意义的。
那题目中的“队列”就应该是“双端队列”,那也应该是参考 std::deque 呀,碰瓷不到 stack 上去。
以下代码仅供参考,不全、算法也没检查过
程序代码:#include <cstddef>
#include <stdexcept>
template<typename T, size_t N>
class foo
{
public:
foo() = default;
foo( const foo& ) = default;
foo& operator=( const foo& ) = default;
bool empty() const noexcept
{
return _size == 0;
}
bool full() const noexcept
{
return _size == N;
}
size_t size() const noexcept
{
return _size;
}
const T& operator[]( size_t index ) const
{
return _data[ (_head+index)%N ];
}
T& operator[]( size_t index )
{
return _data[ (_head+index)%N ];
}
const T& at( size_t index ) const
{
if( index >= N )
throw std::out_of_range();
return _data[ (_head+index)%N ];
}
T& at( size_t index )
{
if( index >= N )
throw std::out_of_range();
return _data[ (_head+index)%N ];
}
void push_back( const T& value )
{
_data[(_head+_size++)%N] = value;
}
void pop_back( const T& value )
{
--_size;
}
void push_front( const T& value )
{
_head = (_head+N-1)%N;
++_size;
_data[_head] = value;
}
void pop_front( const T& value )
{
_head = (_head+1)%N;
--_size;
}
private:
T _data[N];
size_t _head;
size_t _size;
};








,我还没学这种,只学了点C,现在在学C++