400 028 6601

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

数据结构(08)_队列和栈的相互实现

1. 栈的队列的相互实现

思考:栈和队列在实现上非常相似,能否用相互实现?

成都创新互联是一家专业提供张家界企业网站建设,专注与成都做网站、成都网站设计、HTML5建站、小程序制作等业务。10年已为张家界众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

1.1. StackToQueue

用栈实现队列等价于用“后进先出”的特性实现“先进先出”的特性.
数据结构(08)_队列和栈的相互实现
实现思路:

template < typename T >
class StackToQueue : public Queue
{
protected:
    mutable LinkStack m_stack_in;
    mutable LinkStack m_stack_out;

    void move() const   //O(n)
    {
        if(m_stack_out.size() == 0)
        {
            while(m_stack_in.size() > 0)
            {
                m_stack_out.push(m_stack_in.top());
                m_stack_in.pop();
            }
        }
    }
public:
    void enqueue(const T& e) //O(1)
    {
        m_stack_in.push(e);
    }

    void dequeue()  //O(n)
    {
        move();

        if(m_stack_out.size() > 0)
        {
            m_stack_out.pop();
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current StackToQueue...");
        }
    }

    T front() const //O(n)
    {
        move();

        if(m_stack_out.size() > 0)
        {
            return m_stack_out.top();
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current StackToQueue...");
        }
    }

    void clear()    // O(n)
    {
        m_stack_in.clear();
        m_stack_out.clear();
    }

    int length() const  //O(n)
    {
        return m_stack_in.size() + m_stack_out.size();
    }
};

评价:
虽然可以使用栈实现队列,但是相比直接使用链表实现队列,在出队和获取对头元素的操作中,时间复杂度都变为了O(n),可以说并不高效。

1.2.QueueToStack

使用队列实现栈,本质上就是使用“先进先出”的特性实现栈“后进先出”的特性。
数据结构(08)_队列和栈的相互实现
实现思路:

template < typename T >
class QueueToStack : public Stack
{
protected:
    LinkQueue m_queue_in;
    LinkQueue m_queue_out;
    LinkQueue* m_qIn;
    LinkQueue* m_qOut;

    void move() const   //O(n)
    {
        while(m_qIn->length()-1 > 0)
        {
            m_qOut->enqueue(m_qIn->front());
            m_qIn->dequeue();
        }
    }

    void swap()     //O(1)
    {
        LinkQueue* temp = NULL;

        temp = m_qIn;
        m_qIn = m_qOut;
        m_qOut = temp;
    }
public:
    QueueToStack()  //O(1)
    {
        m_qIn  = &m_queue_in;
        m_qOut = &m_queue_out;
    }

    void push(const T& e)   //O(n)
    {
        m_qIn->enqueue(e);
    }

    void pop()      //O(n)
    {
        if(m_qIn->length() > 0)
        {
            move();
            m_qIn->dequeue();
            swap();
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current QueueToStack...");
        }
    }

    T top() const       //O(n)
    {
        if(m_qIn->length() > 0)
        {
            move();
            return m_qIn->front();
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "no element in current QueueToStack...");
        }
    }

    void clear()        //O(n)
    {
        m_qIn->clear();
        m_qOut->clear();
    }

    int size() const    //O(1)
    {
        return m_qIn->length() + m_qOut->length();
    }
};

总结评价:
虽然可以使用队列实现栈,但是相比直接使用链表实现栈,入栈、出栈、获取栈顶元素操作中,时间复杂度都变为了O(n),可以说并不高效。


本文标题:数据结构(08)_队列和栈的相互实现
网页路径:http://mzwzsj.com/article/gpgdje.html

其他资讯

让你的专属顾问为你服务