Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- template <typename T>
- class ListNode {
- public:
- T data;
- ListNode<T>* Prev;
- ListNode<T>* pNext;
- ListNode(T i, ListNode<T>* prev, ListNode<T>* nxt) : data(i), Prev(prev), pNext(nxt) {}
- };
- template <typename T>
- class List {
- private:
- ListNode<T>* first, * current, * last;
- int size;
- int sizeMax;
- ListNode<T>* findAt(int p);
- public:
- List();
- ~List();
- void addAtFront(T d);
- void addAtEnd(T d);
- void addAt(int p, T i);
- T* removeAtFront();
- T* removeAtEnd();
- T* removeAt(int p);
- void Show();
- void demo();
- void sortListA();
- void SaveBinaryFile();
- void LoadBinaryFile();
- T* getAt(int p);
- T* getAtFront();
- T* getAtEnd();
- int getSizeMaxList();
- void setSizeMaxList(int s);
- int getSizeList();
- void setAtStart();
- void setAtEnd();
- T* getNext();
- T* getPrevious();
- };
- template <typename T>
- List<T>::List() : first(nullptr), current(nullptr), last(nullptr), size(0), sizeMax(8) {}
- template <typename T>
- List<T>::~List() {
- ListNode<T>* temp = first;
- while (temp != nullptr) {
- ListNode<T>* n = temp->pNext;
- delete temp;
- temp = n;
- }
- }
- template <typename T>
- void List<T>::addAtFront(T d) {
- if (size != sizeMax) {
- ListNode<T>* temp = new ListNode<T>(d, nullptr, first);
- if (first != nullptr) {
- first->Prev = temp;
- }
- first = temp;
- if (last == nullptr) {
- last = temp;
- size++;
- }
- else {
- cout << "Error 404: Место в списке ограничено" << endl;
- }
- }
- }
- template <typename T>
- void List<T>::addAtEnd(T d) {
- if (size != sizeMax) {
- if (size == 0) {
- addAtFront(d);
- }
- else {
- ListNode<T>* temp = new ListNode<T>(d, last, nullptr);
- last->pNext = temp;
- last = temp;
- size++;
- }
- }
- else {
- cout << "Error 404: Место в списке ограничено" << endl;
- }
- }
- template <typename T>
- void List<T>::addAt(int p, T i) {
- if (size != sizeMax) {
- if (p == 0) {
- addAtFront(i);
- }
- else if (p == size) {
- addAtEnd(i);
- }
- else {
- ListNode<T>* temp = findAt(p);
- if (temp != nullptr) {
- ListNode<T>* l = new ListNode<T>(i, temp->Prev, temp);
- temp->Prev->pNext = l;
- temp->Prev = l;
- size++;
- }
- }
- }
- else {
- cout << "Error 404: Место в списке ограничено" << endl;
- }
- }
- template <typename T>
- T* List<T>::removeAtFront() {
- if (size == 0) {
- cout << "No item to be removed at front of list" << endl;
- return nullptr;
- }
- T temp_data = first->data;
- ListNode<T>* temp = first;
- if (first->pNext != nullptr) {
- first->pNext->Prev = nullptr;
- }
- first = first->pNext;
- size--;
- if (size == 0) {
- last = nullptr;
- }
- delete temp;
- return new T(temp_data);
- }
- template <typename T>
- T* List<T>::removeAtEnd() {
- if (size == 0) {
- cout << "No item to be removed at end of list" << endl;
- return nullptr;
- }
- T temp_data = last->data;
- ListNode<T>* temp = last;
- if (last->Prev != nullptr) {
- last->Prev->pNext = nullptr;
- }
- last = last->Prev;
- size--;
- if (size == 0) {
- first = nullptr;
- }
- delete temp;
- return new T(temp_data);
- }
- template <typename T>
- T* List<T>::removeAt(int p) {
- if (p < 0 || p >= size) {
- cout << "No item to be removed at list position " << p << endl;
- return nullptr;
- }
- if (p == 0) {
- return removeAtFront();
- }
- if (p == size - 1) {
- return removeAtEnd();
- }
- ListNode<T>* temp = findAt(p);
- T temp_data = temp->data;
- temp->Prev->pNext = temp->pNext;
- temp->pNext->Prev = temp->Prev;
- size--;
- delete temp;
- return new T(temp_data);
- }
- template <typename T>
- void List<T>::Show() {
- ListNode<T>* temp = first;
- while (temp != nullptr) {
- cout << temp->data << " - ";
- temp = temp->pNext;
- }
- cout << "End" << endl;
- }
- template <typename T>
- void List<T>::demo() {
- ListNode<T>* temp = last;
- while (temp != nullptr) {
- cout << temp->data << " - ";
- temp = temp->Prev;
- }
- cout << "End" << endl;
- }
- template <typename T>
- ListNode<T>* List<T>::findAt(int p) {
- ListNode<T>* temp = first;
- for (int i = 0; i < p && temp != nullptr; i++) {
- temp = temp->pNext;
- }
- return temp;
- }
- template <typename T>
- void List<T>::sortListA() {
- ListNode <T>* t, * m, * a, * b;
- if (first == nullptr) {
- return;
- }
- for (bool go = true; go;) {
- go = false;
- a = t = first;
- b = first->pNext;
- while (b != nullptr) {
- if (a->data > b->data) {
- if (t == a) {
- first = b;
- }
- else
- t->pNext = b;
- a->pNext = b->pNext;
- b->pNext = a;
- m = a, a = b, b = m;
- go = true;
- }
- t = a;
- a = a->pNext;
- b = b->pNext;
- }
- }
- }
- template <typename T>
- void List<T>::SaveBinaryFile() {
- ofstream fileOut("file.dat", ios::binary);
- if (!fileOut) {
- cout << "Ошибка открытия файла";
- }
- else {
- ListNode<T>* a = first;
- while (a != nullptr) {
- cout << ">> " << a->data << endl;
- fileOut.write((char*)&a->data, sizeof(T));
- a = a->pNext;
- }
- fileOut.close();
- }
- }
- template <typename T>
- void List<T>::LoadBinaryFile() {
- ifstream fileIn("file.dat", ios::binary);
- if (!fileIn) {
- cout << "Ошибка открытия файла";
- }
- else {
- ListNode<T>* a = first;
- int tempSize = 0;
- while (a != nullptr) {
- fileIn.read((char*)&a->data, sizeof(T));
- cout << ">> " << a->data << endl;
- a = a->pNext;
- tempSize++;
- }
- fileIn.close();
- this->size = tempSize;
- cout << "SIZE = " << this->size << endl;
- }
- }
- template <typename T>
- T* List<T>::getAt(int p) {
- ListNode<T>* temp = findAt(p);
- return (temp != nullptr) ? &temp->data : nullptr;
- }
- template <typename T>
- T* List<T>::getAtFront() {
- return (first != nullptr) ? &first->data : nullptr;
- }
- template <typename T>
- T* List<T>::getAtEnd() {
- return (last != nullptr) ? &last->data : nullptr;
- }
- template <typename T>
- int List<T>::getSizeMaxList() {
- return this->sizeMax;
- }
- template <typename T>
- void List<T>::setSizeMaxList(int s) {
- this->sizeMax = s;
- }
- template <typename T>
- int List<T>::getSizeList() {
- return this->size;
- }
- template <typename T>
- void List<T>::setAtStart() {
- current = first;
- }
- template <typename T>
- void List<T>::setAtEnd() {
- current = last;
- }
- template <typename T>
- T* List<T>::getNext() {
- if (current != nullptr) {
- T temp_data = current->data;
- current = current->pNext;
- return new T(temp_data);
- }
- return nullptr;
- }
- template <typename T>
- T* List<T>::getPrevious() {
- if (current != nullptr) {
- T temp_data = current->data;
- current = current->Prev;
- return new T(temp_data);
- }
- return nullptr;
- }
- template <typename T>
- class Array {
- private:
- List<T>* arr;
- int arrsize;
- int arrsizeMax;
- public:
- Array();
- ~Array();
- int getSize();
- int getSizeMax();
- void setSize(int s);
- void setSizeMax(int s);
- void Push(T value);
- void ClearArr();
- void removeEndList();
- void Show();
- void SaveFile();
- void LoadFile();
- void SaveBinaryFile();
- void LoadBinaryFile();
- };
- template <typename T>
- Array<T>::Array() : arrsize(0), arrsizeMax(3), arr(new List<T>[arrsizeMax]) {}
- template <typename T>
- Array<T>::~Array() {
- delete[] arr;
- }
- template <typename T>
- int Array<T>::getSize() {
- return this->arrsize;
- }
- template <typename T>
- int Array<T>::getSizeMax() {
- return this->arrsizeMax;
- }
- template <typename T>
- void Array<T>::setSize(int s) {
- this->arrsize = s;
- }
- template <typename T>
- void Array<T>::setSizeMax(int s) {
- if (s < arrsize) {
- cout << "Max не может быть меньше текущего количества" << endl;
- }
- else {
- this->arrsizeMax = s;
- }
- }
- template <typename T>
- void Array<T>::Push(T value) {
- if (arrsize == arrsizeMax) {
- cout << "Массив полон" << endl;
- }
- else {
- if (this->arr[arrsize].getSizeList() == this->arr[arrsize].getSizeMaxList()) {
- arrsize++;
- this->arr[arrsize].addAtFront(value);
- }
- else {
- this->arr[arrsize].addAtEnd(value);
- }
- }
- }
- template <typename T>
- void Array<T>::removeEndList() {
- if (arrsize > 0) {
- arr[arrsize].removeAtEnd();
- arrsize--;
- }
- }
- template <typename T>
- void Array<T>::ClearArr() {
- if (arrsize == 0) {
- arr[0].~List();
- }
- else {
- arr[arrsize].~List();
- arrsize--;
- }
- }
- template <typename T>
- void Array<T>::Show() {
- for (int i = 0; i <= arrsize; i++) {
- cout << "Array " << i << ": ";
- arr[i].Show();
- }
- }
- template <typename T>
- void Array<T>::SaveFile() {
- ofstream fileOut("array_file.dat");
- if (!fileOut) {
- cout << "Ошибка открытия файла";
- }
- else {
- for (int i = 0; i <= arrsize; i++) {
- ListNode<T>* a = arr[i].getAtFront();
- while (a != nullptr) {
- cout << ">> " << a->data << endl;
- fileOut << a->data << " ";
- a = a->pNext;
- }
- }
- fileOut.close();
- }
- }
- template <typename T>
- void Array<T>::LoadFile() {
- ifstream fileIn("array_file.dat");
- if (!fileIn) {
- cout << "Ошибка открытия файла";
- }
- else {
- for (int i = 0; i <= arrsize; i++) {
- ListNode<T>* a = arr[i].getAtFront();
- int tempSize = 0;
- while (a != nullptr) {
- fileIn >> a->data;
- cout << ">> " << a->data << endl;
- a = a->pNext;
- tempSize++;
- }
- arr[i].setSize(tempSize);
- }
- fileIn.close();
- }
- }
- template <typename T>
- void Array<T>::SaveBinaryFile() {
- ofstream fileOut("array_file.dat", ios::binary);
- if (!fileOut) {
- cout << "Ошибка открытия файла";
- }
- else {
- for (int i = 0; i <= arrsize; i++) {
- ListNode<T>* a = arr[i].getAtFront();
- while (a != nullptr) {
- cout << ">> " << a->data << endl;
- fileOut.write((char*)&a->data, sizeof(T));
- a = a->pNext;
- }
- }
- fileOut.close();
- }
- }
- template <typename T>
- void Array<T>::LoadBinaryFile() {
- ifstream fileIn("array_file.dat", ios::binary);
- if (!fileIn) {
- cout << "Ошибка открытия файла" << endl;
- }
- else {
- for (int i = 0; i <= arrsize; i++) {
- ListNode<T>* a = arr[i].getAtFront();
- int tempSize = 0;
- while (a != nullptr) {
- fileIn.read(reinterpret_cast<char*>(&a->data), sizeof(T));
- cout << ">> " << a->data << endl;
- a = a->pNext;
- tempSize++; // исправлена ошибка
- }
- arr[i].setSize(tempSize);
- }
- fileIn.close();
- }
- }
- template <typename T>
- void Test(T value) {
- List<T> myList;
- myList.addAtEnd(value);
- myList.Show();
- }
- template <typename T>
- void Menu2() {
- cout << "1. Тестировать с int\n";
- cout << "2. Тестировать с char\n";
- cout << "3. Тестировать с float\n";
- cout << "4. Тестировать с double\n";
- }
- int main() {
- Array<int> intArray; // Используйте свой шаблонный класс Array с типом int
- int i = 0;
- char c = 'c';
- float f = 0;
- double d = 0;
- int veb;
- for (;;) {
- cout << "Выбор работы с шаблонами" << endl;
- Menu2<int>(); // Вызовем Menu2 с типом int. Вы можете изменить тип по своему усмотрению.
- cout << ">>";
- cin >> veb;
- if (veb == 1) {
- Test(i);
- system("pause");
- return 0;
- }
- else if (veb == 2) {
- Test(c);
- system("pause");
- return 0;
- }
- else if (veb == 3) {
- Test(f);
- system("pause");
- return 0;
- }
- else if (veb == 4) {
- Test(d);
- system("pause");
- return 0;
- }
- else {
- std::cout << "Ошибка" << std::endl;
- }
- }
- switch (veb) {
- case 'a':
- system("cls");
- int valA;
- cout << "Введите элемент" << endl;
- cout << ">>";
- cin >> valA;
- intArray.Push(valA);
- break;
- case 'w':
- system("cls");
- intArray.Show();
- break;
- case 'b':
- system("cls");
- cout << "Сколько хотите ввести элементов?" << endl;
- int numB;
- cout << ">>";
- cin >> numB;
- int valB;
- cout << "Ввод:" << endl;
- for (int t = 0; t < numB; t++) {
- cout << ">>";
- cin >> valB;
- intArray.Push(valB);
- }
- break;
- case 'v':
- system("cls");
- intArray.removeEndList();
- cout << "Удален последний элемент" << endl;
- break;
- case 'c':
- system("cls");
- cout << "Очистка последнего элемента" << endl;
- intArray.ClearArr();
- break;
- case 'x':
- system("cls");
- intArray.SaveBinaryFile();
- break;
- case 'z':
- system("cls");
- intArray.LoadBinaryFile();
- break;
- case 'r':
- system("cls");
- system("pause");
- return 0;
- case 'e':
- system("cls");
- break;
- default:
- cout << "Неверный выбор" << endl;
- break;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement