To any and everyone :)
As stated in my title, this is a homework assignment, so I appreciate any help or suggestions that are given. Also, per the website rules, I have put effort to this, and I'm not asking anyone to just do my work.

(The program compiles and works as-is, but the output for the search function is not correct.)

OK...here is the problem. The isMember member function is working, but is not returning the correct location. For instance, when initially running the program and entering the numbers 10, 20, and 30 (in that order) into the list, and then doing the search on the number 30 -- it will return that it found the number in location 0.

Can someone help me get this fixed, so it would give location 3 for the search results? Thanks in advance. Jason

p.s. I apologize if my program does not follow "best practices." In fact, I'm very open to "real world" constructive criticism as to where I can improve. With that being said, I do know that this program is lacking in error checking.

My source file:

#include<iostream>

#include "linkedList.h"

//so all functions can use LinkedList class
LinkedList list;

void enterNumbers();
void displayList();
void searchList(double z);
void deleteNode(double z);

// start main
int main()
{
    // declare variables
	int statusChoice = 0;
	int menuChoice = 0;
	double searchValue = 0.0;
   	

	// output user information
    cout << "This program uses a linked list. The user has the options to \n";
	cout << "add data to the list, delete data from the list, or display \n";
	cout << "the contents of the list.\n\n\n";

	// User input to start or stop the program
	cout << "Linked list operations.\n";
	cout << "Enter 1 to start or 2 to stop.\n" << endl;
	cin >> statusChoice;
	while (statusChoice == 1)
	{	
		//User needs to choose option of what to do
		cout << "Choose an option below" << endl;
		cout << "\t1) Add Data" << endl;
		cout << "\t2) Delete Data" << endl;
		cout << "\t3) Display Data" << endl;
		cout << "\t4) Search" << endl;
		cout << "\t5) Exit" << endl;
		cin >> menuChoice;

		switch(menuChoice)
		{
			case 1: enterNumbers();
				break;
			case 2: deleteNode(searchValue);
				break;
			case 3: displayList();
				break;
			case 4: searchList(searchValue);
				break;
			case 5: statusChoice = 2;
		}
	}

	// output termination message
	cout << "\n\n\nProgram has terminated successfully!\n\n\n\n";
	return 0;
}

void enterNumbers()
{
	double number = 0.0;

	// Get input from user and add them to list
	cout << "\nYou now need to enter numbers into the list.";
		
	// enter numbers into the list - entering -1 will escape out of loop
	do
	{   
		cout << "\nEnter a numerical value <Enter -1 to stop>\n";
		cin >> number;
		list.add(number);
	}while(number != -1);
	
}

void displayList()
{
	// Print the list
	cout << "\n\nThe numbers you entered into the list will be displayed.\n";
	cout << "\nFollowing are the numbers entered: " << endl;
	
	list.print();
	cout << endl;
}

void searchList(double z)
{	
	cout << "Enter number to find in the list" << endl;
	cin >> z;
	list.isMember(z);
}

void deleteNode(double z)
{
	cout << "Enter number to delete from the list" << endl;
	cin >> z;
	list.deleteNode(z);
}

My header file:

#include <iostream>
using namespace std;

// declare class ListNode
class ListNode
{
public:
    ListNode(double v, ListNode *p)
    {
        value = v; next = p;
    }
private:    
    double value;
    ListNode *next;
    friend class LinkedList; // LinkedList has friend status	
};

// declare class LinkedList
class LinkedList
{
public:
    void add(double x);
    bool isMember(double x);
	void deleteNode(double x);
    LinkedList( ) { head = NULL;}    
    void print();        
private:
    ListNode * head;
    static void rPrint(ListNode *pList);
};

//   define member print.            
void LinkedList::print()
{
    ListNode *p = head; // Use to walk through list
    while (p != NULL)
    {
		cout << p->value << " ";
        p = p->next;
    }
}

// define member add
void LinkedList::add(double x)
{
	// do not accept sentinel as data to be added to list
	// if sentinel is passed to function member - does not add to list
	// and escapes loop in listPrint.cpp
	if (x != -1)
	{
		head = new ListNode(x, head);
		
	}
	else
		return;
}




// define membber isMember
bool LinkedList::isMember(double x)
{
	int position = 0;
	ListNode *p = head; // Use p to walk through list
    while (p)
    {
        if (p->value == x) 
		{
			cout << "Value was found in the list in position " << position << ".\n\n";
			return true;
		}
        else
            p = p->next;
			position ++;
    }
    // List is exhausted without finding x
    return false;    
}

void LinkedList::deleteNode(double z)
{
	ListNode *nodePtr, *previousNodePtr;

	// If the list is empty, do nothing
	if (!head)
		return;
	// Determine if the first node is the one to delete
	if (head->value == z)
	{
		nodePtr = head;
		head = head->next;
		delete nodePtr;
	}

	else

	{
		//Initialize nodePtr to head of list
		nodePtr = head;

		//skip all nodes whose value member is not equal to z
		while (nodePtr != NULL && nodePtr->value != z)
		{
			previousNodePtr = nodePtr;
			nodePtr = nodePtr->next;
		}

		//link the previous node to the node after nodePtr, then delete nodePtr
		if (nodePtr)
		{
			previousNodePtr->next = nodePtr->next;
			delete nodePtr;
		}
	}
}

Since you are adding nodes to the head (start) of the list, the last node you add will be in position 0. The output is correct for the example you gave. I'm assuming the list isn't supposed to be sorted - if so then you need to change your add function.

mahler...

No, the list does not need to be sorted.

I was looking at the STL-- "list.reverse" -- but I'm not for sure I understand the syntax on how to use that. I'll see what google tells me.

I was also thinking of a recursive function to swap the nodes around, but I think I've been staring at this for too long today.

My apologies if I posted too much code. I'm a nOOb both here and in the C++ world.

AH....Just re -read your message Mahler...

Do you think appending each value into the list would be a better, albeit dirty, way to fix this issue?

Thanks

Jason

What exactly are the requirements? Are you supposed to be adding new nodes to the beginning of the list or the end of the list? Are you sure you are not supposed to get the result you got?

In the case that you are supposed to append to the END of the list instead of the beginning as you have done, then you will need to loop through the entire list and put the new node at the end when adding a node. This is clearly inefficient, but if the requirements state that you should be adding to the end of the list then that's what you have to do.

If the requirements say that you should be getting '2' as the output for your example, but specifies that you should be adding new nodes to the start of the list, then that's messed up.... The only real way to fix that case is if you keep track of the length of the list as you insert/remove nodes and then subtract the actual found location from the length of the list (and then -1 since it is 0 indexed). So in your example you would do 3 - 0 - 1 = 2.

I didn't say this wasn't messed up ;) (Don't get me started on my prof.)

The example output that was shown has the numbers 10, 20, and 30 entered into the list, and when doing the search on the number 30 it is to return that the number was found in location number 3.

I tried this:

// define member add
void LinkedList::add(double x)
{
	// do not accept sentinel as data to be added to list
	// if sentinel is passed to function member - does not add to list
	// and escapes loop in listPrint.cpp
	if (x != -1)
	{
		if (head == NULL)
		{
			head = new ListNode(x, head);
		}
		else
		{
			ListNode *nodePtr = head;
			while (nodePtr->next != NULL)
			{
				nodePtr = nodePtr->next;
			}
			nodePtr->next = new ListNode(x, head);
		}
	}
	else
		return;
}

It works for the first two numbers you enter into the list, but crashes when trying to enter in the 3rd number. I tried it with entering just two numbers -- 10 and 20 (in that order)-- and did a search on 20 and returned location 1, which would be correct. However, when trying to display the numbers, it infinitely fills the screen with 10's and 20's. grrrrrr

I've attached the example output we are supposed to be following.

When adding the node at the end (as you are now doing), you should have the new node point to NULL instead of head. The reason you get it infinitely repeating is because when your end node points to the head (the start) it is basically a giant loop.

This question has already been answered. Start a new discussion instead.