Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. struct stack1{
  7.     int* m;
  8.     int cap;
  9.     int last;
  10.     stack1(){
  11.         cap=1;
  12.         m=static_cast<int*>(operator new(cap * sizeof(int)));//m = new int[cap];
  13.         last=0;
  14.     }
  15.     void print(){
  16.         cout<<cap<<" "<<last<<" $$ ";
  17.         for(int i=0;i<cap;i++)
  18.             if(i>=last) cout<<"x ";
  19.             else cout<<*(m+i)<<" ";
  20.         cout<<'\n';
  21.     }
  22.     bool empty(){
  23.        return last==0;
  24.     }
  25.     void push(int val){
  26.         if(last<cap){
  27.             m[last]=val;
  28.             last++;
  29.         }
  30.         else{
  31.             int* tmp = static_cast<int*>(operator new(cap*2 * sizeof(int)));// = new int[cap*2];
  32.             //print();
  33.             for(int i=0;i<cap;i++){
  34.                 //cout<<i<<" ";
  35.                 //cout<<"// "<<*(m+i+1)<<'\n';
  36.                 tmp[i]=m[i];
  37.             }
  38.             //cout<<'\n';
  39.             tmp[last]=val;
  40.             //int* lastm=m;
  41.             operator delete(m);
  42.             m=tmp;
  43.             //print();
  44.             last++;
  45.             //operator delete(lastm);
  46.             cap*=2;
  47.         }
  48.     }
  49.     void pop(){
  50.         if(!empty()){
  51.             last--;
  52.             if(4*last<=cap){
  53.                 int* tmp = static_cast<int*>(operator new((cap/2+cap%2) * sizeof(int)));// = new int[cap/2];
  54.                 for(int i=0;i<last;i++)
  55.                     tmp[i]=m[i];
  56.                 cap=cap/2+cap%2;
  57.                 operator delete(m);
  58.                 m=tmp;
  59.             }
  60.             //last--;
  61.         }
  62.     }
  63. };
  64. int main(){
  65.     //ios_base::sync_with_stdio(0);
  66.     //freopen("stack1.out","w",stdout);
  67.     //freopen("stack1.in","r",stdin);
  68.     stack1 s;
  69.     int n;
  70.     cin>>n;
  71.     //cout<<n;
  72.     int x;
  73.     char ch;
  74.     for(int i=0;i<n;i++){
  75.         cin>>ch;
  76.         if(ch=='+'){
  77.             cin>>x;
  78.             s.push(x);
  79.         }
  80.         else{
  81.             cout<<s.m[s.last-1]<<'\n';
  82.             s.pop();
  83.         }
  84.         //s.print();
  85.     }
  86.     //cout<<"test";
  87.     //fclose(stdout);
  88.     //fclose(stdin);
  89.     //return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement