zoukankan      html  css  js  c++  java
  • 栈类模板设计及应用

    题目描述

    模拟STL stack类模板设计实现你的stack类模板,该类需具有入栈,出栈,判栈空,取栈顶元素等功能,并能拷贝构造和赋值;利用该类实现本题要求。本题可以使用STL  string类,不可用STL  stack类模板。

    输入描述

    开始int或string代表需要处理的对象类型。对于每种类型,先构造两个目标类型的空栈,读入整数n,再读入n对整数v、x; 1<=v<=2; 将元素x入第v个栈,n对整数处理完成后, 将两个栈中元素出栈,并输出。

    输出描述

    每个栈中元素占一行,元素间以空格分隔。

    样例输入

    int
    7
    1 100
    2 200
    1 300
    2 400 
    1 50
    1 60
    2 80
    
    string
    6
    1   some
    1   one
    2   two
    2   tom
    1   cat
    2   hdu

    样例输出

    60 50 300 100 
    80 400 200 
    cat one some 
    hdu tom two


    练习

    代码:(杭电学生勿抄,防查重)
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <vector>
    #define INF 0x3f3f3f3f
    #define eps 1e-8
    #define PI acos(-1.0)
    #define N 110
    #define me(a,b) memset(a,b,sizeof(a))
    #define file freopen("in.txt","r",stdin)
    using namespace std;
    const int mod=1e8;
    
    template <class Elem>
    class mystack
    {
    private:
           vector<Elem> v;
    public:
          mystack(){v.clear();}
          mystack<Elem>(mystack<Elem>& x):v(x.v){}
    
          void operator=(mystack x)
          {
                v.resize(x.v.size());
                for(int i=0;i<x.v.size();i++)
                {
                      v[i]=x.v[i];
                }
          }
          void push(Elem x)
          {
                v.push_back(x);
          }
          void pop()
          {
                v.pop_back();
          }
          bool empty()
          {
                return v.empty();
          }
          bool size()
          {
                return v.size();
          }
          Elem top()
          {
                return v.back();
          }
          ~mystack(){v.clear();}
    };
    
    
    int main()
    {
          //file;
          int n;
          string s;
    /*
          //测试 构造函数
          mystack<int>a;
          a.push(3);
          a.push(4);
          a.push(2);
          mystack<int>b(a);
          while(!b.empty())
          {
                cout<<b.top();
                b.pop();
                if(b.size())
                      cout<<' ';
          }
          cout<<endl;
    */
          while(cin>>s)
          {
                cin>>n;
                if(s[0]=='i')
                {
                      mystack<int> a[2];
                      typedef int ttt;

               //输出代码和下面的一模一样,由于typedef作用域问题,我选择复制代码。。 ttt tmp;
    int t; while(n--) { cin>>t>>tmp; a[t-1].push(tmp); } for(int i=0;i<2;i++) { while(!a[i].empty()) { cout<<a[i].top(); a[i].pop(); if(a[i].size()) cout<<' '; } cout<<endl; } } else { mystack<string> a[2]; typedef string ttt; ttt tmp;int t; while(n--) { cin>>t>>tmp; a[t-1].push(tmp); } for(int i=0;i<2;i++) { while(!a[i].empty()) { cout<<a[i].top(); a[i].pop(); if(a[i].size()) cout<<' '; } cout<<endl; } } } }


  • 相关阅读:
    尚硅谷面试第一季-05递归与迭代
    尚硅谷面试第一季-04方法的参数传递机制
    尚硅谷面试第一季-03类初始化和实例初始化
    python爬爬爬之单网页html页面爬取
    python之花瓣美女下载
    (转载博文)VC++API速查
    (转载博文)MFC 窗口句柄获取
    全局变量的声明
    python图片小爬虫
    Opencv2.4.4作图像旋转和缩放
  • 原文地址:https://www.cnblogs.com/zgncbsylm/p/10851283.html
Copyright © 2011-2022 走看看