Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- class Queue {
- public:
- void push(const int value) {
- vec1.push_back(value);
- }
- void pop() {
- check_queue();
- if (vec2.empty()) {
- std::cout << "error" << std::endl;
- }
- else {
- vec2.pop_back();
- }
- }
- void peek() {
- check_queue();
- if (vec2.empty()) {
- std::cout << "error" << std::endl;
- }
- else {
- std::cout << vec2.back() << std::endl;
- }
- }
- private:
- std::vector<int> vec1; // 6 7 8 9
- std::vector<int> vec2; // 5 4 3 2 1
- void check_queue() {
- if (vec2.empty()) {
- for (int i = vec1.size() - 1; i >= 0; i--) {
- vec2.push_back(vec1[i]);
- }
- vec1.clear();
- }
- }
- };
- int main() {
- setlocale(LC_ALL, "ru");
- Queue my_queue;
- for (int i = 0; i < 20; i++) {
- my_queue.push(i);
- }
- for (int i = 0; i < 20; i++) {
- my_queue.peek();
- my_queue.pop();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement