数据结构算法——1021. 可旋栈

题目

在这里插入图片描述
in
11
1 1
1 2
1 3
1 4
1 5
3
2
2
2
2
2

out
1
2
3
4
5
1
2
3
4
5
-1

思路

单项栈的话实现reverse复杂度为O(n)
用个双向栈,reverse的时候直接把头尾指针互换,然后换个方向复杂度为O(1)大大减少用时

需要注意一下插入删除空斩时候的特殊情况,避免访问越界

代码

#include<iostream>
using namespace std;

struct dot
{
    int data;
    dot* prev = NULL;
    dot* next = NULL;
};

struct stack
{
    dot* head = NULL;
    dot* tail = NULL;
    bool reverse = false;
};

int main()
{
    int z;
    cin >> z;
    stack s;
    for(int i = 0; i < z; i++)
    {
        int c;
        cin >> c;
        switch(c)
        {
        case 1:
            int n;
            cin >> n;
            if(s.reverse)
            {
                if(s.head == NULL)
                {
                    s.head = s.tail = new dot;
                    s.head->data = n;
                    //栈首尾还是得NULL的
                }
                else
                {
                    s.tail->next = new dot;
                    s.tail->next->prev = s.tail;
                    s.tail = s.tail->next;
                    s.tail->data = n;
                }
                cout << s.tail->data << endl;
            }
            else
            {
                if(s.tail == NULL)
                {
                    s.tail = s.head = new dot;
                    s.tail->data = n;
                }
                else
                {
                    s.head->prev = new dot;
                    s.head->prev->next = s.head;
                    s.head = s.head->prev;
                    s.head->data = n;
                }
                cout << s.head->data << endl;
            }
        break;

        case 2:
            if(s.reverse)
            {
                
                if(s.tail)
                {
                    if(s.tail->prev)
                    {
                        dot* temp = s.tail;
                        s.tail->prev->next = s.tail->next;
                        s.tail = s.tail->prev;
                        delete temp;
                        cout << s.tail->data << endl;
                    }
                    else
                    {
                        dot* temp = s.tail;
                        s.tail = s.head = NULL;
                        delete temp;
                        cout << "-1" << endl;
                    }
                    
                }
                else
                    cout << "-1" << endl;
            }
            else
            {
                if(s.head)
                {
                    
                    if(s.head->next)
                    {
                        dot* temp = s.head;
                        s.head->next->prev = s.head->prev;
                        s.head = s.head->next;
                        
                        delete temp;
                        cout << s.head->data << endl;
                    }
                    else
                    {
                        dot*temp = s.head;
                        s.head = s.tail = NULL;
                        delete temp;
                        cout << "-1" << endl;
                    }
                   
                }
                else
                {
                   cout << "-1" << endl;
                }
            }
        break;

        case 3:
            s.reverse = !s.reverse;
            if(s.reverse)
                if(s.tail)
                    cout << s.tail->data << endl;
                else 
                    cout << "-1" << endl;
            else
                if(s.head)
                    cout << s.head->data << endl;
                else 
                    cout << "-1" << endl;
        break; 
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值