1 Replies - 3421 Views - Last Post: 08 November 2009 - 05:10 PM Rate Topic: -----

#1 Gem74  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 08-November 09

build linked list using an input file

Posted 08 November 2009 - 03:13 PM

Please help, I have 1 error! I am using an input file to build into linked list. I am getting the error message: syntax error: identifier ListNode. It seems I must be calling the incorrect constructor, I have been working on this forever and can't seem to get it error free. Please help ASAP! Thanks!

#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;	//allows all mem fcns of NumberList to access mem fcns of ListNode
		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();
};

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;
	}
	
	NumberList *head = NULL;		         //i may need to rewrite this base on the line below
	// 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);          //this is where my error is
	
	}
	
	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;	
		
}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 08 November 2009 - 04:40 PM


Is This A Good Question/Topic? 0
  • +

Replies To: build linked list using an input file

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: build linked list using an input file

Posted 08 November 2009 - 05:10 PM

why do you have a node class and a list node class?

plus:
while (!myfile.eof())  
		{	  
				// read stuff from the file into a string and print it		
				cout << input << endl;
		}


???

This seems like very incomplete code. Also, ListNode does not exist in main, only within ur objects of NumberList, which is why you are getting the type identifier error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1