LinkedList sorting a-z

  append, c++, class, project, structure

Requirements for this function: If the full name (both the first and last name) is not equal to any full name currently in the list then add it and return true. Elements should be added according to their last name. Elements with the same last name should be added according to
their first names. Otherwise, make no change to the list and return false (indicating that the name is already in the list).

    //this function add nodes to the list
    //return true if fullname isn't in the list. Else return false if fullname is in the list.
    //should be added according to last name.
    bool OnlineDating::makeMatch(const std::string& firstName, const std::string& lastName, const OnlineType& value) {
    Node* p = head;
    //are these nodes already set to firstName and lastName in this function 
    Node first;
    Node last;
    Node* temp = nullptr;
    //if the list is empty just insert the fullname and value to the list
    if (p == nullptr) {
        //add values to the empty list
        insertToRear(firstName, lastName, value);
        return true;
    }
    else {
        // so this loop is to check if fullname is in the list but first sort in alphebetial order
        //sure its added in alphebetical order
    //traverse the list after knowing where head is 
    while (p != nullptr) {



        //checking to make sure theres at least another node in the list
        if (p->next != nullptr) {
            //its not going through ig loop?
            //these are used to check and alphebetically selected names
            if (p->last > p->next->last) {
                insertToRear(p->first, p->last, p->value);
                p->next = temp;

                return true;
            }
            else if (p->next->last > p->last) {
                insertToRear(p->first, p->last, p->value);
                p->next = temp;

                return true;
            }
            //check if full name is already in the list
            if (p->last == p->next->last) {
                insertToRear(p->first, p->last, p->value);
                p->next = temp;

                return true;

            }
            else if (p->first > p->next->first) {
                insertToRear(p->first, p->last, p->value);
                p->next = temp;

                return true;
            }
            else {
                //returns false if it passes through these  checks
                return false;
            }



        }
        p = p->next;
    }
    }
        
     
}

Here is my main.cpp

int main() {

OnlineDating clippersGonnaClip;
clippersGonnaClip.makeMatch("Kawhi", "Leonard", 2);
clippersGonnaClip.makeMatch("Paul", "George", 13);
clippersGonnaClip.makeMatch("Ivica", "Zubac", 40);
clippersGonnaClip.makeMatch("Reggie", "Jackson", 1);
clippersGonnaClip.makeMatch("Patrick", "Beverley", 21);
for (int n = 0; n < clippersGonnaClip.howManyMatches(); n++) {
    string first;
    string last;
    int val;
    clippersGonnaClip.confirmMatch(n, first, last, val);
    cout << first << " " << last << " " << val << endl;
}

return 0;

}

honestly, I just want to create a ptr that will point to each node. Check as long as that node isn’t pointing to nullptr, and sort the LinkedList in alphabetical order. finally link the nodes together using my *temp. Why won’t it let me go through the if statements every time I compile I get a negative number? Every other function works for this program except this makeMatch(…).

Source: Windows Questions C++

LEAVE A COMMENT