eolymp
Problems

Deque with error protection

published at 1/27/22, 1:41:22 pm

6130 has the same solution

include <iostream>

include <deque>

using namespace std; int main() { string n; long long m; deque<long long>v; while(cin>>n){ if(n=="push_back"){ cin>>m; v.push_back(m); cout<<"ok"<<endl; } else if(n=="push_front"){ cin>>m; v.push_front(m); cout<<"ok"<<endl; } else if(n=="pop_back"){ if(v.size()==0){ cout<<"error"<<endl; } else{ cout<<v.back()<<endl; v.pop_back(); } } else if(n=="pop_front"){ if(v.size()==0){ cout<<"error"<<endl; } else{ cout<<v.front()<<endl; v.pop_front(); } } else if(n=="size"){ cout<<v.size()<<endl; } else if(n=="clear"){ v.clear(); cout<<"ok"<<endl; } else if(n=="front"){ if(v.size()==0){ cout<<"error"<<endl; } else cout<<v.front()<<endl; } else if(n=="back"){ if(v.size()==0){ cout<<"error"<<endl; } else cout<<v.back()<<endl; } else if(n=="exit"){ cout<<"bye"; return 0; } } }

published at 5/23/22, 7:28:08 pm

include<deque>

include<bits/stdc++.h>

using namespace std; int main() { deque<int>d; long n; string s; while(true) { cin>>s; if(s=="push_front") { cin>>n; d.push_front(n); cout<<"ok"<<endl; } if(s=="push_back") { cin>>n; d.push_back(n); cout<<"ok"<<endl; } if(s=="pop_back") { if(d.empty()==true) { cout<<"error"<<endl; } else{ cout<<d.back()<<endl; d.pop_back(); } } if(s=="pop_front") { if(d.empty()==true) { cout<<"error"<<endl; } else{ cout<<d.front()<<endl; d.pop_front();} } if(s=="front") { if(d.empty()==true) { cout<<"error"<<endl; } else cout<<d.front()<<endl; } if(s=="back") { if(d.empty()==true) { cout<<"error"<<endl; } else cout<<d.back()<<endl; } if(s=="size") { cout<<d.size()<<endl; } if(s=="clear") { d.clear(); cout<<"ok"<<endl; } if(s=="exit") { cout<<"bye"<<endl; break; } }

}