Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.1 kB
0
Indexable
#include <iostream>
#include <string>
using namespace std;

// Node structure for the double-linked list
struct Node {
    string data;
    Node* prev;
    Node* next;
};

// Function to create a new node
Node* createNode(string data) {
    Node* newNode = new Node();
    newNode->data = data;
    newNode->prev = NULL;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the end of the list
void insertNode(Node** head, string data) {
    Node* newNode = createNode(data);
    if (*head == NULL) {
        *head = newNode;
    }
    else {
        Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->prev = temp;
    }
}

// Function to display the list
void displayList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        cout << "Address: " << temp << endl;
        cout << "Information Part: " << temp->data << endl;
        cout << "Address Part: " << temp->next << endl;
        cout << endl;
        temp = temp->next;
    }
}

Node* rewriteList(Node* head) {
    Node* temp = head;
    Node* uppercaseHead = nullptr;
    Node* uppercaseTail = nullptr;
    bool foundSecondUppercase = false;

    // First loop: Write all uppercase letters
    while (temp != nullptr) {
        if (isupper(temp->data[0])) {
            Node* newNode = createNode(temp->data);
            if (uppercaseHead == nullptr) {
                uppercaseHead = newNode;
                uppercaseTail = newNode;
            }
            else {
                uppercaseTail->next = newNode;
                newNode->prev = uppercaseTail;
                uppercaseTail = newNode;
            }
        }
        temp = temp->next;
    }

    temp = head;
    // Second loop: Delete first element and write nodes before the second uppercase letter
    while (temp != nullptr) {
        if (isupper(temp->data[0])) {
            if (foundSecondUppercase) {
                break;
            }
            foundSecondUppercase = true;
            Node* nextNode = temp->next;
            delete temp;
            temp = nextNode;
            continue;
        }
        Node* newNode = createNode(temp->data);
        newNode->prev = uppercaseTail;
        uppercaseTail->next = newNode;
        uppercaseTail = newNode;
        temp = temp->next;
    }

    return uppercaseHead;
}

int main() {
    Node* listHead = NULL;

    // Enter names letter by letter
    string input;
    cout << "Enter your surname, first name, and patronymic letter by letter (separated by spaces):" << endl;
    cout << "Type 'done' to finish entering names." << endl;

    while (true) {
        cin >> input;
        if (input == "done") {
            break;
        }
        insertNode(&listHead, input);
    }

    cout << endl << "Original List:" << endl;
    displayList(listHead);

    cout << endl << "Converted List:" << endl;
    Node* newHead = rewriteList(listHead);
    displayList(newHead);

    return 0;
}