C++ Help with linked list employee data program So far i have this: class employ
ID: 3913167 • Letter: C
Question
C++ Help with linked list employee data program
So far i have this:
class employeeList
{
protected:
struct Node
{
string firstName;
string lastName;
Node *Next;
Node(string firstName1,string lastName1, Node *Next1 = NULL)
{
this->firstName = firstName1;
this->lastName = lastName1;
this->Next = Next1;
}
};
Node *head;
Node *ptr;
string tempFirst;
string tempLast;
fstream myfile;
public:
employeeList()
{
head = NULL;
head = new Node(tempFirst,tempLast, NULL);
ptr = head;
}
void displayEmployee()
{
while (ptr != NULL)
{
cout << ptr->firstName << " " << ptr->lastName << endl;
ptr = ptr->Next;
}
}
void addEmployee(string firstName,string lastName)
{
head = new Node(firstName, lastName);
}
void removeEmployee()
{
}
void loadData()
{
myfile.open("Employees.txt",ios::in | ios::out);
while (getline(myfile >> tempFirst,tempLast))
{
addEmployee(tempFirst, tempLast);
}
cout << "Data loaded!" << endl;
myfile.close();
}
void saveData()
{
}
};
any help would be appreciated i guess the problem is creating the actual linked list
Explanation / Answer
Given below is the completed code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
using namespace std;
class employeeList
{
protected:
struct Node
{
string firstName;
string lastName;
Node *Next;
Node(string firstName1,string lastName1, Node *Next1 = NULL)
{
this->firstName = firstName1;
this->lastName = lastName1;
this->Next = Next1;
}
};
Node *head;
Node *tail;
public:
employeeList()
{
head = new Node("", "", NULL); //dummy node
tail = head;
}
void displayEmployees()
{
for(Node *current = head->Next; current != NULL; current = current->Next)
{
cout << current->firstName << " " << current->lastName << endl;
}
}
void addEmployee(string firstName,string lastName)
{
tail->Next = new Node(firstName, lastName);
tail = tail->Next;
}
void removeEmployee(string firstName,string lastName)
{
Node* previous = head; //dummy node
Node* current = previous->Next; //first employee
while(current != NULL)
{
if(current->firstName == firstName && current->lastName == lastName) ///found employee
break;
previous = current;
current = current->Next;
}
if(current != NULL) //found
{
previous->Next = current->Next;
if(current == tail) //deleting last node?
tail = previous; //update tail to point to last but 1 node
delete current;
}
}
void loadData()
{
string filename = "Employees.txt";
ifstream myfile(filename.c_str());
if(myfile.fail())
{
cout << "ERROR: could not open input file "<< filename << endl;
exit(1);
}
string fname, lname;
while (myfile >> fname >> lname)
{
addEmployee(fname, lname);
}
myfile.close();
cout << "Data loaded!" << endl;
}
void saveData()
{
string filename = "Employees.txt";
ofstream myfile(filename.c_str());
for(Node *current = head->Next; current != NULL; current = current->Next)
myfile << current->firstName << " " << current->lastName << endl;
myfile.close();
cout << "Data saved!" << endl;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.