Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct Node {
- int Data;
- Node* Next;
- Node* Prev;
- Node(int value) : Data(value), Next(nullptr), Prev(nullptr) {}
- };
- int main() {
- Node* head = nullptr;
- int n = 0;
- std::cin >> n;
- for (int i = 0; i < n; ++i) {
- int value = 0;
- std::cin >> value;
- if (head == nullptr) { // Пишут if (!head)
- head = new Node(value);
- } else {
- Node* new_node = new Node(value);
- new_node->Next = head;
- head->Prev = new_node;
- head = new_node;
- }
- }
- for (Node* c = head; c != nullptr; c = c->Next) {
- std::cout << c->Data << " ";
- }
- for (Node* c = head; c != nullptr;) {
- Node* next_after_c = c->Next;
- delete c;
- c = next_after_c;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement