Hi, I am trying to build a linked list, I have the following, and I get 4 error messages, ListNode, NumberList, and head undeclared, and then syntax error, please help.
#include <iostream>
#include <fstream>
#include <ctime>
#include <string>
#include <cstdlib>
//#include "input_data.h"
using namespace std;
class Node {
public:
int x;
Node *next;
Node *prev;
};
class NumberList
{
private:
class ListNode
{
friend class NumberList;
double value; //data portion of the node
ListNode *next; //successor pointer, points to the next node
ListNode(double value1, ListNode *next1 = NULL) //constructor
{
value = value1;
next = next1;
}
};
ListNode *head;
public:
NumberList() //constructor, initializes head pointer to NULL
{head = NULL;}
void insertNode(double);
void appendNode(double);
void deleteNode(double);
void displayList();
};
//****** appendNode is to add the node to the end of the list *****//
void NumberList::appendNode(double num)
{
if (head==NULL)
head = new ListNode(num);
else
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr->next != NULL)
{
nodePtr = nodePtr->next;
}
nodePtr->next = new ListNode(num);
}
}
//****** prints the linked list *****//
void NumberList::displayList()
{
ListNode *nodePtr;
nodePtr = head;
while (nodePtr)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}
//****** Inserts a node *****//
void NumberList::insertNode(double num)
{
ListNode *nodePtr, *previousNodePtr;
if (head==NULL || head->value >= num)
{
head = new ListNode(num, head);
}
else
{
previousNodePtr = head;
nodePtr = head -> next;
while (nodePtr != NULL && nodePtr -> value < num)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr -> next;
}
previousNodePtr -> next = new ListNode(num, nodePtr);
}
}
//***** Deletes a node *****//
//**************************//
void NumberList::deleteNode(double num)
{
ListNode *nodePtr, *previousNodePtr;
if (!head)
return;
if (head->value == num)
{
nodePtr = head;
head = head->next;
delete nodePtr;
}
else
{
nodePtr=head;
while (nodePtr != NULL && nodePtr->value != num)
{
previousNodePtr = nodePtr;
nodePtr = nodePtr->next;
}
if(nodePtr)
{
previousNodePtr->next = nodePtr->next;
delete nodePtr;
}
}
}
//**** an entry point for execution *****//
int main() {
NumberList one;
NumberList two;
cout << "The following are the elements of Periodic Table:\n" << endl;
cout << "Atomic Atomic Atomic " << endl;
cout << "number: symbol: name:" << endl;
cout << "------------------------------------------------" << endl;
fstream myfile; //file object
string input; //variable used to read line from file
myfile.open("input_data.h", ios::in); //opens file for input, information will be read from the file
// If we couldn't open the input file stream for reading
if (!myfile)
{
// Print an error and exit
cout << "Error, input_data.h could not be opened for reading!" << endl;
exit(1);
}
// While there's still stuff left to read
getline(myfile, input);
while (!myfile.eof())
{
// read stuff from the file into a string and print it
cout << input << endl;
}
ListNode *numberList = NULL; //list of numbers
// Read the file into a linked list
cout << "The contents of the file are:" << endl;
while (myfile >> input)
{
cout << input << " ";
//create a node to hold this number
head = new ListNode (input, head);
}
cout << endl;
one.insertNode(1);
one.insertNode(2);
one.insertNode(3);
one.appendNode(23);
one.insertNode(2);
two.appendNode(90);
one.displayList(); //prints functions for object "one"
two.displayList(); //prints functions for object "two"
one.deleteNode(1);
cout << endl;
cout << endl;
one.displayList();
cout << endl;
myfile.close();
return 0;
}