smatskevich

List

Oct 17th, 2020
436
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. struct Node {
  4.   int Data;
  5.   Node* Next;
  6.   Node* Prev;
  7.  
  8.   Node(int value) : Data(value), Next(nullptr), Prev(nullptr) {}
  9. };
  10.  
  11. int main() {
  12.   Node* head = nullptr;
  13.   int n = 0;
  14.   std::cin >> n;
  15.   for (int i = 0; i < n; ++i) {
  16.     int value = 0;
  17.     std::cin >> value;
  18.     if (head == nullptr) {  // Пишут if (!head)
  19.       head = new Node(value);
  20.     } else {
  21.       Node* new_node = new Node(value);
  22.       new_node->Next = head;
  23.       head->Prev = new_node;
  24.       head = new_node;
  25.     }
  26.   }
  27.  
  28.   for (Node* c = head; c != nullptr; c = c->Next) {
  29.     std::cout << c->Data << " ";
  30.   }
  31.  
  32.   for (Node* c = head; c != nullptr;) {
  33.     Node* next_after_c = c->Next;
  34.     delete c;
  35.     c = next_after_c;
  36.   }
  37.  
  38.   return 0;
  39. }
  40.  
RAW Paste Data