Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <memory>
- const int SIZE=7;
- struct Node {
- int val;
- std::shared_ptr<Node> next;
- };
- std::shared_ptr<Node> newNode(int v) { // Create and return new node
- std::shared_ptr<Node> n(new Node);
- n->val = v;
- n->next = NULL;
- return n;
- }
- void print(std::shared_ptr<Node> r) { // Print the list
- while (r != NULL) {
- std::cout << r->val << ' ';
- r = r->next;
- }
- std::cout << std::endl;
- }
- int main() {
- std::shared_ptr<Node> root(new Node), last;
- int n[SIZE] = {7, 4, 3, 9, 6, 2, 5}; // Simple input, so we can focus on list
- root->val = n[0];
- root->next = NULL;
- last = root;
- for (int i=1; i<SIZE; ++i) { // Build the list
- last->next = newNode(n[i]);
- last = last->next;
- }
- last->next = NULL;
- print(root); //-> 7 4 3 9 6 2 5
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement