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

How do I input a text file to a linked list?

P: 41
Hello everyone,

I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.

my text file looks like this- (the name of the text file is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

Here is one of my sample code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct link
  7. {
  8.     int data;
  9.     link* next;
  10. };
  11.  
  12. class linkedlist
  13. {
  14. private:
  15.     link* first;
  16. public:
  17.     linkedlist()
  18.     {
  19.         first = NULL;
  20.     }
  21.     void additem(int d);
  22.     void display();
  23. };
  24.  
  25. void linkedlist::additem(int d)
  26. {
  27.     link* newlink = new link;
  28.     newlink -> data = d;
  29.     newlink -> next = first;
  30.     first = newlink;
  31. }
  32.  
  33. void linkedlist::display()
  34. {
  35.     link* current = first;
  36.     while( current != NULL)
  37.     {
  38.         cout << current->data << endl;
  39.         current = current->next;
  40.     }
  41. }
  42.  
  43. int main()
  44. {    
  45.     ifstream inputFile("names.txt");
  46.  
  47.     linkedlist li;
  48.  
  49.     li.additem("names.txt");
  50.     li.display();
  51.     return 0;
  52. }
  53.  
  54.  
As you see in the main part I'm not sure how to implement the text file into the linked list.
thank you very much in advance
Nov 4 '07 #1
Share this Question
Share on Google+
1 Reply


gpraghuram
Expert 100+
P: 1,275
Hello everyone,

I need to load a multi-word strings from a file to a linked list in one of my programs, and i'm not sure how to do that.

my text file looks like this- (the name of the text file is: names.txt)

Linda
Martinho
Marla
Jose
Mary
Luka
Joseph
Thiago
Ruba

Here is one of my sample code:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct link
  7. {
  8.     int data;
  9.     link* next;
  10. };
  11.  
  12. class linkedlist
  13. {
  14. private:
  15.     link* first;
  16. public:
  17.     linkedlist()
  18.     {
  19.         first = NULL;
  20.     }
  21.     void additem(int d);
  22.     void display();
  23. };
  24.  
  25. void linkedlist::additem(int d)
  26. {
  27.     link* newlink = new link;
  28.     newlink -> data = d;
  29.     newlink -> next = first;
  30.     first = newlink;
  31. }
  32.  
  33. void linkedlist::display()
  34. {
  35.     link* current = first;
  36.     while( current != NULL)
  37.     {
  38.         cout << current->data << endl;
  39.         current = current->next;
  40.     }
  41. }
  42.  
  43. int main()
  44. {    
  45.     ifstream inputFile("names.txt");
  46.  
  47.     linkedlist li;
  48.  
  49.     li.additem("names.txt");
  50.     li.display();
  51.     return 0;
  52. }
  53.  
  54.  
As you see in the main part I'm not sure how to implement the text file into the linked list.
thank you very much in advance

Hi,
If u want to store every word as one node then you have to write the code to read every word and store it to the list.
Your way of adding the filw eont help.

Raghuram
Nov 5 '07 #2

Post your reply

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