数据结构与算法B代码编写作业,stack or queue,解题报告&AC代码

这道题有很多机智的解决方法…我这一种基本上是最麻烦的…

麻烦是因为要练练手…好久没写队列和栈了有点生疏= =

思路很简单,模拟出栈和队列,然后看看哪个不符合,就搞定了……

AC代码如下…果然生疏了啊…队列写的有点问题导致RE了很多次,最后又因为把Queue写成Quene结果WA了两次…命途多舛……


#include <iostream>

using namespace std;

template <class T>
class myStack;

template <class T>
class myqueue;

template <class T>
class dataStr;

template <class T>
class dataStr
{
public:
    T data;
    dataStr* next;
    dataStr* forw;
};

template <class T>
class myStack
{
public:
    myStack(int);
    ~myStack();
    T push(T);
    T pop();
private:
    T* data;
    int num;
};

template <class T>
class myqueue
{
public:
    myqueue();
    T push(T);
    T pop();
private:
    int num;
    dataStr<T>* head;
    dataStr<T>* tail;
};

int main()
{
    int t, n;
    int type, val, judge;
    cin >> t;
    for(int i = 0; i < t; i++)
    {
        judge = 0;
        cin >> n;
        myStack<int> stk(4096);
        myqueue<int> que;
        for(int j = 0; j < n; j++)
        {
            cin >> type >> val;
            if(judge != 0)
                continue;
            if(type == 1)
            {
                stk.push(val);
                que.push(val);
            }
            else if(type == 2)
            {
                if(val != stk.pop())
                    judge = 2;
                else if(val != que.pop())
                    judge = 1;
            }
        }
        if(judge == 1)
            cout << "Stack" << endl;
        else if(judge == 2)
            cout << "Queue" << endl;
    }
    return 0;
}

template <class T>
myStack<T>::myStack(int x)
{
    data = new T[x];
    num = 0;
}

template <class T>
myStack<T>::~myStack()
{
    delete data;
}

template <class T>
T myStack<T>::push(T x)
{
    data[num] = x;
    num++;
    return x;
}

template <class T>
T myStack<T>::pop()
{
    if(num == 0)
        return *(new T);
    if(num > 0)
        num--;
    return data[num];
}

template <class T>
myqueue<T>::myqueue()
{
    num = 0;
    head = tail = NULL;
}

template <class T>
T myqueue<T>::push(T x)
{
    if(head == NULL)
    {
        head = new dataStr<T>;
        head -> forw = NULL;
        head -> next = NULL;
        head -> data = x;
    }
    else if(tail == NULL)
    {
        tail = new dataStr<T>;
        tail -> forw = head;
        head -> next = tail;
        tail -> next = NULL;
        tail -> data = x;
    }
    else
    {
        dataStr<T>* temp = new dataStr<T>;
        temp -> data = x;
        temp -> next = NULL;
        temp -> forw = tail;
        tail -> next = temp;
        tail = temp;
    }
    num++;
    return x;
}

template <class T>
T myqueue<T>::pop()
{
    if(num <= 0)
        return *(new T);
    num--;
    if(tail == NULL)
    {
        T temp = head -> data;
        head = NULL;
        return temp;
    }
    else
    {
        T temp = head -> data;
        head = head -> next;
        delete head -> forw;
        head -> forw = NULL;
        return temp;
    }
}

惯例君说:原题在这里

总时间限制:
    1000ms
内存限制:
    65535kB

描述

    栈和队列都是常用的线性结构,它们都提供两个操作:

    Push:加入一个元素。

    Pop:弹出一个元素。

    不同的是,栈是”先进后出”,而队列则是”先进先出”。

    给出一个线性结构的进出顺序,判定这个结构是栈还是队列。

输入
    第一行输入一个整数t,代表有t组测试数据
    对于每组测试数据,第一行输入一个整数n,代表操作的次数。
    随后输入n行,每行包含两个整数 type val。
    当type = 1时,表示该次操作为push操作,val表示进入的数字。当type=2时,表示该次操作为pop操作,val代表出来的数字。
    3<=n<=2000
输出
    每组测试数据输出一行。
    输出改组数据对应的线性结构,”Stack” 或者 “Queue”。
    题目保证是栈或者队列的一种。
样例输入

    2
    6
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    4
    1 1
    1 2
    2 1
    2 2

样例输出

    Stack
    Queue



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值