Reading Text File to Linked List
Trying to read a text file to a linked list. The text file looks something like this:
Name: Smith, Joe
ID: 346
Hours: 40
PayRate: 10
The text file will have multiple records as well. Ive tried a few different ways and nothing seems to work. Here is whats I have so far.
This is the Header
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#ifndef MenuProgram_H
#define MenuProgram_H
class linkedlist{
private:
typedef struct node{
char data;
node* next;
}*nodePtr;
nodePtr head;
nodePtr curr;
nodePtr temp;
public://functions
linkedlist();
void AddNode(char addData);
void DisplayList(char addData);
};
#endif
|
Here is the IF statement that calls for the file to be read and added to a list
1 2 3 4
|
if (MenuSelection == 2){
ifstream myfile("Employee_Data.txt");
linkedlist List;
List.AddNode("Employee_Data.txt");
|
Here is the add function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void linkedlist::AddNode(char addData){
nodePtr n = new node;
n->next = NULL;
n->data = addData;
if (head != NULL){
curr = head;
while (curr->next != NULL){
curr = curr->next;
}
curr->next = n;
}
else {
head = n;
}
}
|
Topic archived. No new replies allowed.