السلام عليكم ورحمة الله وبركاته
لم استطع حل هذا السؤال .. حل من احد يستطيع مساعدتي فيه وشرحة لي !
complete the Linked List by adding the following memberfunction to the class employee_list
a. Add a member function to the class employee_list which deletes the last employee has been added to the list.
void employee_list::delete_last_added_employee()
b. Add a member function to the class employee_list which returns the sum of all employees’ salaries.
int employee_list::sum_salaries()
Q2: Edit the program to:
a. Add new Employee whose information is (Ali , 1678 , 7000).
b. Delete the employee which is his id is 1123.
c. Display All employees.
The code:
#include <iostream>
#include <string>
using namespace std;
// class employee
class employee
{
public:
employee (string a, int n, int s)// constructor
{
name = a;
id = n;
salary = s;
link = 0;
}
string name; // employee's name
int id; // employee's ID number
int salary; // employee's salary
employee *link; // pointer to the next employee
};
// class employee_list
class employee_list
{
public:
employee_list(){ first = last = 0;} // constructor
void add_new_employee(string, int, int); // add after last employee;
void delete_employee(int); // delete specific employee passing ID num
void display_employees(); // display all employees
private:
employee* first; // first employee in the list
employee* last; // last employee in the list
};
void employee_list::add_new_employee(string n, int id, int s)
{
if (last != 0) // if list is not empty
{
last->link = new employee(n, id, s);
last = last->link;
}
else
first = last = new employee(n, id, s);
}
void employee_list::delete_employee(int id)
{
if (first != 0) // if list is not empty
{
if ( first == last && id == first->id) // if only one employee in the list
{
delete first;
first = last = 0;
}
// if more than one employee in the list
else if ( id == first->id)
{
employee *temp = first;
first = first->link;
delete temp;// the old first employee is deleted
}
else
{
employee *pre, *temp;
pre = temp = first;
while( temp != 0 && temp->id != id)
{
pre = temp;
temp = temp->link;
}
if (temp != 0)
{
pre->link = temp->link;
if (temp == last)
last = pre;
delete temp;
}
}
}
}
void employee_list::display_employees()
{
if (first == 0)
cout <<"\nThere are no employees in the list\n";
else
{
employee* temp = first;
while (temp != 0)
{
cout <<temp->id<<"\t"<<temp->name<<"\t"<<temp->salary<<endl;
temp = temp->link;
}
}
cout<<"-----------------------------\n";
}
void main()
{
employee_list list1;
list1.display_employees();
list1.add_new_employee("Ahmed", 1121, 5000);
list1.add_new_employee("Naser", 1122, 2000);
list1.add_new_employee("Ahmed", 1123, 8000);
}