Skip to content

Instantly share code, notes, and snippets.

@H2CO3
Created October 25, 2017 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save H2CO3/e96a2c996c1509a85afa81eb6a57c6dd to your computer and use it in GitHub Desktop.
Save H2CO3/e96a2c996c1509a85afa81eb6a57c6dd to your computer and use it in GitHub Desktop.
#include <vector>
#include <cstdio>
struct Node {
int value;
Node *next;
Node(int _value, Node *_next): value(_value), next(_next) {}
~Node() {
std::printf("Destroying %d\n", value);
}
};
int main() {
auto head = new Node(
0, new Node (
1, new Node(
2, new Node(
3, nullptr
)
)
)
);
auto tmp = head;
auto next = head->next;
while (next != nullptr) {
delete tmp;
tmp = next;
next = next->next;
}
}
// output:
// Destroying 0
// Destroying 1
// Destroying 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment