Advertisement
OIQ

Untitled

OIQ
Oct 11th, 2021
741
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct ListNode {
  5.     int value;
  6.     ListNode *next;
  7.     ListNode *prev;
  8. };
  9.  
  10. class LinkedList {
  11. public:
  12.     ListNode *head, *tail;
  13.  
  14.     LinkedList();
  15.  
  16.     explicit LinkedList(int n);
  17.  
  18.     void pushBack(int x);
  19.  
  20.     void pushFront(int x);
  21.  
  22.     void insert(int x, int position);
  23.  
  24.     void remove(int position);
  25.  
  26.     void print(bool direction);
  27.  
  28.     ~LinkedList();
  29. };
  30.  
  31. LinkedList::LinkedList() {
  32.     this->head = nullptr;
  33.     this->tail = nullptr;
  34. }
  35.  
  36. LinkedList::LinkedList(int n) : LinkedList() {
  37.     for (int c, i = 0; i < n; i++) {
  38.         std::cin >> c;
  39.         this->pushBack(c);
  40.     }
  41. }
  42.  
  43. LinkedList::~LinkedList() {
  44.     ListNode *current = this->head;
  45.     while (head) {
  46.         this->head = this->head->next;
  47.         delete current;
  48.         current = this->head;
  49.     }
  50. }
  51.  
  52. void LinkedList::pushBack(int x) {
  53.     ListNode *node = new ListNode{x, nullptr, nullptr};
  54.     if (!this->head) {
  55.         this->head = node;
  56.         this->tail = node;
  57.     } else {
  58.         this->tail->next = node;
  59.         node->prev = tail;
  60.         this->tail = this->tail->next;
  61.     }
  62. }
  63.  
  64. void LinkedList::pushFront(int x) {
  65.     ListNode *node = new ListNode{x, nullptr, nullptr};
  66.     if (!this->head) {
  67.         this->head = node;
  68.         this->tail = node;
  69.     } else {
  70.         this->head->prev = node;
  71.         node->next = head;
  72.         this->head = this->head->prev;
  73.     }
  74. }
  75.  
  76. void LinkedList::insert(int x, int position) {
  77.     ListNode *current = this->head;
  78.     int place = 0;
  79.     while (place != position) {
  80.         current = current->next;
  81.         place++;
  82.     }
  83.     if (!current->next) {
  84.         this->pushBack(x);
  85.         return;
  86.     }
  87.     ListNode *insertion_node = new ListNode{x, current->next, current};
  88.     insertion_node->next->prev = insertion_node;
  89.     current->next = insertion_node;
  90. }
  91.  
  92. void LinkedList::remove(int position) {
  93.     if (position == -1) {
  94.         head = head->next;
  95.         return;
  96.     }
  97.     ListNode *current = this->head;
  98.     int place = 0;
  99.     while (current && place != position) {
  100.         current = current->next;
  101.         place++;
  102.     }
  103.     if (!current || !current->next) {
  104.         return;
  105.     }
  106.     if (!current->next->next) {
  107.         // current->next = tail;
  108.         tail = tail->prev;
  109.         tail->next = nullptr;
  110.         return;
  111.     }
  112.     current->next = current->next->next;
  113.     current->next->prev = current;
  114. }
  115.  
  116. void LinkedList::print(bool direction = true) {
  117.     if (direction) {
  118.         ListNode *current = this->head;
  119.         while (current) {
  120.             std::cout << current->value << " ";
  121.             current = current->next;
  122.         }
  123.     } else {
  124.         ListNode *current = this->tail;
  125.         while (current) {
  126.             std::cout << current->value << " ";
  127.             current = current->prev;
  128.         }
  129.     }
  130.     std::cout << "\n";
  131. }
  132.  
  133. void handleCommand(LinkedList *list, const std::string &command) {
  134.     if (command == "push_back") {
  135.         int x;
  136.         std::cin >> x;
  137.         list->pushBack(x);
  138.     } else if (command == "push_front") {
  139.         int x;
  140.         std::cin >> x;
  141.         list->pushFront(x);
  142.     } else if (command == "insert") {
  143.         int x, position;
  144.         std::cin >> x >> position;
  145.         list->insert(x, position);
  146.     } else if (command == "remove") {
  147.         int position;
  148.         std::cin >> position;
  149.         list->remove(position);
  150.     }
  151. }
  152.  
  153. int main() {
  154.     int n, m;
  155.     std::cin >> n >> m;
  156.  
  157.     LinkedList *list = new LinkedList(n);
  158.  
  159.     std::string command;
  160.     for (int i = 0; i < m; i++) {
  161.         std::cin >> command;
  162.         handleCommand(list, command);
  163.         // list.print();
  164.     }
  165.  
  166.     list->print();
  167.     list->print(false);
  168.  
  169.     delete list;
  170.  
  171.     return 0;
  172. }
  173.  
Advertisement
RAW Paste Data Copied
Advertisement