class - Linked List Operations C++ -


i've got linked list data base using templates compiles fine , lets me print out list of states , lets me search person , print persons data (since these methods work, left them out below save space).

below have print_people_in_state method, need able (given user input of state) print out info on people particular state. right when run it, nothing happens. how can fix this?

if want run code here link file named data.txt (http://rabbit.eng.miami.edu/class/een118/labs/152/dbfile1.txt)

#include <iostream> #include <string> #include <string.h> #include <fstream> using namespace std;   struct person {     int dob,ss_number;     string fname, lname,state;      person()     { }     person(int a, int b, string c, string d, string e)     {dob=a; ss_number=b; fname=c; lname=d; state=e;} };   struct state {     string sname;     person*p;       state()     {}     state(string a)     {sname=a;} };  template<typename t>struct link {     t*data;     link*extradata;     link*next;      link()     {}     link(t*a,link<t>*c=null)     {         data=a;         next=c;      }  }; template<typename t> struct list {int length;     link<t>*head,*tail;     list(link<t>*h=null, link<t>*t=null)     {         head=h;         tail=t;         length=0;     }      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_people_in_state(string search) {     link<state>*temp=head;    if(temp!==null)        {           if(temp->data->p->state==search)              {                cout<<temp->data->p->fname<<endl;               }        //  temp=temp->next; }}     }; list<person>*person_from_file(string file)//reads file  {     list<person>* newlist=new list<person>();     //        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,e;             fin>>a>>b>>c>>d>>e;             if (fin.fail())break;             person * p=new person(a,b,c,d,e);             newlist->add(p);           }     }     else         cout<<"can't open file";     fin.close();     return newlist; } list<state>*state_from_file(string file)//reads file  {     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,e;             fin>>a>>b>>c>>d>>e;             if (fin.fail())break;             state * s=new state(e);             newlist->add(s);           }     }     else         cout<<"can't open file";     fin.close();     return newlist; }  int main() {list<person>*a = person_from_file("data.txt");  list<state> *b= state_from_file("data.txt");           b->print_people_in_state("tx");        return 0; } 

you can simplify link , list. can read file once , fill both lists @ same time. in fact, don't need separate list states, usage confusing.

template<typename t>class link { public:     link(t *data) { data = data; next = null; }     t *data;     link<t> *next; };  template<typename t>struct list {     int count;     link<t> *head, *tail;     list() { head = null; tail = null; count = 0; }      link<t>* adddata(t *data)     {         link<t> *node = new link<t>(data);         count++;         if (head == null)         {             head = tail = node;         }         else         {             tail->next = node;             tail = node;         }         return node;     } };  int main() {     list<person> *list = new list<person>;      ifstream fin("data.txt");     while (fin)     {         int a, b;         string c, d, e;         fin >> >> b >> c >> d >> e;         list->adddata(new person(a, b, c, d, e));     }      link<person> *node = list->head;     while (node)     {         if (node->data->state == "tx")             cout << node->data->fname << endl;         node = node->next;     }      return 0; } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

node.js - How to abort query on demand using Neo4j drivers -