Hello. I need help writing a program that takes numbers from an external file, passes them through a linked list, displays the original list, a frontwards list, and a backwards list. I'm coming up with a few errors though. Here is my code:
Any help would be appreciated.
#include <iostream>
#include <fstream>
#include <iomanip>
#include <list>
usingnamespace std;
//Define and declare Struct
struct Node
{
int value;
Node *Next;
Node(int y)
{
value = y;
Next =NULL;
}
};
//Define class
class LinkedList
{
Node *Head;
Node *Last;
public: //Defined everywhere
LinkedList()
{ Head = NULL; Last = NULL; } //Initialized
//Function Prototypes
void appendNodefront(int x);
void appendNodeback(int x);
void dispNodes();
};
int main() // This is the old standard input from external file to an array
{
int testdata [20],i=0,j=0;
ifstream infile;
ofstream outfile;
Display();
infile.open("Datain.txt" );
outfile.open("Dataout.txt" );
if (!infile)
{ cout << "Cannot open the input file: Datain.txt " << endl;
cout << "Program terminates" << endl; // If the program has no input file to open, it will terminate.
return 1;
}
while ( !infile.eof() )
{
infile >> testdata [i];
i++;
}
infile.close();
i--;
cout << "Before insertion into linked list, the list elements are:" << endl;
for (j=0; j<i;j++)
cout << testdata[j] << " "; //Outputs the original array
LinkedList *list = new LinkedList(); //Declare new linked list called list
//append nodes to front of the list
for( int i = 0 ; i < j; i++) //pull values from array to add to list
list->appendNodefront(testdata[i]); //add values to linked list in function
}
//Function to add to front of list
//Function appendNodefront
// x is testdata[i]
void LinkedList::appendNodefront(int x)
{
Node *Next2 = new Node(x);
if( Head == NULL)
{
Head = Next2;
Last = Next2;
Head->value = x;
}
else
{
Next2 -> Next = Head;
Head = Next2;
Head->value = x;
}
}
list->dispNodes(); //Display values in list
//Function dispNodes
void LinkedList::dispNodes()
{
Node *temp = Head;
while(temp != NULL)
{
cout << temp->value <<" ";
temp = temp->Next;
}
cout << endl;
}
LinkedList *list2 = new LinkedList(); //Declare new linked list called list2
//append nodes to front of the list2
for( int i = 0 ; i < j; i++) //pull values from array to add to list2
list2->appendNodeback(testdata[i]); //add values to linked list2 in function
cout << "\n\nNodes added to the back of the Linked List are:" << endl;
list2->dispNodes(); //Display values in list2
//Function to add to back of list2
// x is testdata[i]
void LinkedList::appendNodeback(int x)
{
Node *Next1 = new Node(x);
if( Head == NULL)
{
Head = Next1;
Last = Next1;
Head->value = x;
}
else
{
Last->Next = Next1;
Last = Next1;
Last-> value =x;
}
}
cout << "\n\nNodes added to the back of the Linked List are:" << endl;
list2->dispNodes(); //Display values in list2
Line 80 has the arrow (->) spaced away from HEAD. Fix that and post the compile errors. Also everytime you create a new Node "new Node(x)" why are you also assigning x to it again with node->value = x;