SHARE
TWEET

Untitled

a guest Nov 18th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring> 
  5. using namespace std;
  6. template<typename K>
  7. class Stack
  8. {
  9. public:
  10.     int size;
  11.     Stack(){
  12.         this->first=NULL;
  13.         this->size=0;
  14.     };
  15.    
  16.     struct Structer
  17.     {
  18.         K value;
  19.         Structer* next;
  20.     };
  21.     Structer* first;
  22.  
  23.     void Push(K value)
  24.     {
  25.         Structer* tr = new Structer;
  26.         tr->value=value;
  27.         tr->next = first;
  28.         first = tr;
  29.         size++;
  30.     }
  31.     void pop()
  32.     {
  33.         if(first!=NULL){
  34.         Structer* tmp=first;
  35.         first = first->next;
  36.         free(tmp);
  37.         size--;}
  38.     }
  39.     bool empty()
  40.     {
  41.         return (first == NULL) ? true : false;
  42.     }
  43. };
  44. template<typename K>
  45. void does(Stack<K> Str){
  46.     freopen("input.txt", "r", stdin);
  47.     char T;
  48.     int N;
  49.     cin>>T>>N;
  50.     for(int i=0; i<N; i++){
  51.         string P;
  52.         cin>>P;
  53.         int l=P.length();
  54.         if(l==4){
  55.             K value;
  56.             cin>>value;
  57.             cout<<value;
  58.             Str.Push(value);
  59.         }
  60.         else
  61.             Str.pop();    
  62.         }
  63.     cout<<Str.size<<' ';
  64.     K sum;
  65.     while (Str.first!=NULL){
  66.         sum+=Str.first->value;
  67.         Str.first=Str.first->next;
  68.     }
  69.     cout<<sum;
  70. }
  71. void createStack(char T){
  72.         if (T='I'){
  73.             Stack<int>A;
  74.             does(A);}
  75.         else if (T='D'){
  76.             Stack<double>A;
  77.             does(A);}
  78.         else if (T='S'){
  79.             Stack<string>A;
  80.             does(A);}
  81.     };
  82. int main(void){
  83.     freopen("input.txt", "r", stdin);
  84.     freopen("output.txt", "w", stdout);
  85.     char T;
  86.     cin>>T;
  87.     createStack(T);
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top