Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.44 KB | None | 0 0
  1. /* * * *
  2.  * Напишите обьевление класса для связного списка класса узлов обьектов типа
  3.  * ( Worker , *** Pencil , Cat ... ) и его реализацию, включив у нее такие методы :
  4.  * Insert () , Delete () , Display(), Find() и оператор []. протестировать в главной программе
  5.  *
  6.  * * * * */
  7. #include<iostream>
  8. #include<cstdlib>
  9. #include<string>
  10.  
  11. using namespace std;
  12. class Employee;
  13. class EmployeeList;
  14.  
  15. struct Node
  16. {
  17.     Node * next;
  18.     Employee * employee;
  19. };
  20.  
  21. class Employee
  22. {  
  23.     private:
  24.         string name;
  25.         int age;
  26.         int days_of_employment;
  27.         int salary;
  28.  
  29.     public:
  30.         // Setters and Getters
  31.         void set_name(string name) { this->name = name; }
  32.         string get_name() { return this->name; }
  33.         void set_age(int age) { this->age = age; }
  34.         int get_age() { return this->age; }
  35.         void set_days_of_employment(int days) { this->days_of_employment = days; }
  36.         int get_days_of_employment() { return this->days_of_employment; }
  37.         void set_salary(int salary) { this->salary = salary; }
  38.         int get_salary() { return this->salary; }
  39.  
  40.         // Output methods
  41.         string description()
  42.         {
  43.             return "Employee: " + this->name + "\n" +
  44.                    "     age: " + to_string(this->age) + "\n" +
  45.                    "    days: " + to_string(this->days_of_employment) + "\n" +
  46.                    "  salary: " + to_string(this->salary) +"\n\n";
  47.         }
  48.  
  49.         void print_description() { std::cout << description(); }
  50.  
  51.         // Constructor
  52.         Employee( string name, int age = 0, int days_of_employment = 0, int salary = 0 )
  53.         {
  54.             this->name = name;
  55.             this->age = age;
  56.             this->days_of_employment = days_of_employment;
  57.             this->salary = salary;
  58.         }
  59. };
  60.  
  61. class EmployeeList
  62. /* Employee linked List */
  63. {
  64.     private:
  65.         Node * node_at_index(size_t index);
  66.     public:
  67.         int length;
  68.         Node * head;
  69.  
  70.         //Node * node_at_index(size_t index);
  71.  
  72.         EmployeeList();
  73.         ~EmployeeList();
  74.         Employee * operator [](unsigned int i)
  75.         {
  76.             // As of now return the first employee
  77.             return head->employee;
  78.         }
  79.         Employee * employee_at_index(size_t index);
  80.         void add_employee_to_front( Employee *employee );
  81.         void add_employee_to_end( Employee *employee );
  82.         bool delete_employee_at_index( int index);
  83.         void print();
  84. };
  85.  
  86. Node * EmployeeList::node_at_index(size_t index)
  87. {
  88.     Node * node = this->head;
  89.     for(size_t i = 0; i < index; i++)
  90.     {
  91.         node = node->next;
  92.     }
  93.     return node;
  94. }
  95.  
  96. EmployeeList::EmployeeList()
  97. {
  98.     this->length = 0;
  99.     this->head = nullptr;
  100. }
  101.  
  102. EmployeeList::~EmployeeList() { }
  103.  
  104. void EmployeeList::add_employee_to_front( Employee * employee)
  105. {
  106.     Node * node = new Node();
  107.     node->employee = employee;
  108.     node->next = this->head;
  109.     this->head = node;
  110.     this->length++;
  111. }
  112.  
  113. void EmployeeList::add_employee_to_end( Employee * employee)
  114. {
  115.     if(length) // Don't try to add to the end of an empty list.
  116.     {
  117.         Node * node = new Node();
  118.         node->employee = employee;
  119.         node->next = nullptr; //we are at the end, so set the next to null
  120.         node_at_index(length - 1)->next = node;
  121.         length ++;
  122.     }
  123.     else
  124.     {
  125.         add_employee_to_front( employee );
  126.     }
  127. }
  128.  
  129. bool EmployeeList::delete_employee_at_index( int index )
  130. {
  131.     if( index > length -1 || index < 0) // Check if the index is valid
  132.     {
  133.         std::cout << "Index is out of bounds" << std::endl;
  134.         return 1; //index is out of bounds
  135.     }
  136.  
  137.     if(index == 0) //deleting the first element
  138.     {
  139.         this->head = node_at_index( index +1); // by assigning the pointer to the next element in the list
  140.     }
  141.     else if (index == length - 1) //deleting the last element
  142.     {
  143.         node_at_index(index - 1)->next = nullptr; //by setting to null the pointer of the previous element
  144.     }
  145.     else //element in the middle of the list
  146.     {
  147.         node_at_index(index -1 )->next = node_at_index(index + 1);
  148.     }
  149.    
  150.     this->length--;
  151.  
  152.  
  153. return 0;
  154. }
  155.  
  156. Employee * EmployeeList::employee_at_index(size_t index)
  157. {
  158.     return node_at_index( index)->employee;
  159. }
  160.  
  161. void EmployeeList::print()
  162. {
  163.     Node * head = this->head;
  164.     int i = 1;
  165.     while(head)
  166.     {
  167.         cout << head->employee->description();
  168.         head = head->next;
  169.         i++;
  170.     }
  171. }
  172. int main(int argc, char const *argv[])
  173. {
  174.     Employee    employee1 = Employee("Anatoly Shirokov", 31, 365, 280000),
  175.                 employee2 = Employee( "DarkPerl", 32, 780, 180000),
  176.                 employee3 = Employee("Illia Zviagin", 35, 180, 220000),
  177.                 employee4 = Employee("Alex", 18, 12, 6000);
  178.  
  179.     EmployeeList * list = new EmployeeList();
  180.    
  181.    
  182.     list->add_employee_to_end( &employee1);
  183.     list->add_employee_to_end( &employee4);
  184.     list->add_employee_to_end( &employee2);
  185.     list->add_employee_to_end( &employee3);
  186.  
  187.     list->print();
  188.  
  189.     std::cout << "Deleting" <<std::endl;
  190.     std::cout <<list->employee_at_index(1)->description();
  191.     list->delete_employee_at_index(1);
  192.  
  193.     std::cout << "Printing new list" <<std::endl;
  194.     list->print();
  195.     delete list;
  196.    
  197.     return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement