Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <memory>
  4.  
  5. const int SIZE=7;
  6.  
  7. struct Node {
  8. int val;
  9. std::shared_ptr<Node> next;
  10. };
  11. std::shared_ptr<Node> newNode(int v) { // Create and return new node
  12. std::shared_ptr<Node> n(new Node);
  13. n->val = v;
  14. n->next = NULL;
  15. return n;
  16. }
  17. void print(std::shared_ptr<Node> r) { // Print the list
  18. while (r != NULL) {
  19. std::cout << r->val << ' ';
  20. r = r->next;
  21. }
  22. std::cout << std::endl;
  23. }
  24. int main() {
  25. std::shared_ptr<Node> root(new Node), last;
  26. int n[SIZE] = {7, 4, 3, 9, 6, 2, 5}; // Simple input, so we can focus on list
  27. root->val = n[0];
  28. root->next = NULL;
  29. last = root;
  30. for (int i=1; i<SIZE; ++i) { // Build the list
  31. last->next = newNode(n[i]);
  32. last = last->next;
  33. }
  34. last->next = NULL;
  35. print(root); //-> 7 4 3 9 6 2 5
  36. std::cout << std::endl;
  37. return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement