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.
This is what I have but I don't believe it's compiling right
#include <iostream>
usingnamespace std
// The Node for the list items
class ListNode
{
public:
ListNode(double v, ListNode* p)
{
value = v; next = p;
}
private:
double value;
ListNode*
friendclass LinkedList; // LinkedList has friend status
};
// The linked list class itself
class LinkedList
{
public:
void add(double x);
bool isMember(double x);
LinkedList() { head = NULL; }
~LinkedList();
private:
ListNode* head;
};
// LinkedList::add - This is where you add a given value to the list
void LinkedList::add(double x)
{
head = new ListNode(x, head)
// LinkedList::isMember – This is how you check to see if a given value is a member of the list.
bool LinkedList::isMember
{
ListNode * p = head; // Use p to walk through list
while (p != NULL)
{
if (p->value == p)
returntrue;
else
p = p->next;
}
// List is exhausted without finding x
returnfalse;
// Destructor that deallocates memory for the list.
LinkedList::~LinkedList
{
while (head != 0)
{
ListNode* p = head;
head = head->next;
delete p;
}
}
int main (1)
{
// In this section you are explaining your program to user
cout << "This program constructs a list of numbers and then allows the user "
<< "\nto check if various numbers are on the list.";
// Create empty list
LinkedList list1;
// This is where you get input from user and add them to list
cout << "\nEnter 5 numbers: ";
for (int k = 1; k <= 5; k++)
{
double x;
cin >> x;
list1.add(x);
}
// Allow user to test membership
for (int k = 1; k <= 5; k++)
{
double x
cout << "Enter a number to test membership for: ";
cin >> x;
if (list1.isMember(x));
{ cout << "\n" << x << "is on the list." << endl; }
else
cout << "\n" << x << " is not on the list." << endl;
}
return 0;
}
#include <iostream>
usingnamespace std;
// The Node for the list items
class ListNode
{
public:
ListNode(double v, ListNode* p)
{
value = v; next = p;
}
private:
double value;
ListNode* next;
friendclass LinkedList; // LinkedList has friend status
};
// The linked list class itself
class LinkedList
{
public:
void add(double x);
bool isMember(double x);
LinkedList() { head = NULL; }
~LinkedList();
private:
ListNode* head;
};
// LinkedList::add - This is where you add a given value to the list
void LinkedList::add(double x)
{
// add to front
ListNode* p = new ListNode(x, head);
head = p;
}
// LinkedList::isMember – This is how you check to see if a given value is a member of the list.
bool LinkedList::isMember(double x)
{
ListNode* p = head;
// iterate to the end node
// look for x
while (p != nullptr) {
if (p->value == x) returntrue;
p = p->next;
}
// if x is found, p is not null else
returnfalse;
}
// Destructor that deallocates memory for the list.
LinkedList::~LinkedList()
{
while (head != 0)
{
ListNode* p = head;
head = head->next;
delete p;
}
}
int main ()
{
// In this section you are explaining your program to user
cout << "This program constructs a list of numbers and then allows the user "
<< "\nto check if various numbers are on the list.";
// Create empty list
LinkedList list1;
// This is where you get input from user and add them to list
cout << "\nEnter 5 numbers: ";
for (int k = 1; k <= 5; k++)
{
double x;
cin >> x;
list1.add(x);
}
// Allow user to test membership
for (int k = 1; k <= 5; k++)
{
double x;
cout << "Enter a number to test membership for: ";
cin >> x;
if (list1.isMember(x))
{ cout << "\n" << x << "is on the list." << endl; }
else
cout << "\n" << x << " is not on the list." << endl;
}
return 0;
}