Advertisement
Guest User

Untitled

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