473,395 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reading from file into linked list

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
1 15447
weaknessforcats
9,208 Expert Mod 8TB
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

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

Similar topics

3
by: Francis Bell | last post by:
Hello, I'm trying to read data from a file and then insert that into a linked list. The way I have it, the program compiles, however, I'm getting a segmentation fault error message when I run...
1
by: Andrej Hocevar | last post by:
Hello, below I've attached a test program reading data from a file and storing that information in a linked list. First problem is that current->p_name will print only the last item in the file...
1
by: Tim | last post by:
I can't seem to figure out why this very simple linked list wont build.. I mean, there is no intelligence, just add to end. Anyway, please let me know if something i can do will make head (the...
4
by: Henk | last post by:
Hi, I am new to the c-programming language and at the moment I am struggling with the following: I want to read a file using fread() and then put it in to memory. I want to use a (singel)...
4
by: scythemk | last post by:
Hi, I am writing a program that, everytime it executes, first loads all information from a file into a linked list of nodes, using struct to define it. After manipulating the data and receiving...
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
8
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.