Exemple #1
0
void LinkedList::insert(int position, int value)
{
    ListElement * le = head;
    while(le->getNext() != nullptr && position > 0)
    {
        position--;
        le = le->getNext();
    }

    if (position != 0)
    {
        std::cerr << "Error. The index is more then the length";
        deleteList();
        exit(0);
    }

    ListElement * newListEl = new ListElement();
    newListEl->takeValue(value);
    newListEl->takeNext(le->getNext());
    le->takeNext(newListEl);
}
Exemple #2
0
void LinkedList::deleteElement(int position)
{
    ListElement * le = head;
    while(le->getNext() != nullptr && position > 0)
    {
        position--;
        le = le->getNext();
    }

    if (le->getNext() == nullptr || position > 0)
    {
        std::cerr << "Error. The index is no element with this index";
        deleteList();
        exit(0);
    }

    ListElement * newListEl = le->getNext();
    le->takeNext(le->getNext()->getNext());
    delete newListEl;
}