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
- {
- private:
- string m_name;
- int m_age;
- int m_days; //days of employment
- int m_salary;
- public:
- // Setters and Getters
- void set_name(string name) { m_name = name; }
- string get_name() { return m_name; }
- void set_age(int age) { m_age = age; }
- int get_age() { return m_age; }
- void set_days_of_employment(int days) { m_days = days; }
- int get_days_of_employment() { return m_days; }
- void set_salary(int salary) { m_salary = salary; }
- int get_salary() { return m_salary; }
- // Output methods
- string description()
- {
- return "Employee: " + m_name + "\n" +
- " age: " + to_string(m_age) + "\n" +
- " days: " + to_string(m_days) + "\n" +
- " salary: " + to_string(m_salary) +"\n\n";
- }
- void print_description() { std::cout << description(); }
- // Constructor
- Employee( string name, int age = 0, int days_of_employment = 0, int salary = 0 )
- : m_name {name},
- m_age {age},
- m_days {days_of_employment},
- m_salary {salary}
- { }
- };
- struct EmployeeNode
- {
- EmployeeNode * next;
- Employee * employee;
- };
- struct EmployeeNodeBundle
- // Bundle of current and previous EmployeeNodes
- // used in inserting and deleting the nodes
- {
- EmployeeNode * current;
- EmployeeNode * previous;
- };
- class EmployeeList
- /* Employee linked List */
- {
- private:
- size_t length{0};
- EmployeeNode * head{nullptr};
- EmployeeNodeBundle * node_at_index(size_t index);
- public:
- int get_length();
- EmployeeList() = default;
- virtual ~EmployeeList();
- /* not working yet
- Employee * operator [](unsigned int i)
- {
- return employee_at_index(i);
- }
- */
- Employee * employee_at_index(size_t index);
- bool insert_at_index(Employee * employee, size_t index);
- bool delete_at_index(size_t index);
- void add_to_front( Employee *employee );
- void add_to_back( Employee *employee );
- bool delete_employee_at_index( int index);
- void print();
- };
- int EmployeeList::get_length()
- {
- return length;
- }
- bool EmployeeList::delete_at_index(size_t index)
- {
- if (index < 0 || index > length -1 )
- {
- std::cout << " Index is out of bounds" <<std::endl;
- return 1;
- }
- EmployeeNodeBundle * bundle = node_at_index(index);
- if(index == 0)
- {
- head = bundle->current->next;
- }
- else
- {
- bundle->previous->next = bundle->current->next;
- }
- delete bundle->current;
- length--;
- return 0;
- }
- bool EmployeeList::insert_at_index(Employee * employee, size_t index)
- {
- if (index < 0 || index > length)
- {
- std::cout << " Index is out of bounds" <<std::endl;
- return 1;
- }
- EmployeeNodeBundle * bundle = node_at_index(index);
- EmployeeNode * node = new EmployeeNode();
- node->employee = employee;
- node->next = bundle->current;
- if (index == 0)
- {
- head = node;
- }
- else
- {
- bundle->previous->next = node;
- }
- length++;
- return 0;
- }
- EmployeeNodeBundle * EmployeeList::node_at_index(size_t index)
- {
- EmployeeNode * current_node = head;
- EmployeeNode * previous_node = NULL;
- EmployeeNodeBundle * bundle = new EmployeeNodeBundle();
- for(size_t i = 0; i < index; i++)
- {
- previous_node = current_node;
- current_node = current_node->next;
- }
- bundle->current = current_node;
- bundle->previous = previous_node;
- return bundle;
- }
- EmployeeList::~EmployeeList()
- {
- delete head;
- head = NULL;
- }
- void EmployeeList::add_to_front( Employee * employee)
- {
- insert_at_index(employee, 0);
- }
- void EmployeeList::add_to_back( Employee * employee)
- {
- insert_at_index(employee,length);
- }
- Employee * EmployeeList::employee_at_index(size_t index)
- {
- return node_at_index(index)->current->employee;
- }
- void EmployeeList::print()
- {
- EmployeeNode * 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_to_front(&employee1);
- list->add_to_front(&employee2);
- list->add_to_front(&employee3);
- list->add_to_front(&employee4);
- list->print();
- list->insert_at_index(&employee4, 3);
- std::cout << "Inserted employee Alex" << std::endl;
- list->print();
- list->delete_at_index(0);
- std::cout << "Deleted employee at index 2" << std::endl;
- list->print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement