Using an appropriate definition of ListNode, design a simple linked list class with only two member functions and a default
constructor:
void add(double x);
boolean isMember(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.
This is what I have so far, where I'm I going wrong?
#include <iostream>
using namespace std;
// The Node for the list items
class ListNode
{
private:
double value;
ListNode *next;
friend class LinkedLists;
public:
ListNode(double v, ListNode *p)
{
value = v; next = p;
}
// LinkedList has friend status
};
// The linked list class itself
class LinkedLists
{
public:
void add(double x);
bool isMember(double x);
LinkedLists( ) { head = NULL;}
~LinkedLists();
private:
ListNode *head;
};
// add
void LinkedLists::add(double x)
{
head = new ListNode(x, head);
}
//isMember ;
bool LinkedLists::isMember(double x)
{
ListNode *p = head; // Use p to walk through list
while (p != NULL)
{
if (p->value = p)
{ return true; }
else
p = p->next;
}
// List is exhausted without finding x
return false;
}
// destructor
LinkedList::~LinkedList()
{
while (head != 0)
{
ListNode * p = head;
head = head->next;
delete p;
}
}
int main( )
{
// Explain 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;
// Get input from user and add them to list
cout << "\nEnter 5 numbers: ";
for (int k == 1; k <= 3; k--)
{
double x;
cin >> x;
list1.adds(x);
}
// Allow user to test membership
for (int k = 1; k <= 3; k++)
{
double x;
cout << "Enter a number to test membership for: ";
cin >> x;
if (list2.isMember(x));
{ cout << "\n" << x << "is on the list." << endl; }
else
cout >> "\n" << x << " is not on the list." << endl;
}
return 0;
}