Задачи

Дек неограниченного размера

опубликовано 17.05.2022, 15:17:14

YASHMA IS COMINAA

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; } }

}

опубликовано 05.12.2022, 00:16:49

include<bits/stdc++.h>

using namespace std; int main(){ deque<int> deq; int d; string a; while(cin>>a and a!="exit"){ if(a=="push_front"){ cin>>d; deq.push_front(d); cout<<"ok\n"; } if(a=="push_back"){ cin>>d; deq.push_back(d); cout<<"ok\n"; } if(a=="pop_front"){ if(deq.empty()) cout<<"error\n"; else{ cout<<deq.front()<<'\n'; deq.pop_front();} } if(a=="pop_back"){ if(deq.empty()) cout<<"error\n"; else{ cout<<deq.back()<<'\n'; deq.pop_back();} } if(a=="front"){ if(deq.empty()) cout<<"error\n"; else cout<<deq.front()<<'\n'; } if(a=="back"){ if(deq.empty()) cout<<"error\n"; else cout<<deq.back()<<'\n'; } if(a=="size") cout<<(int)deq.size()<<'\n'; if(a=="clear"){ deq.clear(); cout<<"ok\n"; } } cout<<"bye"; return 0; }