[ad_1]
I'm currently reading a text file labeled "employee.txt", where I'm inserting a new employee into a linked list as certain instruction calls such as "INSERT_EMPLOYEE" are read. I'm curious to know how could I insert an employee into the linked list alphabetically by last name?
My text file:
INSERT_EMPLOYEE
12345
Colin
Herd
65000
40
INSERT_EMPLOYEE
23456
Allen
Craft
70000
35
INSERT_EMPLOYEE
34567
Mike
Smith
60000
30
PRINT_ROSTER
My insertEmployee() function:
class Employee;
class employeeList {
private:
Employee *head;
public:
employeeList()
head = new Employee();
head = NULL;
void insertEmployee(int& id, string& firstName, string& lastName, int& salary, int& hours)
Employee *newEmployee = new Employee(id, firstName, lastName, salary, hours); //create a new Employee
if(head == NULL) //checks if linked list is empty
head = newEmployee; //head is assigned to newEmployee
return;
else //if not equal to NULL (List isn't empty)
Student *tempEmployee = head; //declares a temporary Employee data type to start at head node
while(tempEmployee->next != NULL) //traverses to the end of the linked list
tempEmployee = tempEmployee->next; //move to neighbor node
tempEmployee->next = newEmployee; //initialized to the new Employee created at the beginning of function
;
[ad_2]
لینک منبع