Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <list>
- #include <vector>
- #include <string>
- #include <random>
- #include <ctime>
- using namespace std;
- template <class T>
- class Element
- {
- //элемент связного списка
- private:
- //указатель на предыдущий и следующий элемент
- Element* next;
- Element* prev;
- //информация, хранимая в поле
- T field;
- public:
- Element(T value = 0, Element<T>* next_ptr = NULL, Element<T>* prev_ptr = NULL)
- {
- field = value;
- next = next_ptr;
- prev - prev_ptr;
- }
- //доступ к полю *next
- virtual Element* getNext() { return next; }
- virtual void setNext(Element* value) { next = value; }
- //доступ к полю *prev
- virtual Element* getPrevious() { return prev; }
- virtual void setPrevious(Element* value) { prev = value; }
- //доступ к полю с хранимой информацией field
- virtual T getValue() { return field; }
- virtual void setValue(T value) { field = value; }
- template<class T> friend ostream& operator<< (ostream& ustream, Element<T>& obj);
- };
- template<class T>
- ostream& operator << (ostream& ustream, Element<T>& obj)
- {
- ustream << obj.field;
- return ustream;
- }
- template <class T>
- class LinkedListParent
- {
- protected:
- //достаточно хранить начало и конец
- Element<T>* head;
- Element<T>* tail;
- //для удобства храним количество элементов
- int num;
- public:
- virtual int Number() { return num; }
- virtual Element<T>* getBegin() { return head; }
- virtual Element<T>* getEnd() { return tail; }
- LinkedListParent()
- {
- //конструктор без параметров
- cout << "\nParent constructor";
- head = NULL;
- num = 0;
- }
- //чисто виртуальная функция: пока не определимся с типом списка, не сможем реализовать добавление
- virtual Element<T>* push(T value) = 0;
- //чисто виртуальная функция: пока не определимся с типом списка, не сможем реализовать удаление
- virtual Element<T>* pop() = 0;
- virtual ~LinkedListParent()
- {
- Element<T>* it = head;
- while (it != nullptr)
- {
- Element<T>* cur = it->next;
- delete it;
- it = cur;
- }
- delete it;
- }
- //получение элемента по индексу - какова асимптотическая оценка этого действия ?
- virtual Element<T>* operator[](int i)
- {
- //индексация
- if (i<0 || i>num) return NULL;
- int k = 0;
- //ищем i-й элемент - вставем в начало и отсчитываем i шагов вперед
- Element<T>* cur = head;
- for (k = 0; k < i; k++)
- {
- cur = cur->getNext();
- }
- return cur;
- }
- template<class T> friend ostream& operator<< (ostream&
- ustream, LinkedListParent<T>& obj);
- template<class T> friend istream& operator>> (istream&
- ustream, LinkedListParent<T>& obj);
- };
- template<class T>
- ostream& operator << (ostream& ustream, LinkedListParent<T>& obj)
- {
- if (typeid(ustream).name() == typeid(ofstream).name())
- {
- ustream << obj.num << "\n";
- for (Element<T>* current = obj.getBegin(); current !=
- NULL; current = current->getNext())
- ustream << current->getValue() << " ";
- return ustream;
- }
- ustream << "\nLength: " << obj.num << "\n";
- int i = 0;
- for (Element<T>* current = obj.getBegin(); current != NULL; current = current->getNext(), i++)
- ustream << "arr[" << i << "] = " << current->getValue() << "\n";
- return ustream;
- }
- template<class T>
- istream& operator >> (istream& ustream, LinkedListParent<T>& obj)
- {
- //чтение из файла и консоли совпадают
- int len;
- ustream >> len;
- //здесь надо очистить память под obj, установить obj.num = 0
- double v = 0;
- for (int i = 0; i < len; i++)
- {
- ustream >> v;
- obj.push(v);
- }
- return ustream;
- }
- template<typename ValueType>
- class ListIterator : public std::iterator<std::input_iterator_tag, ValueType>
- {
- private:
- public:
- ListIterator() { ptr = NULL; }
- //ListIterator(ValueType* p) { ptr = p; }
- ListIterator(Element<ValueType>* p) { ptr = p; }
- ListIterator(const ListIterator& it) { ptr = it.ptr; }
- bool operator!=(ListIterator const& other) const {
- return
- ptr != other.ptr;
- }
- bool operator==(ListIterator const& other) const {
- return
- ptr == other.ptr;
- }//need for BOOST_FOREACH
- Element<ValueType>& operator*()
- {
- return *ptr;
- }
- ListIterator& operator++() {
- ptr = ptr->getNext(); return
- *this;
- }
- ListIterator& operator++(int v) {
- ptr = ptr->getNext();
- return *this;
- }
- ListIterator& operator=(const ListIterator& it) {
- ptr =
- it.ptr; return *this;
- }
- ListIterator& operator=(Element<ValueType>* p) {
- ptr = p;
- return *this;
- }
- private:
- Element<ValueType>* ptr;
- };
- template <class T>
- class IteratedLinkedList : public LinkedListParent<T>
- {
- public:
- IteratedLinkedList() : LinkedListParent<T>()
- {
- cout << "\nIteratedLinkedList constructor";
- }
- virtual ~IteratedLinkedList()
- {
- cout << "\nIteratedLinkedList destructor";
- }
- ListIterator<T> iterator;
- ListIterator<T> begin()
- {
- ListIterator<T> it = LinkedListParent<T>::head; return it;
- }
- ListIterator<T> end()
- {
- ListIterator<T> it = LinkedListParent<T>::tail; return it;
- }
- };
- template<class T>
- class Queue : public IteratedLinkedList<T>
- {
- public:
- Queue() = default;
- virtual Element<T>* push(T value)
- {
- if (this->head == nullptr)
- {
- this->head = new Element<T>(value);
- if (this->head == 0) throw "MemoryAllocation";
- this->tail = this->head;
- }
- else
- {
- this->tail->next = new Element<T>(value);
- if (this->tail->next == 0) throw "MemoryAllocation";
- this->tail = this->tail->next;
- }
- this->num++;
- return this->tail;
- }
- virtual Element<T>* pop()
- {
- if (this->head == nullptr)
- {
- cout << "Queue is empty";
- return;
- }
- else
- {
- this->head = this->head->next;
- delete this->head->prev;
- this->head->prev = nullptr;
- }
- this->num--;
- return this->head;
- }
- !Queue() = default;
- };
- template<class T>
- class Priority_queue : public Queue<T>
- {
- public:
- Priority_queue() = default;
- Element<T>* push(T value)
- {
- if (this->head == nullptr)
- {
- this->head = new Element<T>(value);
- if (this->head == 0) throw "MemoryAllocation";
- this->tail = this->head;
- }
- else
- {
- Element<T>* iter = this->head;
- for (iter; iter != nullptr; iter = iter->next)
- {
- if (value < iter->field)
- {
- iter->prev = new Element<T>(value, iter, iter->prev);
- if (iter->prev == 0) throw "MemoryAllocation";
- break;
- }
- }
- }
- this->num++;
- return this->tail;
- }
- !Priority_queue() = default;
- };
- class Book
- {
- private:
- string first_name_a;
- string second_name_a;
- string titel;
- string publisher;
- int count;
- int year;
- int paper;
- int electronic;
- int audio;
- public:
- Book(string const f = " ", string const s = " ", string const t = " ",
- string const p = " ", int c = 0, int y = 0, int ta = 0, int tp = 0, int te = 0)
- {
- first_name_a = f;
- second_name_a = s;
- titel = t;
- publisher = p;
- count = c;
- year = y;
- paper = ta;
- electronic = tp;
- audio = te;
- }
- bool operator < (const Book& B) const
- {
- return ((count < B.count) + ((count != B.count) * (year < B.year)) +
- ((count != B.count) * (year != B.year) * (titel < B.titel)));
- }
- bool operator ==(const Book& B) const
- {
- return !(*this < B) && !(B < *this);
- }
- int count_() { return count; }
- int year_() { return year; }
- string titel_() { return titel; }
- void print() const
- {
- cout << "\nName authot: " << first_name_a << "\nSurname author: " << second_name_a <<
- "\nTitel of book: " << titel << "\nCount books: " << count << "\nPublisher: " << publisher <<
- "\nYear of publication: " << year << "\nNumber of paper books: " << paper << "\nNumber of electronic books:" <<
- electronic << "\nNumber of audio books:" << audio << '\n';
- }
- ~Book() = default;
- friend Book fill(Book& V);
- };
- Book fill(Book& V)
- {
- srand(time(NULL));
- string names[5] = { "Alice", "Bob", "Carla", "David", "Eva" };
- string surnames[5] = { "Smith", "Johnson", "Brown", "Davis", "Wilson" };
- string publishers[5] = { "Penguin Random House", "HarperCollins", "Simon & Schuster", "Macmillan Publishers", "Hachette Livre" };
- string books[5] = { "To Kill a Mockingbird", "1984", "The Great Gatsby", "Pride and Prejudice", "The Catcher in the Rye" };
- int i1 = rand() % 5, i2 = rand() % 5, i3 = rand() % 5, i4 = rand() % 5,
- i5 = rand() % 5000, i6 = rand() % 2023, i7 = rand() % 1000, i8 = rand() % 1000, i9 = rand() % 1000;
- V.first_name_a = names[i1];
- V.second_name_a = surnames[i2];
- V.titel = books[i3];
- V.publisher = publishers[i4];
- V.count = i5;
- V.year = i6;
- V.paper = i7;
- V.electronic = i8;
- V.audio = i9;
- return V;
- }
- bool vowels(char c)
- {
- return (c == 'A' || c == 'a' ||
- c == 'E' || c == 'e' ||
- c == 'I' || c == 'i' ||
- c == 'O' || c == 'o' ||
- c == 'U' || c == 'u' ||
- c == 'Y' || c == 'y');
- }
- template <class T>
- list<T> filter(const list<T>& lst, bool(*comp)(T))
- {
- if (comp == nullptr) throw "nullptr";
- list<T> ans;
- for (auto it = lst.begin(); it != lst.end(); ++it)
- {
- if (comp(*it))
- ans.push_back(*it);
- }
- return ans;
- }
- template<class T>
- void push(list<T>& lst, const T& element)
- {
- auto i = lst.begin();
- for (i; i != lst.end(); ++i)
- if (element < *i)
- break;
- lst.insert(i, element);
- }
- template<class T>
- void pop(list<T>& l, typename list<T>::const_iterator it)
- {
- if (l.size() != 0)
- l.erase(it);
- }
- template<class T>
- void print(list<T>& const lst)
- {
- for (auto i = lst.begin(); i != lst.end(); ++i)
- cout << *i << " ";
- cout << '\n';
- }
- int main()
- {
- list<char> lst;
- bool(*ptr)(char) = &vowels;
- for (char i = 'A'; i < 'Z'; ++i)
- lst.push_back(i);
- print(lst);
- cout << '\n';
- list<char> lst2 = filter(lst, ptr);
- print(lst2); cout << '\n';
- list<Book> lst3;
- for (int i = 0; i < 5; ++i)
- {
- Book A;
- fill(A);
- push(lst3, A);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement