Linked List - How To Create From File

I am a CS student and currently stuck on an assignment regarding linked lists. I am supposed to take a functioning program that reads input from a file and creates an array and change it so that it uses linked lists instead of an array.

I'm working in three files: main,linkedlist.h, and data.txt. At the moment, when the program enters the loop to read the input it gets stuck in an infinite loop. I can't tell whether it is my linked list construction that is the problem, or simply reading the file input. Any and all advice would be most welcome...

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//***SAMPLE DATA.TXT FILE********
Shelly Malik
12 8 2000
Lincoln Drive
Omaha
Nebraska
68131
402-555-1212
Family
Donald Duck
10 6 1980
Disney Street
Orlando
Florida
11234
622-873-8920
Friend

//*********LINKEDLIST FILE**********
#ifndef H_linkedListType
#define H_linkedListType

#include <iostream>
#include <string>
#include "addressType.h"
#include "extPersonType.h"
#include "dateType.h"


struct node {
    
    string fName;
    string lName;
    int month;
    int day;
    int year;  
    string streetAddress;
    string city;
    string state;
    string zip;
    string phone;
    string relation;
    
    node *next;
};

class linkedListType{

private:
    node *head;

public:
    linkedListType(){
        head = NULL;        
    }

    void addNode(string first, string last, int month, int day, int year, string street, string city, string state, string zip, string phone, string relation)
    {

        node *temp = new node();
        temp->fName = first;
        temp->lName = last;
        temp->month = month;
        temp->day = day;
        temp->year = year;
        temp->streetAddress = street;
        temp->state = state;
        temp->zip = zip;
        temp->phone = phone;
        temp->relation = relation;
        temp->next = NULL;

                
        if(head == NULL)
        {
            cout << "head was null" << endl;
            head = temp;
            return;           
        }           
            
        //traverse list
        node *lastNode = head;
        while(lastNode->next != NULL) 
        {
            lastNode = lastNode->next;
        }
        
        //once the end is reached,set the null *next node to temp then done
        lastNode->next = temp;        

    }

   



};


#endif


//*****************MAIN.CPP BELOW*********************
#include <iostream>
#include <fstream>
#include <string>
#include "addressBookType.h"
#include "linkedListType.h"

using namespace std; 
 
//void loadAddressBook(addressBookType& adBook, bool& flag);
void buildAddressBookList(bool& flag);
void showMenu();


int main()
{
  cout << "program started" << endl;
    addressBookType addressBook;
    string str;
    string str1;
    string str2; 
    int choice;
    int loc;
    int month;

    bool flag = false;

    //loadAddressBook(addressBook, flag);
    buildAddressBookList(flag);
    


    if (flag)
    {
        cout << "Input file does not exist." << endl
             << "Program terminates!!!!" << endl;
        return 1;
    }

    addressBook.sort();

    showMenu();

    cin >> choice;
    cin.ignore(100, '\n');

    while (choice != 10)
    {
        switch (choice)
        {
        case 1: 
            cout << "Enter the last name of the person: ";
            getline(cin , str);
            cout << endl;

            loc = addressBook.search(str);

            if (loc != -1)
	            cout << str << " is in the address book" << endl;
            else
                cout << str << " is not in the address book" << endl;
            break;
        case 2: 
            cout << "Enter the last name of the person: ";
            getline(cin , str);
            cout << endl;

            loc = addressBook.search(str);

            if (loc != -1)
                addressBook.printAt(loc);
            else
                cout << str << " is not in the address book" << endl;
            break;
        case 3: 
            cout << "Enter the month number: ";
            cin >> month;
            cin.ignore(100, '\n');
            cout << endl;

            addressBook.printNameInTheMonth(month);
            break;
        case 4: 
            cout << "Enter starting last name: ";
            getline(cin , str1);
            cout << endl;
            cout << "Enter ending last name: ";
            getline(cin , str2);
            cout << endl;

            addressBook.printNamesBetweenLastNames(str1, str2);
            break;
		    case 5: 
            cout << "Enter person type Family, Friend, Business: ";
            getline(cin , str);
            cout << endl;

            addressBook.printNamesWithStatus(str);
            break;
        case 6: 
            addressBook.print();
            break;        
        case 7:
            cout << "CASE 7: ADD AN ADDRESS CALLED\n" << endl;
            break;
    case 8:
            cout << "CASE 8: DELETE AN ADDRESS CALLED\n" << endl;
            break;
    case 9:
            cout << "CASE 9: SAVE CHANGES CALLED\n" << endl;
            break;
    default: 
            cout << "Invalid choice" << endl;
            break;
            

        }

        showMenu();
        cin >> choice;
        cin.ignore(100, '\n');
    }

    return 0;
}

void buildAddressBookList(bool& flag)
{
    
    ifstream infile;

    char filename[50];
	
    string first;
    string last;

    int month;
    int day;
    int year;

    string street;
    string city;
    string state;
    string zip;

    string phone;
    string pStatus;

    extPersonType temp;

    cout << "Enter file name: ";
    cin >> filename;
    cout << endl;

    infile.open(filename);
    if (!infile)
    {
        cout << "Input file does not exists. "
			 << "Program terminates!!!" << endl;
        flag = true;
        return;
    }


    infile >> first >> last >> month >> day >> year;
    infile.ignore(100,'\n');
    getline(infile,street);
    getline(infile,city);
    getline(infile, state);
    infile >> zip >> phone >> pStatus;

    linkedListType myLinkedList;
        
    while (infile)
    {
      myLinkedList.addNode(first, last, month, day, year, street, city, state, zip, phone, pStatus);     

      
    }
}
/*
void loadAddressBook(addressBookType& adBook, bool& flag)
{
    ifstream infile;

    char filename[50];
	
    string first;
    string last;

    int month;
    int day;
    int year;

    string street;
    string city;
    string state;
    string zip;

    string phone;
    string pStatus;

    extPersonType temp;

    cout << "Enter file name: ";
    cin >> filename;
    cout << endl;

    infile.open(filename);
    if (!infile)
    {
        cout << "Input file does not exists. "
			 << "Program terminates!!!" << endl;
        flag = true;
        return;
    }

    int i = 0;

    infile >> first >> last >> month >> day >> year;
    infile.ignore(100,'\n');
    getline(infile,street);
    getline(infile,city);
    getline(infile, state);
    infile >> zip >> phone >> pStatus;

    while (infile)
    {
        temp.setInfo(first, last, month, day, year, street, city, 
                     state, zip, phone, pStatus);

        adBook.insertAt(temp, i);
        i++;

        infile >> first >> last >> month >> day >> year;	
        infile.ignore(100, '\n');
        getline(infile, street);
        getline(infile, city);
        getline(infile, state);
        infile >> zip >> phone >> pStatus;
    }
}*/

void showMenu()
{
    cout << "Welcome to the address book program." << endl;
    cout << "Choose among the following options:" << endl;
    cout << "1: To see if a person is in the address book"
         << endl;
    cout << "2: Print the information of a person" << endl;
    cout << "3: Print the names of person having birthday in " 
         << "a particular month" << endl;
    cout << "4: Print the names of persons between two " 
         << "last names" << endl;
    cout << "5: Print the names of persons having a "
         << "particular status" << endl;
    cout << "6: Print the address book" << endl;
    cout << "7: Add an address" << endl;
    cout << "8: Delete an address" << endl;
    cout << "9: Save changes" << endl;
    cout << "10: Terminate the program" << endl;
}



  

test it in small pieces.
can you fire up main and add 3 nodes to your list and validate that those are working?
if that works, then check the next thing ... I found your issue but you may have more and this is too big to test the pieces with now, you need a stub main (just comment out real main and put in a small one to exercise small bits).

line 276 is your infinite loop. read it without the fluff, out loud and you will see it:
while(infile)
add the same node over and over.

you do NOT read from infile in that loop, so it will never stop having data ready to be read in it...

requirements and other stuff from drive by:
- do you NEED the items in the order they were in the file? If not, add to top of list is much less code, much easier to do... new guy, guy->next = head, done (even if head is null this works great).
- if you can't do that, keep a tail pointer at the end of the list and say tail->next = new guy, tail = newguy and you don't have to go thru the list every time, but you need more logic to manage tail if you do that, if head is null you need to set tail in that special condition, etc


consider making the build a bool function so you can say if(buildlist()) instead of all that flag stuff in main. Other functions could also return a value.
could add take a node instead of all the fields one by one? That is begging for getting something out of order etc.

node should have a small ctor to set next to null, to prevent accidents. (yes, struct can have a ctor).

a slightly better design if you find yourself doing this again (with trees, for example, hint hint).

struct data {name, address, whatever junk};
struct node{data d; node*next};

now, you can change data and the list would still work if you remove all the data specific parts out of list and keep them somewhere else (as member functions of data and node as appropriate). Each object does one thing... data stores your info, and that can be swapped out for anything you want later, node wraps that up to make it listified, and list does the pointer magic listy stuff.
Last edited on
Some smaller points.

For C++, use nullptr instead of NULL

Pass std::string by const ref and not by value to avoid an unnecessary copy

Use initialisation list for constructors

Initialise member variables when defined.

You need a destructor to free all the allocated memory

You either need to provide a copy constructor and operator= functions or declare them =delete to prevent the invalid default ones being used (default do a shallow copy rather the needed deep copy).
Thanks for the replies! I got it working. Thanks for the general C++ 'pointers' as well!
Topic archived. No new replies allowed.