Using an appropriate definition of listnode, design a simple linked list class with only two member function and a defult constructor:
void add(double x)
boolean isMumber(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
second part of the question :
Modify your list class of programming challenge 1 to add a copy constructor. Test your class by making a copy of a list and then testing membership on the copy.
third part :
Modify the list class you created in the previous programming challenge to add a print member function. Test the class by starting with an empty list, adding some elements, and then printing the resulting list out.
I am getting errors like in the header file tels me :
the #endif for this directive is missing
and :
identifier "nodePtr" is undefined
also:
identifier "listNode" is undefined
cpp File
#include<iostream>
#include "linkedlist.h"
using namespace std;
linkedlist::linkedlist( const linkedlist& otherList )
{
head = NULL;
listNode *newNode;
listNode *nodePtr;
listNode *tempPtr;
if ( !otherList.head )
return;
nodePtr = otherList.head;
head = new listNode;
head->x = nodePtr->x;
head->next = NULL;
nodePtr = nodePtr->next;
tempPtr = head;
while ( nodePtr != NULL )
{
newNode = new listNode;
newNode->x = nodePtr->x;
newNode->next = NULL;
tempPtr->next = newNode;
tempPtr = newNode;
nodePtr = nodePtr->next;
}
}
void linkedlist::add( double x )
{
listNode *newNode;
listNode *nodePtr;
listNode *previousNode = NULL;
newNode = new listNode;
newNode->x = x;
newNode->next = NULL;
if ( !head )
head = newNode;
else
{
nodePtr = head;
previousNode = NULL;
while ( nodePtr != NULL && nodePtr->x < x )
{
previousNode = nodePtr;
nodePtr = nodePtr->next;
}
if ( previousNode == NULL )
{
head = newNode;
newNode->next = nodePtr;
}
else
{
previousNode->next = newNode;
newNode->next = nodePtr;
}
}
}
linkedlist::isMumber(double x)
{
listNode *nodePtr;
int pos=-1;
if ( !head )
{
cout << "The list is empty";
return-1;
}
nodePtr = head;
while (nodePtr)
{
pos++;
if(nodePtr->x== x)
return pos;
else
nodePtr=nodePtr->next;
}
return -1;
}
void linkedlist::print() const
{
listNode *nodePtr;
if ( !head )
{
cout << "The list is empty.";
return;
}
nodePtr = head;
cout << "\n The elements in the list are:";
while (nodePtr)
{
cout << nodePtr->x << " -> ";
nodePtr = nodePtr->next;
}
cout << "Null";
}
#include<iostream>
using std::cout;
using std::cin;
#include "linkedlist.h"
int main()
{
linkedlist myList;
char choice;
int n;
do {
cout << "\nInsert a node : I";
cout << "\nQuit : Q";
cout << "\ntEnter your choice: ";
cin >> choice;
switch ( choice )
{
case 'I' :
case 'i' :
cout<<"\tEnter an integer: ";
cin >> n;
myList.add( n );
break;
case 'Q' :
case 'q' :
choice = 'q';
}
} while ( choice != 'q' );
return 0;
}
Header file
#ifndef linkedlist_H
#define linkedlist_H
class linkedlist
{
private:
struct listNode
{
double x;
struct listNode *next;
};
listNode *head;
public:
linkedlist()
{
head = NULL;
}
// copy constructor
linkedlist( const linkedlist& otherList );
//add member fuunction
void add(double )
//isMumber member function
boolean isMumber(double )
//Print memberfunction
void print() const;
};