Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective: Learning linked list... C++ Problem Specification: An employer would

ID: 3674170 • Letter: O

Question

Objective: Learning linked list... C++

Problem Specification:

An employer would like to maintain a linked list for employees, the data stored is an employee

number (a positive integer), and a yearly salary (a float). Would like you as the programmer to

define the required classes in header files, and the methods in implementation files.

The methods used are:

Insert: Which inserts elements at the beginning of the list, which is the most recent input

          is at the beginning of the list.

Remove: which deletes the last element in the list.

Display: a recursive function that prints the elements of the list, last to first.

Clear: a function that deletes every node from the list and leaves the list empty.

Criteria:

Menu is used to display options and calls methods.

Guards are used.

insert performs its task correctly.

remove performs its task correctly.

display performs it task correctly.

display is recursive.

clear performs it task correctly.

Explanation / Answer

employee.h

#ifndef EMPLOYEE_H
#define EMPLOYEE_H

class employee {
private:
   unsigned int number;
   float salary;
public:
   // default constructor
   employee();

   // constructor with parameteres
   employee(int, float);

   // getters and setter
   int getNumber();
   void setNumber(int);

   float getSalary();
   void setSalary(float);
};

#endif

employee.cpp

#include "employee.h"

employee::employee() {
   number = 0;
   salary = 0.0;
};

employee::employee(int number, float salary) {
   this->number = number;
   this->salary = salary;
}

int employee::getNumber() {
   return number;
}

void employee::setNumber(int number) {
   this->number = number;
}

float employee::getSalary() {
   return salary;
}

void employee::setSalary(float salary) {
   this->salary = salary;
}

employee_list.h

#ifndef EMPLOYEE_LIST_H
#define EMPLOYEE_LIST_H

#include "employee.h"

class node {
private:
   employee* emp;
   node* next;
public:
   // default constructor
   node();

   // constructor with parameters
   node(employee* emp);

   // getters and setters
   employee* getEmployee();
   void setEmployee(employee*);

   node* getNext();
   void setNext(node*);
};

class employee_list {
private:
   node* head;

   // display recursive helper
   void display(node*);
public:
   // default constructor
   employee_list();

   // constructor with parameters
   employee_list(node*);

   // getters and setters
   node* getHead();
   void setHead(node*);

   // member functions
   // insert employee to front
   void insert(int, float);

   // remove employee from end
   void remove();

   // display list recursively
   void display();

   // delete every node in list
   void clear();
};

#endif

employee_list.cpp

#include <bits/stdc++.h>
#include "employee_list.h"
using namespace std;

node::node() {
   emp = NULL;
   next = NULL;
}

node::node(employee* emp) {
   this->emp = emp;
   next = NULL;
}

employee* node::getEmployee() {
   return emp;
}

void node::setEmployee(employee* emp) {
   this->emp = emp;
}

node* node::getNext() {
   return next;
}

void node::setNext(node* next) {
   this->next = next;
}

/** employee_list **/
employee_list::employee_list() {
   head = NULL;
}

employee_list::employee_list(node* head) {
   this->head = head;
}

void employee_list::display(node* head) {
   if (head == NULL) return;
   printf("Number: %d Salary: %f ", head->getEmployee()->getNumber(), head->getEmployee()->getSalary());
   display(head->getNext());
}

void employee_list::display() {
   if (head == NULL) {
       cout << "List is empty! ";
   } else {
       display(head);
   }
}

void employee_list::insert(int number, float salary) {
   employee *newEmployee = new employee(number, salary);
   node *newNode = new node(newEmployee);
   newNode->setNext(head);
   head = newNode;
}

void employee_list::remove() {
   if (head == NULL) {
       cout << "List is empty! ";
       return;
   }
   node* it = head;
   node* prvs = NULL;
   while (it->getNext() != NULL) {
       prvs = it;
       it = it->getNext();
   }
   delete it;
   it = NULL;
   if (prvs != NULL) {
       prvs->setNext(NULL);
   } else {
       head = NULL;
   }
}

void employee_list::clear() {
   while (head != NULL) {
       node* prvs = head;
       head = head->getNext();
       delete prvs;
   }
}

main.cpp

#include <bits/stdc++.h>
#include "employee_list.h"
using namespace std;

int main() {
   employee_list *list = new employee_list();
   while (true) {
       cout << "1. Insert Employee ";
       cout << "2. Remove Last Employee ";
       cout << "3. Display List ";
       cout << "4. Clear List ";
       cout << "5. Exit ";

       int input;
       cout << " Enter choice (1-4): ";
       cin >> input;

       switch (input) {
           case 1:
               int number;
               float salary;
               cout << "Enter employee number and salary: ";
               cin >> number >> salary;
               list->insert(number, salary);
               cout << "Employee added to front of the list ";
               break;
           case 2:
               list->remove();
               cout << "Last employee removed!" << endl;
               break;
           case 3:
               cout << "Displaying List: ";
               list->display();
               break;
           case 4:
               list->clear();
               cout << "List cleared!" << endl;
               break;
           case 5:
               cout << "Thank you!" << endl;
               return 0;
           default:
               cout << "Please enter correct option! ";
       }
   }
   return 0;
}