Good morning and I thank you in advance for the help. I am have major problems with the List function getNewNode and also the Node for the first and last. It says forbid declaration of Node with no type. I add the Node.h and also friend List class. I am majorly confused.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef NODE_H
#define NODE_H
class Node
{
friendclass Link;
public:
Node( int & );
int getData() const;
private:
int info;
Node *nextPtr;
};
#endif // NODE_H
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include<iostream>
#include "Node.h"
usingnamespace std;
Node::Node( int &value )
: info( 0 ), nextPtr( 0 )
{
info = value;
}
int Node::getData() const
{
return info;
}
#include <iostream>
#include "Node.h"
#include "Link.h"
usingnamespace std;
Link::Link()
: firstPtr( 0 ), lastPtr( 0 )
{}
Link::~Link()
{
if( !isEmpty())
{
Node *currentPtr = firstPtr;
Node *tempPtr;
while( currentPtr != 0 )
{
tempPtr = currentPtr;
cout << tempPtr->getData() << "\n" ;
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout << "All nodes destroyed\n" << endl;
}
Node Link::getNewNode( int &value )
{
returnnew Node( value );
}
bool Link::isEmpty() const
{
return firstPtr == 0;
}
void Link::insertAtFront( int &value )
{
Node *newPtr = getNewNode( value );
if( isEmpty() )
firstPtr = lastPtr = newPtr;
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
void Link::insertAtBack( int &value )
{
Node *newPtr = getNewNode( value );
if( isEmpty() )
firstPtr = lastPtr = newPtr;
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
bool Link::removeFromFront( int &value )
{
if( isEmpty())
returnfalse;
else
{
Node *tempPtr = firstPtr;
if( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->info;
delete tempPtr;
returntrue;
}
}
bool Link::removeFromBack( int &value )
{
if( isEmpty())
returnfalse;
else
{
Node *tempPtr = lastPtr;
if( firstPtr == lastPtr )
firstPtr = lastPtr = 0;
else
{
Node *currentPtr = firstPtr;
while (currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->info;
delete tempPtr;
returntrue;
}
}
void Link::print() const
{
if( isEmpty() )
{
cout << "The linked list is completely empty" << endl;
return;
}
Node *currentPtr = firstPtr;
cout << "The current integers are as follows: ";
while( currentPtr !=0 )
{
cout << currentPtr->getData() <<" \n";
currentPtr = currentPtr->nextPtr;
}
}
void Link::selectMenu( Link &CurrentList )
{
int option = 0;
int value = 0;
cout << " Enter an integer that corresponds with the correct action wanting to perform:\n"
<< "1 - insert at the front\n"
<< "2 - insert at the end\n"
<< "3 - delete from the front\n"
<< "4 - delete from the end\n"
<< "5 - end linked list programn";
cin >> option;
while( option < 5 )
{
switch( option )
{
case 1:
cout << "Enter an integer" ;
cin >> value;
CurrentList.insertAtFront( value );
CurrentList.print();
break;
case 2:
cout << "Enter an integer" ;
cin >> value;
CurrentList.insertAtBack( value );
CurrentList.print();
break;
case 3:
if( CurrentList.removeFromFront( value ))
cout << value << " removed\n";
CurrentList.print();
break;
case 4:
if( CurrentList.removeFromBack( value ))
cout << value << " removed\n";
CurrentList.print();
break;
}
}
cout << "complete" << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include "Link.h"
#include "Node.h"
usingnamespace std;
int main()
{
cout << "This program will attempt to collect and return a linked list" << endl;
Link myNumbers;
myNumbers.selectMenu( myNumbers );
}
With regard to Node does not name a type error.
This is because you have a circular include header problem ( link.h includes node.h which includes link.h and nothing ends up getting included).
As the Node class declaration in Node.h only declares the Link class
as a friend, then we do not need to include link.h
What I am trying to say is this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#ifndef NODE_H
#define NODE_H
//#include "Link.h" //get rid of this line from Node.h
class Node
{
friendclass Link;
public:
Node( int );
int getData() const;
private:
int info;
Node *nextPtr;
};
#endif // NODE_H
There are a whole lot of errors in the link.h and link.cpp files.
Most of your definitions in Link.cpp do not match the prototypes given in the class declaration in link.h.
There are also other error like missing brackets.
There are too many to mention - and I leave you to sort them out.
I am sorry for getting sloppy with the code. I started concentrating on the error with the friend class that I disregarded the attention needed for the rest of the code. A great lesson was learned. Thanks for the help. I hope you do not mind me asking one more question.
I finally got it to work but I am a misunderstand this:
Node *getNewNode( int & );
Node *Link::getNewNode( int &value )
{
return new Node( value );
}
Does this mean that I will return a pointer to a new Node( value ). That is what I believe but I am unsure if that is correct. When I remove the * I get a conversion error from Node* to non-scalar type node request. Are you able to explain the error a little. I know how to fix it, but I really would like to know what is the background of it. Thank you so much and have a great weekend.