Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* * * *
- * Напишите обьевление класса для связного списка класса узлов обьектов типа
- * ( Worker , *** Pencil , Cat ... ) и его реализацию, включив у нее такие методы :
- * Insert () , Delete () , Display(), Find() и оператор []. протестировать в главной программе
- *
- * * * * */
- #include<iostream>
- #include<cstdlib>
- #include<string>
- using namespace std;
- class Employee;
- class EmployeeList;
- struct Node
- {
- Node * next;
- Employee * employee;
- };
- class Employee
- {
- private:
- string name;
- int age;
- int days_of_employment;
- int salary;
- public:
- // Setters and Getters
- void set_name(string name) { this->name = name; }
- string get_name() { return this->name; }
- void set_age(int age) { this->age = age; }
- int get_age() { return this->age; }
- void set_days_of_employment(int days) { this->days_of_employment = days; }
- int get_days_of_employment() { return this->days_of_employment; }
- void set_salary(int salary) { this->salary = salary; }
- int get_salary() { return this->salary; }
- // Output methods
- string description()
- {
- return "Employee: " + this->name + "\n" +
- " age: " + to_string(this->age) + "\n" +
- " days: " + to_string(this->days_of_employment) + "\n" +
- " salary: " + to_string(this->salary) +"\n\n";
- }
- void print_description() { std::cout << description(); }
- // Constructor
- Employee( string name, int age = 0, int days_of_employment = 0, int salary = 0 )
- {
- this->name = name;
- this->age = age;
- this->days_of_employment = days_of_employment;
- this->salary = salary;
- }
- };
- class EmployeeList
- /* Employee linked List */
- {
- private:
- Node * node_at_index(size_t index);
- public:
- int length;
- Node * head;
- //Node * node_at_index(size_t index);
- EmployeeList();
- ~EmployeeList();
- Employee * operator [](unsigned int i)
- {
- // As of now return the first employee
- return head->employee;
- }
- Employee * employee_at_index(size_t index);
- void add_employee_to_front( Employee *employee );
- void add_employee_to_end( Employee *employee );
- bool delete_employee_at_index( int index);
- void print();
- };
- Node * EmployeeList::node_at_index(size_t index)
- {
- Node * node = this->head;
- for(size_t i = 0; i < index; i++)
- {
- node = node->next;
- }
- return node;
- }
- EmployeeList::EmployeeList()
- {
- this->length = 0;
- this->head = nullptr;
- }
- EmployeeList::~EmployeeList() { }
- void EmployeeList::add_employee_to_front( Employee * employee)
- {
- Node * node = new Node();
- node->employee = employee;
- node->next = this->head;
- this->head = node;
- this->length++;
- }
- void EmployeeList::add_employee_to_end( Employee * employee)
- {
- if(length) // Don't try to add to the end of an empty list.
- {
- Node * node = new Node();
- node->employee = employee;
- node->next = nullptr; //we are at the end, so set the next to null
- node_at_index(length - 1)->next = node;
- length ++;
- }
- else
- {
- add_employee_to_front( employee );
- }
- }
- bool EmployeeList::delete_employee_at_index( int index )
- {
- if( index > length -1 || index < 0) // Check if the index is valid
- {
- std::cout << "Index is out of bounds" << std::endl;
- return 1; //index is out of bounds
- }
- if(index == 0) //deleting the first element
- {
- this->head = node_at_index( index +1); // by assigning the pointer to the next element in the list
- }
- else if (index == length - 1) //deleting the last element
- {
- node_at_index(index - 1)->next = nullptr; //by setting to null the pointer of the previous element
- }
- else //element in the middle of the list
- {
- node_at_index(index -1 )->next = node_at_index(index + 1);
- }
- this->length--;
- return 0;
- }
- Employee * EmployeeList::employee_at_index(size_t index)
- {
- return node_at_index( index)->employee;
- }
- void EmployeeList::print()
- {
- Node * head = this->head;
- int i = 1;
- while(head)
- {
- cout << head->employee->description();
- head = head->next;
- i++;
- }
- }
- int main(int argc, char const *argv[])
- {
- Employee employee1 = Employee("Anatoly Shirokov", 31, 365, 280000),
- employee2 = Employee( "DarkPerl", 32, 780, 180000),
- employee3 = Employee("Illia Zviagin", 35, 180, 220000),
- employee4 = Employee("Alex", 18, 12, 6000);
- EmployeeList * list = new EmployeeList();
- list->add_employee_to_end( &employee1);
- list->add_employee_to_end( &employee4);
- list->add_employee_to_end( &employee2);
- list->add_employee_to_end( &employee3);
- list->print();
- std::cout << "Deleting" <<std::endl;
- std::cout <<list->employee_at_index(1)->description();
- list->delete_employee_at_index(1);
- std::cout << "Printing new list" <<std::endl;
- list->print();
- delete list;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement