Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <list>
- struct ListNode
- {
- bool isString;
- std::string string;
- int value;
- ListNode()
- {
- isString = true;
- string = "No name";
- value = NULL;
- }
- ListNode(std::string str)
- {
- isString = true;
- string = str;
- value = NULL;
- }
- ListNode(int val)
- {
- isString = false;
- value = val;
- string = "";
- }
- ListNode& operator=(ListNode& other)
- {
- isString = other.isString;
- string = other.string;
- value = other.value;
- }
- bool operator==(ListNode& other)
- {
- if (this->isString == other.isString && this->string == other.string && this->value == other.value) return true;
- return false;
- }
- };
- struct node
- {
- std::vector<ListNode> data;
- node* next;
- node(std::vector<ListNode> x)
- {
- data = x;
- }
- bool operator==(node& other)
- {
- bool flag = false;
- size_t d = data.size();
- size_t a = other.data.size();
- if (d == a)
- {
- int count = 0;
- for (size_t i = 0; i < data.size(); i++)
- {
- if (data[i] == other.data[i]) count++;
- }
- if (count == data.size()) flag = true;
- }
- }
- };
- class List
- {
- size_t sizeList;
- node* first, * last;
- public:
- List() { first = last = nullptr; }
- bool isEmpty() const
- {
- if (sizeList == 0) return true;
- return false;
- }
- void insertFront(std::vector<ListNode> x)
- {
- node* nodeToInsert = new node(x);
- //nodeToInsert->data = x;
- nodeToInsert->next = first;
- first = nodeToInsert;
- sizeList++;
- }
- void insertLast(std::vector<ListNode> x)
- {
- node* nodeToInsert = new node(x);
- //nodeToInsert->data = x;
- nodeToInsert->next = nullptr;
- last->next = nodeToInsert;
- last = nodeToInsert;
- sizeList++;
- }
- bool removeFirst()
- {
- if (isEmpty()) return false;
- node* firstNode = first;
- first = first->next;
- firstNode->next = nullptr;
- delete firstNode;
- sizeList--;
- return true;
- }
- int getSize() const
- {
- return sizeList;
- }
- node* find(std::vector<ListNode> key)
- {
- node* it = first;
- while (it && !isFirstDataAndKeyEqual(it->data, key)) it = it->next;
- return it;
- }
- bool remove(std::vector<ListNode> key)
- {
- if (isEmpty()) return false;
- if (isFirstDataAndKeyEqual(first->data,key))
- {
- std::vector<ListNode> x = first->data;
- removeFirst();
- return true;
- }
- node* prev = first;
- while (prev->next)
- {
- if (isFirstDataAndKeyEqual(prev->next->data, key))
- {
- node* n = prev->next;
- prev->next = n->next;
- std::vector<ListNode> x = n->data;
- delete n;
- sizeList--;
- return true;
- }
- prev = prev->next;
- }
- return true;
- }
- bool isFirstDataAndKeyEqual(std::vector<ListNode> first, std::vector<ListNode> key)
- {
- bool flag = false;
- if (first.size() == key.size())
- {
- int count = 0;
- for (size_t i = 0; i < key.size(); i++)
- {
- if (first[i] == key[i]) count++;
- }
- if (count == first.size()) flag = true;
- }
- return flag;
- }
- node* firstEl()
- {
- return first;
- }
- bool remove(node* it)
- {
- if (!it) return true;
- if (it->next)
- {
- node* n = it->next;
- std::vector<ListNode> x = it->data;
- *it = *n;
- delete n;
- --sizeList;
- if (!it->next)
- {
- last = it;
- }
- }
- return true;
- }
- };
- //bool writeToList(std::list<std::vector<ListNode>>& list)
- //{
- // std::string plateNumber("list");
- // std::ofstream out(plateNumber, std::ios::binary);
- // if (!out) return false;
- // if (list.size() == 0) return false;
- //
- // size_t list_size = list.size();
- // out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
- //
- // for (std::list<std::vector<ListNode>>::iterator it = list.begin(); it != list.end(); it++)
- // {
- // it.;
- // }
- //}
- bool writeToList1(List& list)
- {
- std::string plateNumber("list");
- std::ofstream out(plateNumber, std::ios::binary);
- if (!out) return false;
- if (list.getSize() == 0) return false;
- size_t list_size = list.getSize();
- out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
- node* current = list.firstEl();
- while (current)
- {
- size_t vec_size = current->data.size();
- out.write(reinterpret_cast<char*>(&vec_size), sizeof(vec_size));
- for (size_t i = 0; i < vec_size; i++)
- {
- ListNode& el = current->data[i];
- out.write(reinterpret_cast<char*>(&el.string), sizeof(el.string));
- out.write(reinterpret_cast<char*>(&el.value), sizeof(int));
- out.write(reinterpret_cast<char*>(&el.isString), sizeof(el.isString));
- }
- }
- return true;
- }
- // std::string plateNumber("list");
- // std::ofstream out(plateNumber, std::ios::binary);
- // if (!out) return false;
- // if (list.size() == 0) return false;
- //
- // size_t list_size = list.size();
- // out.write(reinterpret_cast<char*>(&list_size), sizeof(list_size));
- //
- // for (std::list<std::vector<ListNode>>::iterator it = list.begin(); it != list.end(); it++)
- // {
- // it.;
- // }
- //}
- int main()
- {
- std::vector<ListNode> firstVector;
- std::vector<ListNode> secondVector;
- std::vector<ListNode> thirdVector;
- ListNode f("something");
- ListNode f1("something else");
- ListNode f2("something ss");
- ListNode f3("123");
- ListNode f4(12);
- ListNode f5("12345");
- ListNode f6(123);
- ListNode f7("674");
- ListNode f8("Aswda");
- firstVector.push_back(f);
- firstVector.push_back(f1);
- firstVector.push_back(f2);
- secondVector.push_back(f3);
- secondVector.push_back(f4);
- secondVector.push_back(f5);
- thirdVector.push_back(f6);
- thirdVector.push_back(f7);
- thirdVector.push_back(f8);
- std::list<std::vector<ListNode>> sa;
- List sas;
- sa.push_back(firstVector);
- sa.push_back(secondVector);
- sa.push_back(thirdVector);
- sa.remove(firstVector);
- sas.insertFront(firstVector);
- sas.insertFront(secondVector);
- sas.insertFront(thirdVector);
- if (!writeToList1(sas))
- {
- std::cout << "Error writing file.\n";
- return 1;
- }
- std::cout << "Hello World" << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement