Linked List search Operations

So here I've got a linked list that reads in person data from a file(ss_number, dob, fname, lname, state of residence), then creates a linked list of states and with a linked list of the people who reside in that state inside each state. I can perform a number of operations not shown here. However now I'm trying to find and print the youngest or oldest person from any state that the user inputs, however my print_oldest_or_youngest function prints just some random person from that state and gives me a seg fault, any help solving this problem would be greatly appreciated

-Heres one of 1000 lines from the file
10081957 101440980 Betty Lamprey NY //dob(DD/MM/YYYY) ss_number Fname Lname State of residence)





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



struct person
{
    int dob,ss_number,year,month,day;
    string fname, lname,state;
    bool isstate;
    
    person()
    { year = dob%10000;
         month = (dob/10000)%100;
         day = dob/1000000;}
    person(int a, int b, string c, string d, string e)
    {dob=a; ss_number=b; fname=c; lname=d; state=e; isstate=false;
        }
    void print()
    {
        cout<<dob<<" "<<ss_number<<" "<<fname<<" "<<lname<<" "<<state<<endl;
    }
    
};

template<typename T>struct Link
{
    T*data;
    Link*next;
    
    Link()
    {}
    Link(T*a,Link<T>*C=NULL)
    {
        data=a;
        next=C;
        
    }
    ~Link()
    {delete next;}
    
};
template<typename T>
struct List
{
    Link<T>*Head,*tail;
    int count;
    List(Link<T>*h=NULL, Link<T>*t=NULL)
    {
        Head=h;
        tail=t;
        
    }
    ~List()
    { delete Head;}
    
    void add(T*object)
    {
        {   if (Head == NULL && tail == NULL)
        {   Link<T> * newlink = new Link<T>(object);
            Head = newlink;
            tail = Head;  }
        else
        {   Link<T> * newlink = new Link<T>(object);
            tail->next = newlink;
            tail=newlink;} }
    }

   




    void print_oldest_or_youngest(string state)
    {
        Link<T>*printS=Head;
        //T*S=NULL;   //printS is a link with type state
        
        while(printS!=NULL)
        {
            if(printS->data->sname == state)
            {
                cout << "SEARCH RESULTS FOR STATE: \n" << "Name: " << state <<
                "\n\n";
                
                string a;
                
                cin>>a;
                Link<person>*temp=printS->data->P->Head;
                while(temp!=NULL)
                {
                    if(a=="youngest")
                        find_youngest();
                    else
                        find_oldest();
                    
                    temp=temp->next;
                    temp->data->print();
                    break;}
                
            }
            printS = printS->next;
        }
        
    }
    
    person* find_youngest ()
    {
        Link<person> *temp=Head->data->P->Head;
        int youngY=0;
        int youngM=0;
        int youngD=0;
        person*hold=NULL;
        while(temp!=NULL)
        {
            if(temp->data->year<youngY)
            {
                youngY=temp->data->year;
                hold=temp->data;
                temp=temp->next;
            }
            else if(temp->data->year==youngY)
            {
                if(temp->data->month<youngM)
                {
                    youngM=temp->data->month;
                    hold=temp->data;
                    temp=temp->next;
                }
                else if(temp->data->month==youngM)
                {
                    if(temp->data->day<youngD)
                    {
                        youngD=temp->data->day;
                        hold=temp->data;
                        temp=temp->next;
                    }
                    else temp=temp->next;
                }
                else temp=temp->next;
            }
            else temp=temp->next;
        }
        return hold;
    }
   person* find_oldest()
    {
        Link<person> *temp=Head->data->P->Head;
        int youngY=0;
        int youngM=0;
        int youngD=0;
        person*hold=NULL;
        while(temp!=NULL)
        {
           if(temp->data->year>youngY)
           {
               youngY=temp->data->year;
               hold=temp->data;
               temp=temp->next;
           }
            else if(temp->data->year==youngY)
            {
                if(temp->data->month>youngM)
                {
                    youngM=temp->data->month;
                    hold=temp->data;
                    temp=temp->next;
                }
                else if(temp->data->month==youngM)
                {
                    if(temp->data->day>youngD)
                    {
                        youngD=temp->data->day;
                        hold=temp->data;
                        temp=temp->next;
                    }
                    else temp=temp->next;
                }
                else temp=temp->next;
            }
            else temp=temp->next;
        }
        //cout<<temp->data->fname;
        return hold;
    }
    
    };
struct state
{
    string sname;
    List<person>*P;
    bool isstate;
    state()
    {
        //        P=new person[3000];
    }
    state(string a,List<person> *x)
    {sname=a;
        P=x;
        isstate=true;}
    void print()
    {
        cout<<sname<<endl;
    }
};

List<state>*read_file(string file)//reads file, creates people and states and put people in their states
{
    List<state>* newlist=new List<state>();
        //        Link<T> * head=NULL;
    //        Link<T> * temp=NULL;
    ifstream fin;
    fin.open("data.txt");
    if (fin.fail())
        cout<<"file not found\n";
    if (!fin.fail())
    {
        while(true)
        {
            int a,b;
            string c,d,sn;
            fin>>a>>b>>c>>d>>sn;
            if (fin.fail())break;
            person * p=new person(a,b,c,d,sn);
            Link<state>*temp=newlist->Head;
            while(true)
            {
                if(temp==NULL)
                {
                    state*s=new state(sn,new List<person>());
                      newlist->add(s);
                    temp=newlist->tail;
                    temp->data->P->add(p);
                    
                  
                    break;
                }
                
                if(temp->data->sname == sn)
                {
                    temp->data->P->add(p);
                    break;
                }
                temp=temp->next;
            }
        }
    }
    else
        cout<<"Can't open file";
    fin.close();
    return newlist;
}

int main()
{
    person P;
    string a,b,c;
    cout<<"What file would you like to read in"<<endl;
    
    cin>>a;
   
List<state> *B= read_file(a);
   
  
    
    

    B->print_oldest_or_youngest(f);
   

    //B->find_oldest();
    
 
    
    return 0;
}

Last edited on
Topic archived. No new replies allowed.