Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- Node* next;
- Node(int x) : data(x), next(nullptr) {}
- };
- int main() {
- // 1 2 3 0
- Node* head = nullptr;
- int x = 0;
- Node* cur = nullptr;
- while (cin >> x, x != 0) {
- if (cur) {
- cur = cur->next = new Node(x);
- } else {
- head = cur = new Node(x);
- }
- }
- for (cur = head; cur; cur = cur->next) {
- cout << cur->data << " ";
- }
- for (cur = head; cur;) {
- Node* to_delete = cur;
- cur = cur->next;
- delete to_delete;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement