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

Recommended Answers

All 10 Replies

In your header, you need to match the #ifndef linkedlist_H with a #endif at the very end of the header file.

In your cpp file, if you want to use the type listNode, you have to fully specify it, i.e. linkedList::listNode , otherwise the compiler won't find it.

Alongside the inclusion guard problems mentioned by Mike, there are a few other problems in your header too!

There should be a semicolon ";" at the end of the declarations of the Add function and the isMumber function.
Also should that be isNumber or isMember rather than isMumber? heh heh!

And from the initial looks of the header, the return type of the isMumber function should be bool NOT boolean.

I think most of the error messages you're getting are because of the errors in the header. The reason that the compiler is mentioning the problems with listNode is because the linkedList class itself is failing to compile due to the afore mentioned problems.

Once you've made those changes, the only other thing that looks suspect to me is your implementation of isMumber in the cpp file for your linked list class.
You haven't included the return type of the function (bool according to the header) but also you appear to be returning a signed integer rather than a bool.

If the function is supposed to be returning a bool then it should be returning true or false. However, if you ARE supposed to be returning an int (which appears to be the case. It looks as if it returns the index of a node which has a value that matches the value passed into the function) then you should change the signature of the function in the header and in the cpp file to return an int rather than a bool.

Other than that the code looks more or less OK.

Additionally, immediately after the line cin >> choice; you may also want to consider using something like cin.sync(); to ignore/discard the rest of the input stream in case the user enters something stupid or unexpected!

I am getting errors in the isNumber that says that c++ dose not support default int

is there a way that you can show me how it can be fix?

linkedlist::isMumber(double x)

You need the return type on the front of that.

@OP...
What Moschops just said!

That's what I was on about when I said this:


Once you've made those changes, the only other thing that looks suspect to me is your implementation of isMumber in the cpp file for your linked list class.
You haven't included the return type of the function (bool according to the header) but also you appear to be returning a signed integer rather than a bool.

If the function is supposed to be returning a bool then it should be returning true or false. However, if you ARE supposed to be returning an int (which appears to be the case. It looks as if it returns the index of a node which has a value that matches the value passed into the function) then you should change the signature of the function in the header and in the cpp file to return an int rather than a bool.

And from looking at your code, it seems that the isMumber function should be returning an int. So it might be worth updating its return value to int in the header and the .cpp file!

i have errors with the bold Lines

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;
}
}
}
[B]linkedlist::isMumber(double x)
{
listNode *nodePtr;
int pos=-1;
if ( !head )[/B]
{
cout << "The list is empty";
return-1;
}
nodePtr = head;
while (nodePtr)
{
pos++;
if(nodePtr->x== x)
return pos;
else
nodePtr=nodePtr->next;
}
return -1;
}
[B]void linkedlist::print() const
{
listNode *nodePtr;
if ( !head )[/B]
{
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
bool isNumber(int )
//Print memberfunction
void print() const;
};
#endif

Without proper formatting, your code is extremely difficult to read. Repost with good indentation.

sorry about the last one

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

We can only keep telling you the same thing over and over.

This line:

linkedlist::isMumber(double x)

You need a return type on the front of it. That means that at the front of it, before linkedlist::isMumber you must put the type of object that is being returned.

Elsewhere, you said this:
bool isNumber(int )

So you've said that the isNumber function will return a bool.

So put bool on the front on linkedlist::isMumber, like this:

bool linkedlist::isMumber

The second problem is that you've written isMumber where you meant to write isNumber. You put an M where you meant to put an N. Do not write
bool linkedlist::isMumber, but instead
bool linkedlist::isNumber

See the difference?

To recap:

linkedlist.h -
missing semi-colon on end of line 20
missing semi-colon on end of line 22

linkedlist.cpp -
line 59 should read bool linkedlist::isNumber(int x)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.