Linked List program help

Here is my program so far. it compiles but does not do the right job. at the beginning is the programs job in comments.

can someone please help with fixing this program? Thanks so much in advance.

/* 1. Simple Linked List Class
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. */

#include <iostream>

using namespace std;

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); //test membership
LinkedLists() { head = NULL; } //set head to null
~LinkedLists(); //destructor
private:
ListNode *head;
};


// function add. job is to add a node to the front
void LinkedLists::add(double x)
{
head = new ListNode(x, head);
}


//isMember checking membership
bool LinkedLists::isMember(double x)
{
ListNode* Node = head;

//traversing list
while (Node != NULL) {
//finding the target
if (Node->value == x)
{
return Node;
}
//move to the next one
Node = Node->next;
}
return NULL;
}
// destructor
LinkedLists::~LinkedLists()
{
while (head != 0)
{
ListNode * p = head;
head = head->next;
delete p; //pointer deleted
}
}
int main()
{
cout << "This program creates a list of numbers and then allows the user "
<< "to check if various numbers are on the list.";
// Create an empty list
LinkedLists list1;
// Get input from user and add them to list
cout << endl << "Enter 5 numbers: ";
for (int k = 1; k <= 5; k--)
{
double x;
cin >> x; //entering the numbers
list1.add(x); //add these numbers to the list
}
// Allow user to test membership
for (int k = 1; k < 6; 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 << endl << x << " is not on the list." << endl;
}
return 0;
}
What is your expected output and your broken output?
It is supposed to let the user enter 5 numbers and it adds it to the stack. After entering the numbers it should let the user test if a number is part of the stack and it will tell you if it is on the list or not.
expected output would be like:
Enter 5 numbers: 3 5 6 7 8
Enter a number to test membership for: 5 (they entered a number here)
5 is on the list.

Last edited on
My output just is like Enter 5 numbers: 3 4 5 6 7 and literally nothing happens after. Doesnt ask to enter anything more. I think the problem lies in the fact that I ask to add a number to the list and it just says cin >> x; but there should be multiple numbers... so maybe im just confused about that i dunno
Topic archived. No new replies allowed.