Advertisement
Sanlover

Untitled

Oct 7th, 2020
606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6.  
  7.  
  8.  
  9. class list
  10. {
  11. private:
  12. struct cell
  13. {
  14. string surname;
  15. string serialNumber;
  16. cell* next;
  17.  
  18. cell()
  19. {
  20. next = nullptr;
  21. }
  22.  
  23. cell(string surname, string serialNumber, cell* next)
  24. : surname(surname), serialNumber(serialNumber), next(next) {};
  25.  
  26. };
  27.  
  28. cell* first;
  29.  
  30.  
  31. string carName;
  32. list* next;
  33.  
  34. public:
  35. list()
  36. {
  37. first = nullptr;
  38. next = nullptr;
  39.  
  40. }
  41. list(string carName, list* next) : carName(carName), next(next)
  42. {
  43. first = nullptr;
  44. }
  45.  
  46. void setCarName(string _carName);
  47. string getCarName();
  48. void setNext(list* _next);
  49. list* getNext();
  50.  
  51. bool isEmpty();
  52. void print();
  53. void add(string surname, string serialNumber);
  54. };
  55.  
  56. class carList : public list
  57. {
  58. private:
  59. list* first;
  60.  
  61. public:
  62. carList()
  63. {
  64. first = nullptr;
  65. }
  66.  
  67. bool isEmpty();
  68. void print();
  69. void add(string carName);
  70. list* getList(string carName);
  71. };
  72.  
  73. int main()
  74. {
  75.  
  76. carList cars;
  77. cars.add("Toyota");
  78. cars.getList("Toyota")->add("A", "B");
  79. cars.getList("Toyota")->add("C", "D");
  80. cars.getList("Toyota")->print();
  81.  
  82. return 0;
  83. }
  84.  
  85.  
  86. void list::setCarName(string _carName)
  87. {
  88. carName = _carName;
  89. }
  90.  
  91. string list::getCarName()
  92. {
  93. return carName;
  94. }
  95.  
  96. void list::setNext(list* _next)
  97. {
  98. next = _next;
  99. }
  100.  
  101. list* list::getNext()
  102. {
  103. return next;
  104. };
  105.  
  106. bool
  107. list::isEmpty()
  108. {
  109. return first == nullptr;
  110. }
  111.  
  112. void
  113. list::print()
  114. {
  115. if (isEmpty())
  116. {
  117. cout << "List is empty" << endl;
  118. return;
  119. }
  120.  
  121. cell* it = first;
  122.  
  123. size_t k = 1;
  124. while (it != nullptr)
  125. {
  126. cout << k << ") Surname is " << it->surname << " | Serial Number is " << it->serialNumber << endl;
  127. k++;
  128. it = it->next;
  129. }
  130. }
  131.  
  132. void
  133. list::add(string surname, string serialNumber)
  134. {
  135. cell* newCell = new cell(surname, serialNumber, nullptr);
  136.  
  137. if (isEmpty())
  138. {
  139. first = newCell;
  140. }
  141. else
  142. {
  143. cell* it = first;
  144.  
  145. while (it->next != nullptr)
  146. it = it->next;
  147.  
  148. it->next = newCell;
  149. }
  150. }
  151.  
  152. void
  153. carList::add(string carName)
  154. {
  155. list* newList = new list(carName, nullptr);
  156.  
  157. if (isEmpty())
  158. {
  159. first = newList;
  160. }
  161. else
  162. {
  163. list* it = first;
  164.  
  165. while (it->getNext() != nullptr)
  166. it = it->getNext();
  167.  
  168. it->setNext(newList);
  169. }
  170. }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178. list* carList::getList(string carName)
  179. {
  180. if (first == nullptr)
  181. return nullptr;
  182.  
  183. list* it = first;
  184.  
  185. while(it->getNext() != nullptr)
  186. {
  187. if(it->getCarName() == carName)
  188. {
  189. return it;
  190. }
  191. it = it->getNext();
  192. }
  193.  
  194. return nullptr;
  195. }
  196.  
  197.  
  198.  
  199.  
  200.  
  201. bool
  202. carList::isEmpty()
  203. {
  204. return first == nullptr;
  205. }
  206.  
  207. void
  208. carList::print()
  209. {
  210. if (isEmpty())
  211. {
  212. cout << "CarList is empty" << endl;
  213. return;
  214. }
  215.  
  216. list* it = first;
  217.  
  218. size_t k = 1;
  219. while (it != nullptr)
  220. {
  221. cout << k << ") Car name is " << it->getCarName() << endl;
  222. k++;
  223. it = it->getNext();
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement