Using an appropriate definition of ListNode, design a simple linked list class c
ID: 3764424 • 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
*
* Created on: 21-Nov-2015
* Author: Ravi
*/
#include<string>
#include <iostream>
#include<cstdlib>
using namespace std;
#ifndef STRINGLIST_H_
#define STRINGLIST_H_
class StringNode{
public:
string data;
StringNode *next;
};
class StringList {
private:
StringNode *head;
public:
StringList();
virtual ~StringList();
void add (std::string);
int positionOf (std::string);
bool setNodeVal(int, std::string);
};
#endif /* STRINGLIST_H_ */
/*
* StringList.cpp
*
* Created on: 21-Nov-2015
* Author: Ravi
*/
#include "StringList.h"
StringList::StringList() {
// TODO Auto-generated constructor stub
head = NULL;
}
StringList::~StringList() {
// TODO Auto-generated destructor stub
free(head);
}
void StringList::add(string data) {
if(head == NULL) {
head = new StringNode;
head->data = data;
head->next = NULL;
} else {
StringNode *temp = head;
while(temp->next != NULL) {
temp = temp->next;
};
StringNode *node = new StringNode;
node->data = data;
node->next = NULL;
temp->next = node;
}
}
int StringList::positionOf(string data) {
if(head != NULL) {
StringNode *temp = head;
int count = 0;
while(temp != NULL) {
count++;
if(temp->data == data) {
return count;
}
temp = temp->next;
};
}
return 0;
}
bool StringList::setNodeVal(int index, string data) {
if(head != NULL) {
StringNode *temp = head;
int count = 0;
while(temp != NULL) {
count++;
if(count == index) {
temp->data = data;
return true;
}
temp = temp->next;
};
}
return false;
}
int main() {
StringList sl;
sl.add("Node1");
sl.add("Node2");
int pos = sl.positionOf("Node2");
cout << "Position of Node2: " << pos <<endl;
sl.add("Node3");
sl.setNodeVal(2, "Node####21");
pos = sl.positionOf("Node####21");
cout << "Position of Node####21: " << pos <<endl;
}
----------------output--------------
Position of Node2: 2
Position of Node####21: 2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.