Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- template <typename T>
- class List
- {
- struct Item
- {
- T inf;
- Item* next;
- Item(T x, Item* ref = nullptr) : inf(x), next(ref) {}
- } *first = nullptr, *last = nullptr;
- public:
- List() = default;
- // конструктор копирования
- List(const List &other)
- {
- for (Item *t = other.first; t != nullptr; t = t->next)
- {
- this->add(t->inf);
- }
- }
- void add(T x)
- {
- Item *item = new Item(x);
- if (last != nullptr)
- last->next = item;
- last = item;
- if (first == nullptr)
- first = last;
- }
- void print()
- {
- for (Item* t = first; t != nullptr; t = t->next)
- cout << t->inf << " ";
- cout << endl;
- }
- void doubleOdd()
- {
- for (Item* t = first; t != nullptr; t = t->next)
- {
- if (t->inf % 2 == 0)
- {
- Item *newItem = new Item(t->inf, t->next);
- t->next = newItem;
- t = t->next;
- }
- }
- }
- // создание нового списка, в котором удалены элементы, следующие после x
- List getListDelAfterX(T x)
- {
- List *newList = new List();
- if (first != nullptr)
- {
- newList->add(first->inf);
- for (Item *t = first; t -> next != nullptr; t = t -> next)
- {
- if (t->inf != x)
- {
- newList->add(t->next->inf);
- }
- }
- }
- return *newList;
- }
- // перенос списка в поток вывода
- friend ostream& operator<<(ostream& out, const List<T> &lst)
- {
- for (Item* t = lst.first; t->next != nullptr; t = t->next)
- {
- out << t->inf << " -> ";
- }
- if (lst.last != nullptr)
- {
- out << lst.last->inf;
- }
- return out;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement