473,543 Members | 3,906 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from file into linked list

1 New Member
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 15504
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
2955
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 the program. I'm fairly new at the pointer business, and I'd appreciate any advice. Here's my code: int main() { ifstream fin;
1
3034
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 for each item. Where is the problem? Is it in the sscanf? Whay can't I abandon tmp_name and just use p_name instead in that sscanf? Second, the order...
1
2028
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 root pointer) not be null. /* LINKED LIST DEFINITIONS */ typedef struct a_fnode {
4
3826
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) linked list to continousely (dynamically) free up more memory when needed and put the buffers with the data of the file on a sort of stack. The...
4
3780
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 user input, the program once again outputs the linked list information to the same file. My problem is that when I'm first trying to get the info from...
5
3346
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 I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran...
6
4144
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 try my own small program. The problem is, I seem to be having trouble with memory, i.e. sometimes my program will work and display the correct output,...
8
718
by: dmp | last post by:
What are Linked list? Please somebody show some ready made programs of linked list
0
8612
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 anything from a short integer value to a complex struct type, also has a pointer to the next node in the single-linked list. That pointer will be NULL...
0
7402
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7347
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7733
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7344
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7684
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3388
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1814
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.