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

1) Using an appropriate definition of listnode, design a simple linked list clas

ID: 3714241 • Letter: 1

Question

1)Using an appropriate definition of listnode, design a simple linked list class with only two member function and a defult constructor: void add(double x) boolean isMumber(double x) LinkedList(); The add function adds a new node containing x to the front (head) of the list, while the isMember function tests to see if the list contains a node with the value x. Test your linked list class by adding various numbers to the list and then testing for membership

I have already figured out the code for number 1, I need help with number 2 ONLY, thank you!

code for number 1:

#include "stdafx.h"

#include

using namespace std;

//if we place an element at the start of the list we get a First in last out form of a data structure

class LinkedList

{

private:

struct myList

{

double data;

myList* nxt;

};

myList* head;

public:

LinkedList()

{

//use default constructor to make the head ready

head = new myList;

head->data = NULL;

head->nxt = NULL;

}

void add(double x)

{

//add in front of list

myList* temp = head;

head = new myList;

head->nxt = temp;

head->data = x;

}

int search(double x)

{

int i = 0;

myList* temp = head;

while (temp->data != NULL)

{

if (temp->data == x)

{

//the value is in the list

return i;

}

temp = temp->nxt;

i++;

}

//-1 for not found

return -1;

}

bool isMember(double x)

{

myList* temp = head;

while (temp->data != NULL)

{

if (temp->data == x)

{

//the value is in the list

return true;

}

temp = temp->nxt;

}

return false;

}

void printMyList()

{

myList* temp = head;

int i = 0;

while (temp->data != NULL)

{

cout << "The value at index " << i << " is " << temp->data << endl;

//this is how we iterate the list

temp = temp->nxt;

i++;

}

}

};

int main()

{

//call our constructor

LinkedList l;

//add a bunch of doubles in the list

l.add(1.1);

l.add(2.1);

l.add(3.1);

l.add(4.1);

l.add(5.1);

l.add(6.1);

l.add(7.1);

//print it out

l.printMyList();

if (l.isMember(1.1))

{

cout << 1.1 << " is in the list, it is at index " << l.search(1.1) << " in the list" << endl;

}

else

{

cout << "element is not in the list" << endl;

}

if (l.isMember(2345.3456))

{

cout << "that double is not in the list so this is never ever going to be printed to your screen;)" << endl;

}

else

{

cout << "element not found" << endl;

}

system("pause");

return 0;

}

2.)Modify your list class of programming challenge 1 to add a copy constructor. Test your class by making a copy of a list and then testing membership on the copy.

Explanation / Answer

Code:

LinkedList(LinkedList &obj)
{
for (myList *o = obj.head; o; o = o->nxt) {
this->add(o->data);
}
}

This iterates over the data and adds it to the new list.