Linked List

Apr 7, 2020 at 3:31pm
Using an appropriate definition of ListNode, design a 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 but I don't believe it's compiling right
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  #include <iostream>

using namespace std

// The Node for the list items
class ListNode
{
public:
    ListNode(double v, ListNode* p)
    {
        value = v; next = p;
    }
private:
    double value;
    ListNode*
        friend class 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)
                return true;
            else
                p = p->next;
        }
        // List is exhausted without finding x
        return false;

            //    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;
        }
Apr 7, 2020 at 4:16pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
  #include <iostream>

using namespace std;

// The Node for the list items
class ListNode
{
public:
    ListNode(double v, ListNode* p)
    {
        value = v; next = p;
    }
private:
    double value;
    ListNode* next;
        friend class 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) return true;
			p = p->next;
		}
		// if x is found, p is not null else
		return false;
	}
        

            //    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;
        }
Last edited on Apr 7, 2020 at 4:17pm
Apr 7, 2020 at 4:32pm
Hi Manga,

I very much appreciate your time and help.
Topic archived. No new replies allowed.