Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "List.h"
- template <typename Type>
- ref class CyclicDoubleList : public List<Type>
- {
- template <typename Type>
- ref struct Node
- {
- Type data;
- Node^ next = nullptr;
- Node^ prev = nullptr;
- };
- public:
- CyclicDoubleList();
- ~CyclicDoubleList();
- void Print(System::Windows::Forms::ListBox^ listBoxForPrint) override; // печать списка
- void InsertToBegin(Type) override; // вставка в начало
- void Insert(Type, int) override; // вставка после элемента с указанным индексом
- void Delete(int) override; // удаление элемента с указанным индексом
- void Clear() override; // удаление всего списка
- int Find(Type) override; // поиск элемента
- Type At(int) override; // возвращает элемент с указанным индексом
- bool IsEmpty() override; // проверка списка на пустоту
- int Size() override; // возвращает размер списка
- protected:
- void CreateFirst(Type) override; // создать первый элемент списка
- Node<Type>^ head = nullptr; // указатель на начало списка
- Node<Type>^ current = nullptr; // указатель на текущий элемент списка
- };
- // ####################################### Реализация #######################################
- // конструктор по умолчанию
- template<typename Type>
- CyclicDoubleList<Type>::CyclicDoubleList()
- {
- head = nullptr;
- }
- // деструктор
- template<typename Type>
- inline CyclicDoubleList<Type>::~CyclicDoubleList()
- {
- if (this->IsEmpty())
- return;
- this->Clear();
- }
- // печать списка
- template<typename Type>
- void CyclicDoubleList<Type>::Print(System::Windows::Forms::ListBox^ listBoxForPrint)
- {
- listBoxForPrint->ClearSelected();
- listBoxForPrint->Items->Clear();
- for (int i = 0; i != size; ++i)
- listBoxForPrint->Items->Add(this->At(i));
- }
- // вставка в начало
- template<typename Type>
- inline void CyclicDoubleList<Type>::InsertToBegin(Type value)
- {
- if (this->IsEmpty())
- this->CreateFirst(value);
- else
- {
- Node<Type>^ tmp = gcnew Node<Type>;
- tmp->data = value;
- tmp->next = head;
- tmp->prev = head->prev;
- tmp->prev->next = tmp;
- head->prev = tmp;
- head = tmp;
- this->size++;
- }
- }
- // вставка после элемента с указанным индексом
- template<typename Type>
- inline void CyclicDoubleList<Type>::Insert(Type value, int index)
- {
- if (this->IsEmpty())
- throw gcnew System::Exception("List is empty");
- if (index >= size)
- throw gcnew System::IndexOutOfRangeException("Can not insert element");
- current = head;
- for (int i = 0; i != index + 1; ++i)
- {
- if (i == index)
- {
- Node<Type>^ tmp = gcnew Node<Type>;
- tmp->data = value;
- tmp->next = current->next;
- tmp->prev = current;
- tmp->next->prev = tmp;
- current->next = tmp;
- this->size++;
- }
- current = current->next;
- }
- }
- // удаление элемента с указанным индексом
- template<typename Type>
- inline void CyclicDoubleList<Type>::Delete(int index)
- {
- if (this->IsEmpty())
- throw gcnew System::Exception("List is empty");
- if (index >= size)
- throw gcnew System::IndexOutOfRangeException("Can not delete list element");
- current = head;
- if (index == 0 && size == 1)
- this->Clear();
- else if (index == 0)
- {
- current = head;
- head->next->prev = head->prev;
- head->prev->next = head->next;
- head = current->next;
- delete current;
- --size;
- }
- else if (index < size)
- {
- current = head->next;
- for (int i = 1; i != index; ++i)
- current = current->next;
- current->prev->next = current->next;
- current->next->prev = current->prev;
- delete current;
- --size;
- }
- }
- // удаление всего списка
- template<typename Type>
- inline void CyclicDoubleList<Type>::Clear()
- {
- if (this->IsEmpty())
- return;
- if (size == 1)
- {
- delete head;
- head = nullptr;
- size = 0;
- return;
- }
- int n = size;
- size = 0;
- for (int i = 1; i != n; ++i)
- this->Delete(i);
- this->Delete(0);
- head = nullptr;
- }
- // поиск элемента
- template<typename Type>
- inline int CyclicDoubleList<Type>::Find(Type dataToFind)
- {
- if (this->IsEmpty())
- throw gcnew System::Exception("List is empty");
- if (head->data == dataToFind)
- return 0;
- current = head->next;
- int index = 1;
- while (current != head)
- {
- if (current->data == dataToFind)
- return index;
- ++index;
- current = current->next;
- }
- return -1;
- }
- // возвращает элемент с указанным индексом
- template<typename Type>
- inline Type CyclicDoubleList<Type>::At(int index)
- {
- if (this->IsEmpty())
- throw gcnew System::Exception("List is empty");
- if (index >= size)
- throw gcnew System::IndexOutOfRangeException("Can not get list element");
- if (index == 0)
- return head->data;
- else if (index == size - 1)
- return head->prev->data;
- else
- {
- current = head->next;
- for (int i = 1; i != index + 1; ++i)
- {
- if (i == index)
- return current->data;
- current = current->next;
- }
- }
- }
- // проверка списка на пустоту
- template<typename Type>
- inline bool CyclicDoubleList<Type>::IsEmpty()
- {
- if (head == nullptr)
- return true;
- return false;
- }
- // возвращает размер списка
- template<typename Type>
- inline int CyclicDoubleList<Type>::Size()
- {
- return this->size;
- }
- // создать первый элемент списка
- template<typename Type>
- inline void CyclicDoubleList<Type>::CreateFirst(Type value)
- {
- this->head = gcnew Node<Type>;
- this->head->data = value;
- this->head->next = head;
- this->head->prev = head;
- this->size++;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement