Advertisement
L3peha

Untitled

Feb 12th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. using namespace std;
  5.  
  6. class unit
  7. {
  8. public:
  9. unit(int data); //constructor
  10. virtual ~unit(); //destructor
  11. void push(int data);
  12. int getData();
  13. unit* getNext();
  14. unit* findElement(int data);
  15. void addToTheEnd(int data);
  16. void deleteLast();
  17.  
  18. private:
  19. unit* next;
  20. int data;
  21. };
  22.  
  23. unit::unit(int data)
  24. {
  25. this->data = data;
  26. next = NULL;
  27. }
  28.  
  29. unit::~unit()
  30. {
  31. delete[] next;
  32. }
  33.  
  34. void unit::push(int data)
  35. {
  36. unit* element = new unit(this->data);
  37. element->next = this->next;
  38. this->next = element;
  39. this->data = data;
  40. }
  41.  
  42. int unit::getData()
  43. {
  44. return data;
  45. }
  46.  
  47. unit* unit::getNext()
  48. {
  49. return this->next;
  50. }
  51.  
  52. unit* unit::findElement(int data)
  53. {
  54. unit* selected = this;
  55. while (selected->getData() != data)
  56. {
  57. selected = selected->getNext();
  58. }
  59. return selected;
  60. }
  61.  
  62. void unit::addToTheEnd(int data)
  63. {
  64. unit* newData = new unit(data);
  65. unit* selected = this;
  66. while (selected->next != NULL)
  67. {
  68. selected = selected->next;
  69. }
  70. selected->next = newData;
  71. }
  72.  
  73. void unit::deleteLast()
  74. {
  75. unit* selected = this;
  76. while (selected->next != 0)
  77. {
  78. selected = selected->next;
  79. }
  80. selected = NULL;
  81. }
  82.  
  83. class list
  84. {
  85. public:
  86. list();
  87. ~list();
  88. int getLenght();
  89. void print();
  90. void add(int data);
  91. private:
  92. int lenght;
  93. unit* root;
  94. };
  95.  
  96. list::list()
  97. {
  98. lenght = 0;
  99. }
  100.  
  101. list::~list()
  102. {
  103. delete root;
  104. }
  105.  
  106. int list::getLenght()
  107. {
  108. return lenght;
  109. }
  110.  
  111. void list::print()
  112. {
  113. unit* selected = root;
  114. while (selected->getNext() != NULL)
  115. {
  116. cout << selected->getData();
  117. cout << endl;
  118. }
  119. cout << selected->getData();
  120. }
  121.  
  122. void list::add(int data)
  123. {
  124. if (lenght != 0)
  125. {
  126. root->addToTheEnd(data);
  127. }
  128. else
  129. {
  130. root = new unit(data);
  131. }
  132. lenght++;
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement