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

Using an appropriate definition of ListNode, design a simple linked list class c

ID: 3765755 • 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

#ifndef LINKEDLIST_H

#define LINKEDLIST_H

class linkedlist

{

private:

struct listNode

{

double x;

struct listNode *next;

};

listNode *head;

public:

linkedlist()

{

head = NULL;

}

// copy constructor

linkedlist( const linkedlist& otherList );

//add member fuunction

void add(double );

//isMumber member function

bool isMember(double );

//Print memberfunction

void print() const;

};

#endif

cpp file:

#include<iostream>

#include "linkedlist.h"

using namespace std;

linkedlist::linkedlist( const linkedlist& otherList )

{

head = NULL;

listNode *newNode;

listNode *nodePtr;

listNode *tempPtr;

if ( !otherList.head )

return;

nodePtr = otherList.head;

head = new listNode;

head->x = nodePtr->x;

head->next = NULL;

nodePtr = nodePtr->next;

tempPtr = head;

while ( nodePtr != NULL )

{

newNode = new listNode;

newNode->x = nodePtr->x;

newNode->next = NULL;

tempPtr->next = newNode;

tempPtr = newNode;

nodePtr = nodePtr->next;

}

}

void linkedlist::add( double x )

{

listNode *newNode;

listNode *nodePtr;

listNode *previousNode = NULL;

newNode = new listNode;

newNode->x = x;

newNode->next = NULL;

if ( !head )

head = newNode;

else

{

nodePtr = head;

previousNode = NULL;

while ( nodePtr != NULL && nodePtr->x < x )

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

if ( previousNode == NULL )

{

head = newNode;

newNode->next = nodePtr;

}

else

{

previousNode->next = newNode;

newNode->next = nodePtr;

}

}

}

bool linkedlist::isMember(double x)

{

listNode *nodePtr;

double pos=-1;

if ( !head )

{

cout << "The list is empty";

return-1;

}

nodePtr = head;

while (nodePtr)

{

pos++;

if(nodePtr->x== x)

return pos;

else

nodePtr=nodePtr->next;

}

return -1;

}

void linkedlist::print() const

{

listNode *nodePtr;

if ( !head )

{

cout << "The list is empty.";

return;

}

nodePtr = head;

cout << " The elements in the list are:";

while (nodePtr)

{

cout << nodePtr->x << " -> ";

nodePtr = nodePtr->next;

}

cout << "Null";

}

#include<iostream>

using std::cout;

using std::cin;

#include "linkedlist.h"

int main()

{

linkedlist myList;

char choice;

int n;

do {

cout << " Insert a node : I";

cout << " Quit : Q";

cout << " Enter your choice: ";

cin >> choice;

switch ( choice )

{

case 'I' :

case 'i' :

cout<<" Enter an integer: ";

cin >> n;

myList.add( n );

break;

case 'Q' :

case 'q' :

choice = 'q';

}

} while ( choice != 'q' );

return 0;

}

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