Untitled

 avatar
unknown
c_cpp
2 years ago
1.8 kB
5
Indexable
// 3 5 7 1 4 2 --> NULL
// 5 7 1 4 2 3 --> NULL
void SinglyLinkedList::ShiftLeft() {
    Node *futureHead = head, *curr = head->next, *currHead = head->next;
    while(curr != nullptr) {
        if (curr == nullptr) {
            curr = futureHead;
            futureHead->next = nullptr;
        }
        curr = curr->next;
    }
    delete head;
    head = currHead;
}

// 2 5 -5 7 {3} 6 9 2 -7 9 --> NULL
int SinglyLinkedList::DelMinBetweenFNegLEven() {
    Node *currNeg = head, *firstNeg = nullptr;
    int posFirstNeg = 0;

    while (currNeg != nullptr) {
        ++posFirstNeg;
        if (currNeg->data < 0) {
            firstNeg = currNeg->next;
            break;
        }
        currNeg = currNeg->next;
    }

    if (firstNeg == nullptr)
        return 0;

    Node *currEven = head, *lastEven = nullptr;
    int posLastEven = 0;

    while (currEven != nullptr) {
        ++posLastEven;
        if (currEven->data % 2 == 0)
            lastEven = currEven;
        currEven = currEven->next;
    }

    if (lastEven == nullptr)
        return 0;

    if ((posFirstNeg - posLastEven == 1) || (posLastEven = posFirstNeg == 1))
        return 0;

    if (posLastEven < posFirstNeg) {
        Node *tmp = lastEven;
        lastEven = firstNeg;
        firstNeg = tmp;
    }
    qDebug() << "111111111111";

    Node *currMin = firstNeg;
    int min = firstNeg->data;

    while (firstNeg != lastEven) {
        if (firstNeg->data < min )
            currMin = firstNeg;
        firstNeg = firstNeg->next;
    }

    qDebug() << currMin->data;

    // 2 5 -5 7 {3} 6 9 2 -7 9 --> NULL
    while (firstNeg != currMin) {
        if (firstNeg == currMin) {
            firstNeg = currMin->next;
            delete  currMin;
        }
        firstNeg = firstNeg->next;
    }

    return 1;
}
Editor is loading...