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
- #include <iostream>
- #include <fstream>
- using namespace std;
- struct link
- {
- int data;
- link* next;
- };
- class linkedlist
- {
- private:
- link* first;
- public:
- linkedlist()
- {
- first = NULL;
- }
- void additem(int d);
- void display();
- };
- void linkedlist::additem(int d)
- {
- link* newlink = new link;
- newlink -> data = d;
- newlink -> next = first;
- first = newlink;
- }
- void linkedlist::display()
- {
- link* current = first;
- while( current != NULL)
- {
- cout << current->data << endl;
- current = current->next;
- }
- }
- int main()
- {
- ifstream inputFile("names.txt");
- linkedlist li;
- li.additem("names.txt");
- li.display();
- return 0;
- }
thank you very much in advance