By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,214 Members | 2,099 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,214 IT Pros & Developers. It's quick & easy.

Reading from file into linked list

P: 1
Ok I have a file in it is a record of a persons first and last name. Format is like:
Trevor Johnson
Kevin Smith
Allan Harris

I need to read that file into program and then turn it into a linked list. So on the list I can go Trevor, Kevin, Allan in a straight row but I can also call out there last name when I am on their first name in the list. Sorry if it doesn't make sense trying to explain best I can.

So far I have
Expand|Select|Wrap|Line Numbers
  1. // list.cpp
  2. // simple linked list program
  3.  
  4. #include <STDLIB.H>
  5. #include <STRING>
  6. #include <iostream>
  7. #include <fstream>
  8. #include <iomanip>
  9. #include <cstdlib>
  10.  
  11. using std::cout;
  12. using std::string;
  13. using std::cin;
  14. using std::ios;
  15. using std::cerr;
  16. using std::endl;
  17. using std::setiosflags;
  18. using std::resetiosflags;
  19. using std::setw;
  20. using std::setprecision;
  21. using std::ifstream;
  22.  
  23. int listSearch = 0;
  24.  
  25. // node object for the linked list
  26. struct Node {
  27.     string data;
  28.     Node* nextWep;
  29.     Node* ammoForWep;
  30. };
  31.  
  32. // implement a singly linked list
  33. class LinkedList {protected:
  34.     Node* front;        // pointer to the front of the linked list
  35.     Node* back;         // pointer to the last node in the linked list
  36.  
  37. public:
  38.     // constructs an empty list
  39.     LinkedList() {
  40.         front = back = NULL;
  41.     }
  42.  
  43.     // deletes the list
  44.     ~LinkedList() {
  45.         // remove objects from the list as long as list is not empty
  46.         while(Length() > 0) {
  47.             RemoveFront();
  48.         }
  49.     }
  50.  
  51.     // inserts a node at the front of the list
  52.     void InsertFrontWep(string newValue) {
  53.         Node* newNode = new Node;
  54.         newNode->data = newValue;
  55.         if (front == NULL) {
  56.             // list must be empty so make front & back point to new node
  57.             front = back = newNode;
  58.             newNode->nextWep= NULL;
  59.         } else {
  60.             // list is not empty so insert between front and first node
  61.             newNode->nextWep = front;
  62.             front = newNode;
  63.         }
  64.     }
  65.  
  66.     // search the list for a target value
  67.     // return index if found or -1 if not found
  68.     int Search(string targetVal) {
  69.         Node* p;
  70.         int count = 0;
  71.         for (p = front; p != NULL; p = p->link) {
  72.             if (p->data == targetVal) {
  73.                 return count;
  74.             }
  75.             count++;
  76.         }
  77.         return -1;
  78.     }
  79.  
  80.  
  81.     // removes a node from the front of the list
  82.     int RemoveFront() {
  83.         int returnVal;
  84.         Node *temp;
  85.         if (front != NULL) {
  86.             // list is not empty so remove & return first node
  87.             returnVal = front->data;
  88.             temp = front;
  89.             front = front->link;
  90.             delete temp;
  91.         } else {
  92.             // list is empty just return 0
  93.             returnVal = 0;
  94.         }
  95.         return returnVal;
  96.     }
  97.  
  98.     // returns the length of the list
  99.     int Length() {
  100.         Node* p;
  101.         int count = 0;
  102.         // loop through each node in the list until we find a null value
  103.         for (p = front; p != NULL; p = p->link) {
  104.             count++;
  105.         }
  106.         return count;
  107.     }
  108.  
  109.     // outputs a string containing all the data values in the list
  110.     void Output() {
  111.         Node* p;
  112.         // loop through each node in the list until we find a null value
  113.         for (p = front; p != NULL; p = p->link) {
  114.             cout << p->data << ", ";
  115.         }
  116.     }
  117. };
  118.  
  119. // use inheritance to create a Set class from the LinkedList class
  120. class Set : public LinkedList {
  121. public:
  122.     // insert a new value only if it is unique (not already in the set)
  123.     int Search(string targetVal) {
  124.         Node* p;
  125.         int count = 0;
  126.         for (p = front; p != NULL; p = p->link) {
  127.             if (p->data == targetVal) {
  128.                 return count;
  129.             }
  130.             count++;
  131.         }
  132.         return -1;
  133.     }
  134.  
  135.     void Insert(string newValue) {
  136.         Node* newNode = new Node;
  137.         newNode->data = newValue;
  138.         if (front == NULL) {
  139.             // list must be empty so make front & back point to new node
  140.             front = back = newNode;
  141.             newNode->link = NULL;
  142.         } else {
  143.             listSearch = Search(newValue);
  144.             if(listSearch == -1)
  145.             {
  146.             // list is not empty so insert between front and first node
  147.             newNode->link = front;
  148.             front = newNode;
  149.             }
  150.         }
  151.     }
  152.  
  153.     // make this the union of two sets
  154.     void Union(Set& a, Set& b) {
  155.     Node* p;
  156.     int search = 0;
  157.     for (p = a.front; p != NULL; p = p->link) 
  158.         {
  159.             this->Insert(p->data);//u.Insert(p->data);
  160.         }
  161.         for (p = b.front; p != NULL; p= p->link)
  162.         {
  163.             search = this->Search(p->data);
  164.             if (search == -1)
  165.                 //u.Insert(p->data);
  166.                 this->Insert(p->data);
  167.         }
  168.  
  169.     }
  170.  
  171.     // make this the intersection of two sets
  172.     void Intersection(Set& a, Set& b) 
  173. {// Open Function
  174.         Node* p;
  175.         int search = 0;
  176.         for(p = a.front; p != NULL; p = p->link) { //Open for statment
  177.             search = b.Search(p->data);
  178.             if(search != -1){ // Open if statement
  179.                 //i.Insert(p->data);
  180.                 this->Insert(p->data);
  181.                                             }
  182.                                              } //  Close For statement
  183. } // Close Function
  184.  
  185.  
  186. }; 
  187.  
  188. void main() {
  189.     ifstream inResources("resource.txt", ios::in);
  190.  
  191.     if (!inResources)
  192.     {
  193.         cerr << "File could not be opened\n";
  194.     }
  195.     Set setA, setB, setUnion, setIntersection;
  196.  
  197.     setA.Insert('1');
  198.     setA.Insert('1');
  199.     setA.Insert('1');
  200.     setA.Insert('1');
  201.     setA.Insert('1');
  202.     string text_line;
  203.     cout << "Contents of setA: ";
  204.     setA.Output();
  205.     cout << "\n\n";
  206.  
  207.     setB.Insert('1');
  208.     setB.Insert('1');
  209.     setB.Insert('1');
  210.  
  211.     cout << "Contents of setB: ";
  212.     setB.Output();
  213.     cout << "\n\n";
  214.  
  215.     setUnion.Union(setA, setB);
  216.     cout << "Contents of setA union setB: ";
  217.     setUnion.Output();
  218.     cout << "\n\n";
  219.  
  220.     setIntersection.Intersection(setA, setB);
  221.     cout << "Contents of setA intersection setB: ";
  222.     setIntersection.Output();
  223.     cout << "\n\n";
  224. }
  225.  
Don't mind the main I just was testing the list before hand. I don't want help with the whole assignment just looking for a push forward in the right direction with that linked list.
Aug 10 '07 #1
Share this Question
Share on Google+
1 Reply


weaknessforcats
Expert Mod 5K+
P: 9,197
Is this some kind of class homework assignment??

Usually, in C++ you use the list template and get on with it. Linked lists have been written to death so unless this is some sort of academic exercise, just use the template.
Aug 10 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.