Advertisement
smatskevich

ReadList

May 22nd, 2021
950
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.57 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6.   int data;
  7.   Node* next;
  8.  
  9.   Node(int x) : data(x), next(nullptr) {}
  10. };
  11.  
  12. int main() {
  13.   // 1 2 3 0
  14.   Node* head = nullptr;
  15.   int x = 0;
  16.   Node* cur = nullptr;
  17.   while (cin >> x, x != 0) {
  18.     if (cur) {
  19.       cur = cur->next = new Node(x);
  20.     } else {
  21.       head = cur = new Node(x);
  22.     }
  23.   }
  24.  
  25.   for (cur = head; cur; cur = cur->next) {
  26.     cout << cur->data << " ";
  27.   }
  28.   for (cur = head; cur;) {
  29.     Node* to_delete = cur;
  30.     cur = cur->next;
  31.     delete to_delete;
  32.   }
  33.   return 0;
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement