Guest User

Untitled

a guest
Jan 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <stack>
  2. #include <iostream>
  3. #include <utility>
  4. using namespace std;
  5. class Node
  6. {
  7. public:
  8. Node(int num) : _num(num), _next(NULL){}
  9. friend class List;
  10. private:
  11. int _num;
  12. Node* _next;
  13. };
  14.  
  15. class List
  16. {
  17. public:
  18. List() : _root(NULL){}
  19. void add(int rhs)
  20. {
  21. if (!_root)
  22. {
  23. _root = new Node(rhs);
  24. }
  25. else
  26. {
  27. Node* tmp = _root;
  28. while (tmp->_next)
  29. {
  30. tmp = tmp->_next;
  31. }
  32. tmp->_next = new Node(rhs);
  33. }
  34. }
  35. void print() const
  36. {
  37. Node* tmp = _root;
  38. while (tmp)
  39. {
  40. cout << tmp->_num << " ";
  41. tmp = tmp->_next;
  42. }
  43. cout << endl;
  44. }
  45.  
  46. void doubling() // udvoit kazhdoe znachenie
  47. {
  48. Node* tmp = _root;
  49. while (tmp)
  50. {
  51. tmp->_num *= 2;
  52. tmp = tmp->_next;
  53. }
  54. }
  55. ~List()
  56. {
  57. Node* tmp = _root;
  58. while (tmp)
  59. {
  60. _root = tmp->_next;
  61. delete tmp;
  62. tmp = _root;
  63. }
  64. }
  65. private:
  66. Node* _root;
  67. };
  68.  
  69. int main()
  70. {
  71. List lst;
  72. lst.add(7);
  73. lst.doubling();
  74. lst.add(3);
  75. lst.doubling();
  76.  
  77.  
  78.  
  79. lst.print();
  80. return 0;
  81. }
Add Comment
Please, Sign In to add comment