C++ How do I start this? Using an appropriate definition of ListNode, design a s
ID: 3673934 • 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 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.h
#ifndef STRING_LIST_H
#define STRING_LIST_H
#include <bits/stdc++.h>
using namespace std;
class ListNode {
private:
// to store value
string value;
// to store next pointer
ListNode* next;
public:
// default constructor
ListNode();
// declare single value constructor
ListNode(string);
// return value of node
string getValue();
// set value of node
void setValue(string);
// get next node pointer
ListNode* getNext();
// set next node pointer
void setNext(ListNode*);
};
class StringList {
private:
// head node of the list
ListNode* head;
// size of the list
int size;
public:
// default constructor
StringList();
// copy constructor
StringList(StringList *);
// add given item as node to list
void add(string);
// find the position of item in list
int positionOf(string);
// set the value of node of given index to given value
bool setNodeVal(int, string);
// return list as vector
vector<string> getAsVector();
// get head
ListNode* getHead();
// get size
int getSize();
};
#endif
StringList.cpp
#include <bits/stdc++.h>
#include "StringList.h"
using namespace std;
ListNode::ListNode() {
value = "";
next = NULL;
}
ListNode::ListNode(string value) {
this->value = value;
next = NULL;
}
string ListNode::getValue() {
return value;
}
void ListNode::setValue(string value) {
this->value = value;
}
ListNode* ListNode::getNext() {
return next;
}
void ListNode::setNext(ListNode* next) {
this->next = next;
}
StringList::StringList() {
head = NULL;
size = 0;
}
StringList::StringList(StringList *list) {
ListNode* it = list->getHead();
size = list->getSize();
while (it != NULL) {
add(it->getValue());
it = it->getNext();
}
}
void StringList::add(string value) {
if (head == NULL) {
head = new ListNode(value);
} else {
ListNode* it = head;
while (it->getNext() != NULL) {
it = it->getNext();
}
it->setNext(new ListNode(value));
}
++size;
}
int StringList::positionOf(string tosearch) {
ListNode* it = head;
int count = 0;
while (it != NULL && it->getValue() != tosearch) {
it = it->getNext();
count += 1;
}
if (it == NULL) return -1;
else return count;
}
bool StringList::setNodeVal(int position, string value) {
if (position < 0 || position >= size) return false;
ListNode* it = head;
int count = 0;
while (count < position && it != NULL) {
count += 1;
it = it->getNext();
}
it->setValue(value);
return true;
}
vector<string> StringList::getAsVector() {
vector<string> vList;
ListNode* it = head;
while (it != NULL) {
vList.push_back(it->getValue());
it = it->getNext();
}
return vList;
}
ListNode* StringList::getHead() {
return head;
}
int StringList::getSize() {
return size;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.