码迷,mamicode.com
首页 > 其他好文 > 详细

实现特殊的栈和队列

时间:2019-06-10 18:34:46      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:queue   obj   stack   EDA   null   help   its   实现   tar   

利用一个固定数组实现栈和队列

class ArrayStack //用数组实现栈
{
public:
    ArrayStack(int initsize) //创建一个固定长度的数组
    {
        if (initsize < 0)
        {
            cout << "数组长度小于0" << endl;
        }
        else
        {
            array = vector<int>(initsize - 1);
            size = 0;
        }
    }

    void push(int obj)
    {
        if (size == array.size())
            cout << "栈已满" << endl;
        else
            array[size++] = obj;
    }

    int pop()
    {
        if (size == 0)
            cout << "栈为空" << endl;
        else
            return array[--size];

    }

    int top()
    {
        if (size == 0)
            return NULL;
        else
            return array[size - 1];
    }

private:
    vector<int> array;
    int size;
};

class ArrayQueue //用数组实现队列
{
public:
    ArrayQueue(int initsize)
    {
        if (initsize < 0)
            cout << "数组长度小于0" << endl;
        else
        {
            vector<int> array(initsize-1);
            size = 0;
            start = 0;
            end = 0;
        }
    }
   
    void push(int obj)
    {
        if (size == array.size())
            cout << "队列已满" << endl;
        else
        {
            array[end++] = obj;
            size++;
            if (end == array.size()) //当队尾到达数组的最后一个位置时,队尾指针转而指向数组的第一个位置
                end = 0;
        }
    }

    int pop()
    {
        if (size == 0)
            cout << "队列为空" << endl;
        else
        {
            size--;
            int tmp = start;
            if (start == array.size() - 1) //当队首到达数组的最后一个元素时,经过pop操作,指向数组的第一个元素
                start = 0;
            else
                start++;
            return array[tmp];
        }
    }
    
    int top()
    {
        if(size == 0)
            return NULL;
        else
            return array[start];
    }

private:
    vector<int> array;
    int size;
    int start; //指向队首
    int end;  //指向队尾元素的后一个位置,该位置为空
};

具有返回min功能的栈

使用两个栈,一个为data栈,一个为min栈。在进行push操作时,首先向data栈中push值,如果push的值小于min栈的栈顶,则向min栈中push这个值,如果大于等于min栈的栈顶,则再push一个min栈栈顶的值。在进行pop操作时,data栈和min栈都对栈顶进行pop。进行top操作,即对data栈取top,进行min操作,即对min栈取top。

class MinStack
{
public:
    void push(int value) 
    {
        stackdata.push(value);
        if (stackmin.empty() || value<stackmin.top()) //压入元素小于min栈顶,则压入该元素;否则再压入一个栈顶元素。
            stackmin.push(value);
        else stackmin.push(stackmin.top());
    }
    
    void pop() 
    {
        if (!stackmin.empty())
        {
            stackmin.pop();
            stackdata.pop();
        }
        else
            cout << "栈为空" << endl;
    }
    
    int top() 
    {
        return stackdata.top();
    }

    int min() 
    {
        return stackmin.top();
    }

private:
    stack<int> stackmin;
    stack<int> stackdata;
};

用两个队列实现栈

使用两个队列,一个为data队列,另一个为help队列。在进行push操作时,向data队列中push值。在进行pop操作时,不断地将data队列的首元素弹出并且压入help队列中,直到data队列中只剩下一个元素,将该元素pop,交换data队列和help队列。在进行top操作时,与pop操作不同的是,不仅要返回data队列中的最后一个元素,还要将这个元素压入help栈中。最后交换data队列和help队列。

class QueueStack
{
public:
    void push(int obj)
    {
        queuedata.push(obj);
    }

    void pop()
    {
        if (queuedata.empty())
            cout << "栈为空" << endl;
        else
        {
            while (queuedata.size() > 1)
            {
                queuehelp.push(queuedata.front());
                queuedata.pop();
            }
            queuedata.pop();
            queue<int> tmp = queuehelp;
            queuehelp = queuedata;
            queuedata = tmp;
        }   
    }

    int top()
    {
        if (queuedata.empty())
            return NULL;
        else
        {
            while (queuedata.size() > 1)
            {
                queuehelp.push(queuedata.front());
                queuedata.pop();
            }
            int res = queuedata.front();
            queuedata.pop();
            queuehelp.push(res);
            queue<int> tmp = queuehelp;
            queuehelp = queuedata;
            queuedata = tmp;
            return res;
        }
    }

private:
    queue<int> queuedata;
    queue<int> queuehelp;
};

用两个栈实现队列

使用两个栈,一个为push栈,另一个为pop栈。在进行push操作时,向push栈中push值。在进行pop操作时,如果pop栈不为空,直接在pop栈执行pop操作;在pop栈为空时,则将push栈中的栈顶元素依次弹出并且压入pop栈中,再对pop栈执行pop操作。top操作与pop类似。

class StackQueue
{
public:
    void push(int obj)
    {
        stackpush.push(obj);

    }

    void pop()
    {
        if (stackpop.empty() && stackpush.empty())
            cout << "队列为空" << endl;
        else if (stackpop.empty())
        {
            while (!stackpush.empty())
            {
                stackpop.push(stackpush.top());
                stackpush.pop();
            }
        }
        stackpop.pop();
    }

    int top()
    {
        if (stackpop.empty() && stackpush.empty())
            return NULL;
        else if (stackpop.empty())
        {
            while (!stackpush.empty())
            {
                stackpop.push(stackpush.top());
                stackpush.pop();
            }
        }
        stackpop.top();
    }

private:
    stack<int> stackpush;
    stack<int> stackpop;
};

实现特殊的栈和队列

标签:queue   obj   stack   EDA   null   help   its   实现   tar   

原文地址:https://www.cnblogs.com/gcheeze/p/10999250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!