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;
}
}
}