0 / 0 / 0
Регистрация: 09.10.2015
Сообщений: 12
|
|
1 | |
List22.10.2015, 21:07. Показов 466. Ответов 0
Метки нет (Все метки)
как продолжать программу ?
Упражнение Тип Бинар List ( Каждый элемент знает его адрес следующий и предыдущий элемент )Придерживайтесь переопределить все основные функции и операторы.Тип элемент должен иметь возможность добавлять, удалять и изменять выбранное значение элемента и потенциальную отдачуВведите возможность учиться и изменить всю длинуИмейте функцию печати из которых все значения //List.cpp #include "Element.cpp" #include <iostream> using namespace std; class List { private: Element *P_first; int length; int realLength; public: List (unsigned int l) { if (l == 0) { cout << "Incorrect value. Used default 5 value. For change List length you can use changeLength function." << endl; l = 5; } length = l; realLength = 0; Element *el = new Element(); P_first = el; for (int i = 0; i < length - 1; ++i) { Element *n = new Element(el, NULL); el->setNext(n); el = n; } } List (const List &obj) { } void printList () { Element *el = P_first; for (int i = 0; i < realLength - 1; ++i) { cout << el->getValue() << ", "; el = el->getNext(); } cout << el->getValue() << endl; } void addElement (int value) { if (realLength == length) { cout << "List is full. For increase List length you can use changeLength function." << endl; return; } Element *el = P_first; for (int i = 0; i < realLength; ++i) { el = el->getNext(); } el->setValue(value); ++realLength; } void setElementValue (unsigned int index, int value) { if (index > realLength - 1) { cout << "Incorrect index of list." << endl; return; } Element *el = P_first; for (int i = 0; i < index; ++i) { el = el->getNext(); } el->setValue(value); } void deleteElement (unsigned int index) { if (index > realLength - 1) { cout << "Incorrect index of list." << endl; return; } Element *el = P_first; for (int i = 0; i < index; ++i) { el = el->getNext(); } Element *last = P_first; for (int i = 0; i < length - 1; ++i) { last = last->getNext(); } el->setValue(0); --realLength; if (index == 0) { P_first = el->getNext(); P_first->setPrevious(NULL); el->setNext(NULL); el->setPrevious(last); return; } if (index == length - 1) { return; } el->getPrevious()->setNext(el->getNext()); el->getNext()->setPrevious(el->getPrevious()); last->setNext(el); el->setPrevious(last); el->setNext(NULL); } int getElementValue(unsigned int index) { if (index > realLength) { cout << "Incorrect index of list." << endl; return 0; } Element *el = P_first; for (int i = 0; i < index; ++i) { el = el->getNext(); } return el->getValue(); } void changeLength(unsigned int l) { if (l == 0) { cout << "Incorrect value." << endl; return; } if (l == length) { cout << "List length already is " << l << " ." << endl; return; } Element *el = P_first; if (l < realLength) { cout << "Warning: New length is less than the elements count, last " << (l - realLength) << " elements will be deleted." << endl; for (int i = 0; i < l; ++i) { el = el->getNext(); } el->getPrevious()->setNext(NULL); for (int i = 0; i < length - l - 1; ++i) { el = el->getNext(); delete el->getPrevious(); } delete el; realLength = l; length = l; return; } for (int i = 0; i < length - 1; ++i) { el = el->getNext(); } for (int i = 0; i < l - length; ++i) { Element *n = new Element(el, NULL); el->setNext(n); el = n; } length = l; } int getLength () { return length; } int getRealLength () { return realLength; } ~List () { Element *el = P_first; for (int i = 0; i < length - 1; ++i) { el = el->getNext(); delete el->getPrevious(); } delete el; } }; // Element.cpp #include <iostream> using namespace std; class Element { private: int value; Element *P_previous; Element *P_next; public: Element (Element *previous = NULL, Element *next = NULL, int v = 0) { P_previous = previous; P_next = next; value = v; } Element (const Element &obj) { P_previous = obj.P_previous; P_next = obj.P_next; value = obj.value; } int getValue () { return value; } Element* getPrevious () { return P_previous; } Element* getNext () { return P_next; } void setValue (int v) { value = v; } void setPrevious (Element *previous) { P_previous = previous; } void setNext (Element *next) { P_next = next; } ~Element () { cout << "Element with " << value << " value deleted" << endl; } };
0
|
22.10.2015, 21:07 | |
Ответы с готовыми решениями:
0
Как пройти по List, отыскать дубликаты и записать их в другой List? Ошибка при компиляции undefined reference to `List<int>::List()'| Invalid operands of types 'List*' and 'List*' to binary 'operator+' Сделать реализацию list (классы node и list) на шаблонах |
22.10.2015, 21:07 | |
22.10.2015, 21:07 | |
Помогаю со студенческими работами здесь
1
у меня созданы 2 фаила - один с классом (называется list.h), а другой с реализациями функций (list.cpp). Записать в файл list (очередь) объектов, в которых содержатся строки string, и считать с файла обратно в list Реализация std::list, сложность list::size() Работа с STL list + пропись List.h undefined reference to `List<int>::List()' Преобразование списка list в *list Искать еще темы с ответами Или воспользуйтесь поиском по форуму: |