Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define sz(v) (int)v.size()
- #define pb push_back
- using namespace std;
- class Node{
- public:
- int value;
- Node* prev, * next;
- public:
- Node(int value){
- this->value = value;
- this->prev = this->next = NULL;
- }
- };
- class LinkedList{
- public:
- Node* head, * tail;
- public:
- LinkedList(){
- head = tail = nullptr;
- }
- Node* push_front(int value){
- Node* ptr = new Node(value);
- ptr->next = head;
- if(head != nullptr){
- head->prev = ptr;
- }
- if(tail == nullptr){
- tail = ptr;
- }
- head = ptr;
- return ptr;
- }
- Node* push_back(int value){
- Node* ptr = new Node(value);
- ptr->prev = tail;
- if(tail != nullptr){
- tail->next = ptr;
- }
- if(head == nullptr){
- head = ptr;
- }
- tail = ptr;
- return ptr;
- }
- void pop_front(){
- Node* ptr = head->next;
- if(ptr != nullptr){
- ptr->prev = nullptr;
- }else{
- tail = nullptr;
- }
- cout << head->value << '\n';
- delete head;
- head = ptr;
- }
- void pop_back(){
- Node* ptr = tail->prev;
- if(ptr != nullptr){
- ptr->next = nullptr;
- }else{
- head = nullptr;
- }
- cout << tail->value << '\n';
- delete tail;
- tail = ptr;
- }
- };
- struct Queue{
- private:
- LinkedList Node;
- int max_n;
- int size;
- public:
- Queue(int n){
- size = 0;
- max_n = n;
- }
- void push_back(int value){
- if(size == max_n){
- cout << "error\n";
- }else{
- Node.push_back(value);
- size++;
- }
- }
- void push_front(int value){
- if(size == max_n){
- cout << "error\n";
- }else{
- Node.push_front(value);
- size++;
- }
- }
- void pop_front(){
- if(size == 0){
- cout << "error\n";
- }else {
- Node.pop_front();
- size--;
- }
- }
- void pop_back(){
- if(size == 0){
- cout << "error\n";
- }else{
- Node.pop_back();
- size--;
- }
- }
- };
- signed main() {
- ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- int n, m;
- string input;
- cin >> n >> m;
- Queue Queue(m);
- while(n--){
- cin >> input;
- if(input == "push_back"){
- int num;
- cin >> num;
- Queue.push_back(num);
- }else if(input == "push_front"){
- int num;
- cin >> num;
- Queue.push_front(num);
- }else if(input == "pop_back"){
- Queue.pop_back();
- }else{
- Queue.pop_front();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement