| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1545 人关注过本帖, 1 人收藏
标题:这道题好难,根本不知道什么意思,请各位大佬帮忙解一下
只看楼主 加入收藏
second、
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2020-3-22
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:6 
这道题好难,根本不知道什么意思,请各位大佬帮忙解一下
参考stack,设计一个类,能够实现对环形队列的读写。其中包括对队列类型、队列总长度的定义、初始化、为空判断、为满判断、读取、写入、获取队列有效内容长度等。
搜索更多相关主题的帖子: 队列 判断 读取 长度 获取 
2020-03-22 10:18
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:20 
参考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;
};

2020-03-22 12:37
second、
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2020-3-22
收藏
得分:0 
回复 2楼 rjsp
太谢谢啦,其实我根本不知道什么队列,老师就给了我们一个PPT,里面是关于类和对象的,让我们两天内学完还要写程序,我实在是没办法了
2020-03-22 13:36
second、
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2020-3-22
收藏
得分:0 
回复 2楼 rjsp这是老师给的参考答案
#include <iostream>
using namespace std;
template <class T>
class cycleQueue
{
    private:
        unsigned int m_size;
        int m_front;
        int m_rear;
        T*  m_data;
    public:
        cycleQueue(unsigned size)
            :m_size(size),
            m_front(0),
            m_rear(0)
        {   
            m_data = new T[size];
        }   

        ~cycleQueue()
        {   
            delete [] m_data;
        }   

        bool isEmpty()
        {   
            return m_front == m_rear;
        }   

        bool isFull()
        {   
            return m_front == (m_rear + 1)%m_size;
        }   
        void push(T ele)throw(bad_exception)
        {
            if(isFull())
            {
                throw bad_exception();
            }
            m_data[m_rear] = ele;
            m_rear = (m_rear + 1)%m_size;
        }
        T pop() throw(bad_exception)
        {
            if(isEmpty())
            {
                throw bad_exception();
            }
            T tmp = m_data[m_front];
            m_front = (m_front + 1)%m_size;
            return tmp;
        }
};
int main()
{
    cycleQueue<int> q(5);
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    for (int i = 0; i < 4 ; i++)
        cout << q.pop() << endl;
    q.push(5);
    q.push(5);
    q.push(5);
    cout << q.pop() << endl;
    cout << q.pop() << endl;
    cout << q.pop() << endl;
    cout << q.pop() << endl;
    return 0;
}
2020-03-23 08:34
lin5161678
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:45
帖 子:1136
专家分:3729
注 册:2011-12-3
收藏
得分:0 
回复 2楼 rjsp
程序代码:
    void pop_front( const T& value )
    {
        _head = (_head+1)%N;
        --_size;
    }

参数看起来是多余的
收到的鲜花
  • rjsp2020-03-23 12:01 送鲜花  5朵   附言:是,复制上行后忘了改

https://zh.
2020-03-23 11:22
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9008
专家分:53957
注 册:2011-1-18
收藏
得分:0 
回复 4楼 second、
程序代码:
#include <cstddef>
#include <stdexcept>
#include <cassert>

template<typename T, size_t N>
class foo
{
public:
    foo() : _head(), _size()
    {
    }
    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 noexcept
    {
        assert( index < N );
        return ((const T*)_data)[ (_head+index)%N ];
    }
    T& operator[]( size_t index ) noexcept
    {
        assert( index < N );
        return ((T*)_data)[ (_head+index)%N ];
    }
    const T& at( size_t index ) const noexcept
    {
        if( index >= N )
            throw std::out_of_range();
        return operator[](index);
    }
    T& at( size_t index ) noexcept
    {
        if( index >= N )
            throw std::out_of_range();
        return operator[](index);
    }
    const T& front() const noexcept
    {
        assert( !empty() );
        return operator[](0);
    }
    T& front() noexcept
    {
        assert( !empty() );
        return operator[](0);
    }
    const T& back() const noexcept
    {
        assert( !empty() );
        return operator[](_size-1);
    }
    T& back() noexcept
    {
        assert( !empty() );
        return operator[](_size-1);
    }

    void clear()
    {
        for( size_t index=0; index!=_size; ++index )
            operator[](_size-1-index).~T();
        _head = 0;
        _size = 0;
    }
    void push_front( const T& value )
    {
        assert( !full() );
        _head = (_head+N-1)%N;
        ++_size;
        new (&front())T( value );
    }
    void push_back( const T& value )
    {
        assert( !full() );
        ++_size;
        new (&back())T( value );
    }
    void pop_front()
    {
        assert( !empty() );
        front().~T();
        _head = (_head+1)%N;
        --_size;
    }
    void pop_back()
    {
        assert( !empty() );
        back().~T();
        --_size;
    }

private:
    char _data[N*sizeof(T)];
    size_t _head;
    size_t _size;
};

#include <iostream>
using namespace std;

int main( void )
{
    auto prtall = []( const auto& f ) {
        cout << '[';
        for( size_t i=0; i!=f.size(); ++i )
            cout << ' ' << f[i];
        cout << " ]";
        if( f.empty() )
            cout << " empty";
        if( f.full() )
            cout << " full";
        if( !f.empty() )
            cout << " front=" << f.front() << " back=" << f.back();
        cout << endl;
    };

    foo<int,3> a;
    prtall( a );

    a.push_back( 2 );
    prtall( a );

    a.push_front( 1 );
    prtall( a );

    a.push_back( 3 );
    prtall( a );

    a.pop_back();
    prtall( a );

    a.pop_front();
    prtall( a );

    a.clear();
    prtall( a );
}
2020-03-23 14:12
second、
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2020-3-22
收藏
得分:0 
回复 6楼 rjsp
对不起哈,看不懂,我还没学这种,只学了点C,现在在学C++
2020-03-23 19:38
快速回复:这道题好难,根本不知道什么意思,请各位大佬帮忙解一下
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030289 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved