Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <windows.h>
- using namespace std;
- struct Node {
- int value;
- Node* next;
- Node(int val) : value(val), next(nullptr) {}
- };
- class TList {
- private:
- string name;
- Node* head;
- int count;
- public:
- // конструктор без параметров
- TList() : name("Unnamed List"), head(nullptr), count(0) {}
- // конструктор с параметрами
- TList(const string& listName) : name(listName), head(nullptr), count(0) {}
- // конструктор копирования
- TList(const TList& other) : name(other.name), head(nullptr), count(other.count) {
- if (other.head) {
- head = new Node(other.head->value);
- Node* srcNode = other.head->next;
- Node* destNode = head;
- while (srcNode) {
- destNode->next = new Node(srcNode->value);
- destNode = destNode->next;
- srcNode = srcNode->next;
- }
- }
- }
- // деструктор
- ~TList() {
- clear();
- }
- // распечатка элементов списка на экран
- void print() const {
- Node* current = head;
- cout << "Список (" << name << "): ";
- while (current) {
- cout << current->value << " ";
- current = current->next;
- }
- cout << endl;
- }
- // распечатка элемента списка с заданным номером на экран
- void printElement(int index) const {
- if (index <= 0 || index > count) {
- cout << "Такого индекса нет" << endl;
- return;
- }
- Node* current = head;
- for (int i = 0; (i+1) < index; i++) {
- current = current->next;
- }
- cout << "Элемент под номером " << index << ": " << current->value<< endl;
- }
- // выдача количества элементов списка
- int getCount() const {
- return count;
- }
- // добавление элемента в конец списка
- void append(int value) {
- Node* newNode = new Node(value);
- if (!head) {
- head = newNode;
- }
- else {
- Node* current = head;
- while (current->next) {
- current = current->next;
- }
- current->next = newNode;
- }
- ++count;
- }
- // удаление элемента с начала списка
- void removeFirst() {
- if (!head) {
- cout << "Список пуст" << endl;
- return;
- }
- Node* temp = head;
- head = head->next;
- delete temp;
- --count;
- }
- // очистка всего списка
- void clear() {
- while (head) {
- removeFirst();
- }
- }
- //добавление к данному списку второго списка
- void appendList(const TList& other) {
- Node* current = other.head;
- while (current) {
- append(current->value);
- current = current->next;
- }
- }
- // сравнение количества элементов данного списка с другим списком
- string compareCount(const TList& other) const {
- if (count > other.count) {
- return "больше";
- }
- else if (count < other.count) {
- return "меньше";
- }
- else {
- return "равен";
- }
- }
- TList& operator =(const TList& list);
- string operator ==(const TList& list); //+
- friend std::ostream& operator << (std::ostream &os, const TList &tl);
- friend std::istream& operator >> (std::istream &in,TList &tl);
- TList& operator ++(int); //+
- TList& operator --(); //+
- bool operator >(const TList& list); //+
- TList operator+(const TList& list);
- TList& operator+=(const TList& list); //+
- TList operator-(const TList& list);
- };
- TList& TList::operator =(const TList& list)
- {
- TList temp;
- temp.name = list.name;
- temp.head = list.head;
- temp.count = list.count;
- return temp;
- }
- std::ostream& operator << (std::ostream &os, const TList &tl){
- // ts.
- Node* x = tl.head;
- while(x != nullptr){
- os << x->value;
- x = x->next;
- }
- return os << endl;
- }
- std::istream& operator >> (std::istream &in,TList &tl){
- cout << "кол-во элементов:\n";
- int n;
- in >> n;
- // Node* x = tl.head;
- tl.count = n;
- Node* head;
- cout << "элементы:\n";
- for(int i = 0; i < n; i++){
- // спиздил с append'а
- int value;
- in >> value;
- Node* newNode = new Node(value);
- if (!head) {
- head = newNode;
- }
- else {
- Node* current = head;
- while (current->next) {
- current = current->next;
- }
- current->next = newNode;
- }
- }
- return in;
- }
- string TList::operator ==(const TList& list)
- {
- return compareCount(list);
- }
- TList& TList::operator ++(int)
- {
- append(1);
- return *this;
- };
- TList& TList::operator --()
- {
- removeFirst();
- return *this;
- };
- bool TList::operator>(const TList& list) {
- if (count > list.count)
- return 0;
- return 1;
- }
- TList& TList::operator+=(const TList& list)
- {
- appendList(list);
- return *this;
- }
- TList TList::operator+(const TList& list) {
- TList result;
- result.name = name + " + " + list.name;
- // Объединяем узлы двух списков
- Node* current1 = head;
- while (current1 != nullptr) {
- result.append(current1->value);
- current1 = current1->next;
- }
- Node* current2 = list.head;
- while (current2 != nullptr) {
- result.append(current2->value);
- current2 = current2->next;
- }
- return result;
- }
- TList TList::operator-(const TList& other) {
- TList result;
- result.name = name + " - " + other.name;
- // Добавляем в результат узлы первого списка
- Node* current1 = head;
- while (current1 != nullptr) {
- result.append(current1->value);
- current1 = current1->next;
- }
- // Удаляем из результата узлы, которые есть во втором списке
- Node* current2 = other.head;
- while (current2 != nullptr) {
- result.remove(current2->value);
- current2 = current2->next;
- }
- return result;
- }
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- TList list1("List1");
- list1++; //добавление элемента в конец списка
- list1.append(60);
- list1.print();
- TList list2("List2");
- list2.append(40);
- list2.append(50);
- list2.print();
- TList list3 = list1;
- list3.print();
- --list1; //уменьшение количества элементов на 1
- cout << "Список после удаления первого элемента: " << endl;
- list1.print();
- list1+=list2; //добавление к первому списку значений второго списка
- cout << "Cписок после добавления к нему второго: " << endl;
- list1.print();
- cout << "list1 count: " << list1.getCount() << endl;
- cout << "list2 count: " << list2.getCount() << endl;
- cout << "list1 > list2? " << (list2 > list1) << endl; //проверка на то, что количество элементов данного
- //списка больше количества элементов другого списка,
- cout << "list1 " << (list1 == list2) << " list2" << endl; //проверка на равенство двух списков
- list1.printElement(1);
- cout << (list1 + list2) << endl;
- list1.clear();
- list1.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment