build linked List

Hello Everyone!
I need help to create a buildList function that will build a linked list by inserting one int at a time into the front of the linked list from a text file.
And then prints out the numbers to the screen.
Here is what I have so far, and some problem during buildList...

Thank you for your time!

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
#include <iostream>
using namespace std;
#include <fstream>


struct nodeType
{
    int  info;
    nodeType  * link;
};

void buildList (nodeType *&head);
void showList (nodeType *head);
int main (void)
{  
    nodeType *head =NULL;
    StackADT  <int> stack;//aggrument list for class template.

    buildList (head);
    showList (head);
   

return 0;
}
void buildList (nodeType*& head)
{

    head = NULL;
    head->link = NULL;
    nodeType *temp; 
    temp = new nodeType;
    
    int num;

    ifstream myfile ("stackNumbers.txt");

  if (myfile.is_open())
  {
      while (!myfile.eof())
    {
      myfile >> num;//get num from text file
      temp->info=num;// stores num into a linked list
      temp->link=head;//puts node in the list PROBLEM HERE
      head = temp;// points head to a new node
    }
    
}
 myfile.close();

 
else 
cout << "Unable to open file"; 
 
}

void showList (nodeType * head)
{
    if (head !=NULL)
    {
        cout << head->info << endl;
        showList (head->link);
    }

}
Last edited on
Topic archived. No new replies allowed.