Linked List Help

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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <fstream>
#include <iomanip>
#include <list>

using namespace 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;
Last edited on
Topic archived. No new replies allowed.