Using an appropriate definition of ListNode, design a simple linked list class c
ID: 3765116 • Letter: U
Question
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 create a new empty StringList object. The copy constructor should create a completely separate duplicate of a StringList object (a deep copy - I think with a LinkedList you'll have to it do this way anyway). 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.h
#include <iostream>
#include <vector>
#include <string>
using namespace std;
typedef struct list{
string val;
struct list* next;
}listnode;
class StringList
{
private:
listnode* root;
public:
StringList();
StringList(const StringList &obj);
~StringList();
void add(string val);
int positionOf(string val);
bool setNodeVal(int pos, string val);
vector<string> getAsVector();
};
StringList.cpp
#include "StringList.h
StringList::StringList()
{
root = NULL;
}
StringList::~StringList()
{
listnode* temp;
while(root!=NULL){
temp = root;
root = root->next;
delete(temp);
}
}
StringList::StringList(const StringList& obj)
{
listnode* temp = obj.root;
while(temp!=NULL){
add(temp->val);
temp = temp->next;
}
}
void StringList::add(string val)
{
listnode* temp = root;
listnode* node = new listnode();
node->val = val;
if(root == NULL)
root = node;
else
{
while(temp->next!=NULL)
temp = temp->next;
temp->next = node;
}
}
int StringList::positionOf(string val)
{
listnode* temp = root;
int pos=0;
while(temp!=NULL){
if(temp->val == val)
return pos;
temp = temp->next;
pos++;
}
return pos;
}
bool StringList::setNodeVal(int pos, string val)
{
listnode* temp = root;
int index = 0;
while(temp!=NULL){
if(index == pos){
temp->val = val;
return true;
}
if(index > pos)
return false;
temp = temp->next;
index++;
}
}
vector<string> StringList::getAsVector()
{
listnode* temp = root;
vector<string> listofstring;
while(temp!=NULL){
listofstring.push_back(temp->val);
temp = temp->next;
}
return listofstring;
}
int main()
{
int i;
StringList obj;
vector<string> stringvector;
obj.add("My");
obj.add("name");
obj.add("is");
obj.add("chegg");
stringvector = obj.getAsVector();
for(i=0;i<stringvector.size();i++)
cout<<stringvector[i]<<endl;
printf(" Content of first object after modification is ");
obj.setNodeVal(3,"chegg.com");
stringvector = obj.getAsVector();
for(i=0;i<stringvector.size();i++)
cout<<stringvector[i]<<endl;
printf(" Position of name is %d ",obj.positionOf("name") );
StringList obj2 = obj;
stringvector = obj2.getAsVector();
printf(" Contents of second object is ");
for(i=0;i<stringvector.size();i++)
cout<<stringvector[i]<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.