Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Having trouble with my c++ homework on how to create these functions. Having tro

ID: 3734015 • Letter: H

Question

Having trouble with my c++ homework on how to create these functions. Having trouble on questions 1 through question 5. Consider the List class in Linked List lecture Question 1 Write a void fromString(const string & word) function to create a List from a string Question 2. Write a string tostring() function to create a List from a string Question 3 Write a List operator+ (const List & rhs) const function to merge two list together and return a new list Question 4 Write the List ) function call deleteList(Node & ptr) which must be done recursively. If the headPtr is not the end, call yourself On headPtr->next and then delete the headPtr Question 5 Write the friend function friend ostream & operator

Explanation / Answer

#include <iostream>

using namespace std;

class Node {
    char ch;
    Node* next;

    Node(char ch) {
        this->ch = ch;
        next = NULL;
    }
};

class List {
    Node* head;
    Node* tail;

    List() {
        head = NULL;
        tail = NULL;
    }

    viod fromString(const string & word) {
        deleteList();
        for ( int i = 0; i < word.length(); i++) {
            if (tail == NULL) {
                head = tail = Node(word[i]);
            else
                tail = tail -> next = Node(word[i]);
            }
        }
    }

    string toString() {
        string result = "";

        Node *temp = head;

        while (temp != NULL) {
            result = result + temp -> ch;
            temp = temp -> next;
        }

        return temp;
    }

    List operator+ (const List & rhs) const {
        List newList;
        newList.fromString(toString());
        Node* temp = rhs.head;

        while (temp != NULL) {
            newList.tail = newList.tail -> next = Node(temp -> ch);
            temp = temp -> next;
        }

        return newList;
    }

    ~List() {
       deleteList(head);        
    }

    friend ostream & operator<< (ostream & out, const List rhs) {
        Node* temp = rhs.head;
        while (temp != NULL) {
            out << temp -> ch;
            temp = temp -> next;
        }
        return out;
    }
  
private:
    deleteList(Node * & ptr) {
        if (head != NULL) {
            deleteList(head -> next);
            delete head;
        }
        else
            tail = NULL;
    }
};


I hope this code will help you answer all your questions!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote