Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- using namespace std;
- struct stack1{
- int* m;
- int cap;
- int last;
- stack1(){
- cap=1;
- m=static_cast<int*>(operator new(cap * sizeof(int)));//m = new int[cap];
- last=0;
- }
- void print(){
- cout<<cap<<" "<<last<<" $$ ";
- for(int i=0;i<cap;i++)
- if(i>=last) cout<<"x ";
- else cout<<*(m+i)<<" ";
- cout<<'\n';
- }
- bool empty(){
- return last==0;
- }
- void push(int val){
- if(last<cap){
- m[last]=val;
- last++;
- }
- else{
- int* tmp = static_cast<int*>(operator new(cap*2 * sizeof(int)));// = new int[cap*2];
- //print();
- for(int i=0;i<cap;i++){
- //cout<<i<<" ";
- //cout<<"// "<<*(m+i+1)<<'\n';
- tmp[i]=m[i];
- }
- //cout<<'\n';
- tmp[last]=val;
- //int* lastm=m;
- operator delete(m);
- m=tmp;
- //print();
- last++;
- //operator delete(lastm);
- cap*=2;
- }
- }
- void pop(){
- if(!empty()){
- last--;
- if(4*last<=cap){
- int* tmp = static_cast<int*>(operator new((cap/2+cap%2) * sizeof(int)));// = new int[cap/2];
- for(int i=0;i<last;i++)
- tmp[i]=m[i];
- cap=cap/2+cap%2;
- operator delete(m);
- m=tmp;
- }
- //last--;
- }
- }
- };
- int main(){
- //ios_base::sync_with_stdio(0);
- //freopen("stack1.out","w",stdout);
- //freopen("stack1.in","r",stdin);
- stack1 s;
- int n;
- cin>>n;
- //cout<<n;
- int x;
- char ch;
- for(int i=0;i<n;i++){
- cin>>ch;
- if(ch=='+'){
- cin>>x;
- s.push(x);
- }
- else{
- cout<<s.m[s.last-1]<<'\n';
- s.pop();
- }
- //s.print();
- }
- //cout<<"test";
- //fclose(stdout);
- //fclose(stdin);
- //return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement