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

C++ How do I start this? Using an appropriate definition of ListNode, design a s

ID: 3673935 • Letter: C

Question

C++

How do I start this?

Using an appropriate definition of ListNode, design a simple linked list class called StringList with the following member functions:

void add (std::string);

int positionOf (std::string);

bool setNodeVal(int, std::string);

std::vector<std::string> getAsVector();

a default constructor

a copy constructor

a destructor

The add() function adds a new node containing the value of the parameter to the end of the list. The positionOf() function returns the (zero-based) position in the list for the first occurrence of the parameter in the list, or -1 if that value is not in the list. In the setNodeVal() function, the first parameter represents a (zero-based) position in the list. The setNodeVal() function sets the value of the node at that position to the value of the string parameter. If the position parameter is >= the number of nodes in the list, the operation cannot be carried out and setNodeVal() should return false, otherwise it should be successful and return true. The getAsVector() function returns a vector with the same size, values and order as the StringList. The default constructor should initialize a new empty StringList object. The copy constructor should create a completely separate duplicate of a StringList object (a deep copy). The destructor should delete any memory that was dynamically allocated by the StringList object.

Files must be called: StringList.hpp and StringList.cpp

Explanation / Answer

StringList.hpp
#ifndef STRING_LIST
#define STRING_LIST
#include <string>
#include <vector>

class StringList
{
private:
    struct ListNode
    {
        std::string parameter;
        ListNode *next;
        ListNode(std::string parameter1, ListNode *next1 = NULL)
        {
            parameter = parameter1;
            next = next1;
        }
    };
  
   ListNode *head;
  
public:
    StringList();
    ~StringList();
    StringList(const StringList &);
    void add(std::string);
    int positionOf(std::string);
    bool setNodeVal(int, std::string);
    std::vector<std::string>getAsVector();
  
};
#endif


StringList.cpp


#include "StringList.hpp"
#include <string>
#include <vector>
#include <iostream>


StringList::StringList()    // default constructor
{
    head = NULL;
}


StringList::StringList(const StringList& source)    // copy constructor
{
    head = NULL;
  
    ListNode *p = source.head;
    while(p!=NULL)
    {
        add(p->parameter);
        p = p->next;
    }


}

void StringList::add(std::string para)
{
  
    if (head == NULL)
        head = new ListNode(para);
    else
    {
        ListNode *nodePtr = head;
        while (nodePtr->next != NULL)     // walks down the list until pointer equals NULL
            nodePtr = nodePtr->next;
      
        nodePtr->next = new ListNode(para); // actually adds new node to end
      
    }
}

int StringList::positionOf(std::string value)
{
  
    ListNode *nodePtr = head;
    int length = 0;
    while (nodePtr != NULL)
    {
        if (nodePtr->parameter == value)
            return length;
        else
            nodePtr = nodePtr->next;
            length++;
    }
  
        return -1;
}

bool StringList::setNodeVal(int position, std::string value)
{
  
    ListNode *nodePtr = head;
    int count = 0;
      
    while (nodePtr != NULL)
    {
        if (count == position)
        {
            nodePtr->parameter = value;
            return true;
        }
        count++;
        nodePtr = nodePtr->next;
    }

        return false;
}


std::vector<std::string> StringList::getAsVector()
{
    std::vector<std::string> stringVec;
    StringList obj;
  
    ListNode *nodePtr = head;
  
    while (nodePtr != NULL)
    {
        stringVec.push_back(nodePtr->parameter);
        nodePtr = nodePtr->next;
    
    }
    return stringVec;
}

StringList::~StringList()
{

  
    ListNode *nodePtr = head;

    while(nodePtr != NULL)
    {
        ListNode *temp = nodePtr;
        nodePtr = nodePtr->next;
        delete temp;
     
    }

}


main.cpp


#include <iostream>
#include <vector>
#include <string>

#include "StringList.hpp"
using namespace std;

int main() {

    StringList list;
  
    list.add("Hello");
    list.add("World");
    list.add("Goodbye");
  
    cout << "This is the position of Hello in the list: " << list.positionOf("Hello") << endl;
  
    if (list.setNodeVal(0, "Hi"))
        {
            cout << "setNodeVal has returned true" << endl;
        }
    if (list.setNodeVal(6, "Hi") == false)
    {
        cout << "setNodeVal has returned false" << endl;
    }
  
    cout << "I changed Hello to Hi at position: " << list.positionOf("Hi") << endl;
  
    StringList newlist = list;
  
    vector<string> veclist = newlist.getAsVector();
  
    for (int i = 0; i < 3; i++)
    {
        cout << veclist[i] << endl;
    }
  
    cout << "This is the position of something in the new list: " << newlist.positionOf("Goodbye") << endl;
  
    return 0;
}


sample output


This is the position of Hello in the list: 0                                                                                                                
setNodeVal has returned true                                                                                                                                
setNodeVal has returned false                                                                                                                               
I changed Hello to Hi at position: 0                                                                                                                        
Hi                                                                                                                                                          
World                                                                                                                                                       
Goodbye                                                                                                                                                     
This is the position of something in the new list: 2