Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <list>
  5.  
  6. struct ListNode
  7. {
  8. bool isString;
  9. std::string string;
  10. int value;
  11.  
  12. ListNode()
  13. {
  14. isString = true;
  15. string = "No name";
  16. value = NULL;
  17. }
  18. ListNode(std::string str)
  19. {
  20. isString = true;
  21. string = str;
  22. value = NULL;
  23. }
  24. ListNode(int val)
  25. {
  26. isString = false;
  27. value = val;
  28. string = "";
  29. }
  30.  
  31. ListNode& operator=(ListNode& other)
  32. {
  33. isString = other.isString;
  34. string = other.string;
  35. value = other.value;
  36. }
  37.  
  38. bool operator==(ListNode& other)
  39. {
  40. if (this->isString == other.isString && this->string == other.string && this->value == other.value) return true;
  41. return false;
  42. }
  43. };
  44.  
  45. struct node
  46. {
  47. std::vector<ListNode> data;
  48. node* next;
  49.  
  50. node(std::vector<ListNode> x)
  51. {
  52. data = x;
  53. }
  54.  
  55. bool operator==(node& other)
  56. {
  57. bool flag = false;
  58.  
  59. size_t d = data.size();
  60. size_t a = other.data.size();
  61. if (d == a)
  62. {
  63. int count = 0;
  64. for (size_t i = 0; i < data.size(); i++)
  65. {
  66. if (data[i] == other.data[i]) count++;
  67. }
  68.  
  69. if (count == data.size()) flag = true;
  70. }
  71. }
  72. };
  73.  
  74. class List
  75. {
  76. size_t sizeList;
  77. node* first, * last;
  78.  
  79. public:
  80. List() { first = last = nullptr; }
  81.  
  82. bool isEmpty() const
  83. {
  84. if (sizeList == 0) return true;
  85. return false;
  86. }
  87.  
  88. void insertFront(std::vector<ListNode> x)
  89. {
  90. node* nodeToInsert = new node(x);
  91. //nodeToInsert->data = x;
  92. nodeToInsert->next = first;
  93. first = nodeToInsert;
  94. sizeList++;
  95. }
  96.  
  97. void insertLast(std::vector<ListNode> x)
  98. {
  99. node* nodeToInsert = new node(x);
  100. //nodeToInsert->data = x;
  101. nodeToInsert->next = nullptr;
  102. last->next = nodeToInsert;
  103. last = nodeToInsert;
  104. sizeList++;
  105. }
  106.  
  107. bool removeFirst()
  108. {
  109. if (isEmpty()) return false;
  110. node* firstNode = first;
  111. first = first->next;
  112. firstNode->next = nullptr;
  113. delete firstNode;
  114. sizeList--;
  115. return true;
  116. }
  117.  
  118. int getSize() const
  119. {
  120. return sizeList;
  121. }
  122.  
  123. node* find(std::vector<ListNode> key)
  124. {
  125. node* it = first;
  126. while (it && !isFirstDataAndKeyEqual(it->data, key)) it = it->next;
  127.  
  128. return it;
  129. }
  130.  
  131. bool remove(std::vector<ListNode> key)
  132. {
  133. if (isEmpty()) return false;
  134.  
  135. if (isFirstDataAndKeyEqual(first->data,key))
  136. {
  137. std::vector<ListNode> x = first->data;
  138. removeFirst();
  139. return true;
  140. }
  141. node* prev = first;
  142. while (prev->next)
  143. {
  144. if (isFirstDataAndKeyEqual(prev->next->data, key))
  145. {
  146. node* n = prev->next;
  147. prev->next = n->next;
  148. std::vector<ListNode> x = n->data;
  149. delete n;
  150. sizeList--;
  151. return true;
  152. }
  153. prev = prev->next;
  154. }
  155. return true;
  156. }
  157.  
  158. bool isFirstDataAndKeyEqual(std::vector<ListNode> first, std::vector<ListNode> key)
  159. {
  160. bool flag = false;
  161.  
  162. if (first.size() == key.size())
  163. {
  164. int count = 0;
  165. for (size_t i = 0; i < key.size(); i++)
  166. {
  167. if (first[i] == key[i]) count++;
  168. }
  169.  
  170. if (count == first.size()) flag = true;
  171. }
  172. return flag;
  173. }
  174.  
  175. node* firstEl()
  176. {
  177. return first;
  178. }
  179.  
  180. bool remove(node* it)
  181. {
  182. if (!it) return true;
  183. if (it->next)
  184. {
  185. node* n = it->next;
  186. std::vector<ListNode> x = it->data;
  187. *it = *n;
  188. delete n;
  189. --sizeList;
  190. if (!it->next)
  191. {
  192. last = it;
  193. }
  194. }
  195. return true;
  196. }
  197. };
  198.  
  199. //bool writeToList(std::list<std::vector<ListNode>>& list)
  200. //{
  201. // std::string plateNumber("list");
  202. // std::ofstream out(plateNumber, std::ios::binary);
  203. // if (!out) return false;
  204. // if (list.size() == 0) return false;
  205. //
  206. // size_t list_size = list.size();
  207. // out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
  208. //
  209. // for (std::list<std::vector<ListNode>>::iterator it = list.begin(); it != list.end(); it++)
  210. // {
  211. // it.;
  212. // }
  213. //}
  214.  
  215. bool writeToList1(List& list)
  216. {
  217. std::string plateNumber("list");
  218. std::ofstream out(plateNumber, std::ios::binary);
  219. if (!out) return false;
  220. if (list.getSize() == 0) return false;
  221. size_t list_size = list.getSize();
  222. out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
  223.  
  224. node* current = list.firstEl();
  225. while (current)
  226. {
  227. size_t vec_size = current->data.size();
  228. out.write(reinterpret_cast<char*>(&vec_size), sizeof(vec_size));
  229. for (size_t i = 0; i < vec_size; i++)
  230. {
  231. ListNode& el = current->data[i];
  232. out.write(reinterpret_cast<char*>(&el.string), sizeof(el.string));
  233. out.write(reinterpret_cast<char*>(&el.value), sizeof(int));
  234. out.write(reinterpret_cast<char*>(&el.isString), sizeof(el.isString));
  235. }
  236. }
  237.  
  238. return true;
  239. }
  240. // std::string plateNumber("list");
  241. // std::ofstream out(plateNumber, std::ios::binary);
  242. // if (!out) return false;
  243. // if (list.size() == 0) return false;
  244. //
  245. // size_t list_size = list.size();
  246. // out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
  247. //
  248. // for (std::list<std::vector<ListNode>>::iterator it = list.begin(); it != list.end(); it++)
  249. // {
  250. // it.;
  251. // }
  252. //}
  253.  
  254. int main()
  255. {
  256. std::vector<ListNode> firstVector;
  257. std::vector<ListNode> secondVector;
  258. std::vector<ListNode> thirdVector;
  259.  
  260. ListNode f("something");
  261. ListNode f1("something else");
  262. ListNode f2("something ss");
  263.  
  264. ListNode f3("123");
  265. ListNode f4(12);
  266. ListNode f5("12345");
  267.  
  268. ListNode f6(123);
  269. ListNode f7("674");
  270. ListNode f8("Aswda");
  271.  
  272. firstVector.push_back(f);
  273. firstVector.push_back(f1);
  274. firstVector.push_back(f2);
  275.  
  276. secondVector.push_back(f3);
  277. secondVector.push_back(f4);
  278. secondVector.push_back(f5);
  279.  
  280. thirdVector.push_back(f6);
  281. thirdVector.push_back(f7);
  282. thirdVector.push_back(f8);
  283.  
  284. std::list<std::vector<ListNode>> sa;
  285. List sas;
  286. sa.push_back(firstVector);
  287. sa.push_back(secondVector);
  288. sa.push_back(thirdVector);
  289. sa.remove(firstVector);
  290.  
  291. sas.insertFront(firstVector);
  292. sas.insertFront(secondVector);
  293. sas.insertFront(thirdVector);
  294.  
  295. if (!writeToList1(sas))
  296. {
  297. std::cout << "Error writing file.\n";
  298. return 1;
  299. }
  300.  
  301. std::cout << "Hello World" << std::endl;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement