Question

In: Computer Science

In this homework, you will implement a single linked list to store a list of employees...

In this homework, you will implement a single linked list to store a list of employees in a company. Every employee has an ID, name, department, and salary. You will create 2 classes: Employee and EmployeeList. Employee class should have all information about an employee and also a “next” pointer. See below:

Employee

Type

Attribute

int

ID

string

name

string

department

int

salary

Employee*

next

Return Type

Function

(constructor)

Employee(int ID, string name, string department, int salary)

EmployeeList class should have a head pointer and a size as attributes. The head pointer points to list of the employee nodes. In the class, following member functions should be implemented:

EmployeeList

Type

Attribute

Employee*

head

int

size

Return Type

Function

(constructor)

EmployeeList()

void

addEmployee(int ID, string name, string department, int salary)

void

removeEmployee(int ID)

void

print()

void

print(string department)

void

print(int ID)

int

getSize()

bool

isEmpty()

addEmployee (): Adds a new employee to the list. The new employee is placed based on his/her salary from lowest to highest. For example, if you have following three employees:

A new employee, “11 / Mike / Marketing / $65000” will be placed between Jim and John since his salary is more than $50000 and less than $70000.

removeEmployee(): This functions will remove the employee using by his/her ID. If the given ID number is not in the list, it will give an error.

print(): Prints all employees in (salary) order. ID, name, department, and salary should be printed for each employee.

print(string department): Prints all employees whose department matches with the user input department.

print(int ID): Prints the employee with given input ID.

getSize(): returns the number of employees in the list.

isEmpty(): returns true if list is empty, returns false otherwise.

The main program is provided for you. So you will only implement the Employee and EmployeeList classes. I expect you to have 2 files: EmployeeList.h and EmployeeList.cpp. Employee class definition should be in the EmployeeList.h file.

main.cpp includes all necessary functions to read the dataset file (dataset.txt). Also, several test cases are prepared for you to see whether your code is running or not. You do not need to change any code in the main.cpp file.

Solutions

Expert Solution

Working code implemented in C++ and appropriate comments provided for better understanding.

Here I am attaching code for all files:

  • EmployeeList.cpp
  • EmployeeList.h
  • main.cpp

EmployeeList.cpp:

#include "EmployeeList.h"
#include <iostream>

EmployeeList::EmployeeList()
{
   head = new Employee();
   size = 0;
}

// adds a new emplyee to the list, sorted by salary
void EmployeeList::addEmployee(int ID, std::string name, std::string department, int salary)
{
   Employee* newEmployee = new Employee(ID, name, department, salary);
   Employee* current = head;
   Employee* previous = head;

   // adds the first element to the linked list
   if (EmployeeList::isEmpty())
   {
       head = newEmployee;
       size++;
   }
   else
   {
       // if the salary of the new node is less than the head, then it makes the new node the head node
       if (head->salary > newEmployee->salary)
       {
           newEmployee->next = head;
           head = newEmployee;
       }

       // single case for if the 2nd node added is less than the head
       else if (head->next == NULL && head->salary < newEmployee->salary)
       {
           head->next = newEmployee;
       }
       else
       {
           // loop through the list to add the node
           for (int i = 0; i < size - 1; i++)
           {
               previous = current;
               current = current->next;

               // general case for a new lower salary node
               if (current->salary > newEmployee->salary)
               {
                   newEmployee->next = current;
                   previous->next = newEmployee;
                   break;
               }

               // general case for if the last node in the list is already less then the new node
               else if (current->salary < newEmployee->salary && current->next == NULL)
               {
                   current->next = newEmployee;
                   break;
               }
           }
       }
       size++;
   }
}

// removes an employee by ID
void EmployeeList::removeEmployee(int ID)
{
   Employee* current = head;
   Employee* deletedNode;

   int foundIndex;

   // if you try to remove from an empty list
   if (EmployeeList::getSize() == 0)
   {
       std::cout << "List is empty!\n";
   }
   else
   {
       // find the index of the ID given
       for (int i = 0; i < size; i++)
       {
           // if you find the ID at the current, then break from the loop
           if (current->ID == ID)
           {
               foundIndex = i;
               break;
           }
           else
           {
               // set the index to null and cycle the current
               foundIndex = -1;
               current = current->next;
           }
       }

       // special case for if the first index is chosen
       if (foundIndex == 0)
       {
           deletedNode = head;
           head = head->next;
           size--;
           delete deletedNode;
       }

       // error message for if the ID is not found
       else if (foundIndex == -1)
       {
           std::cout << "ID not found!\n";
       }

       // general case to find the node to delete
       else
       {
           current = head;

           for (int i = 0; i < foundIndex - 1; i++)
           {
               current = current->next;
           }
           deletedNode = current->next;

           // reconnecting the nodes if it is not the last node
           if (foundIndex != size - 1)
           {
               current->next = deletedNode->next;
           }

           size--;
           delete deletedNode;
       }
   }
}

// prints all employees in the list
void EmployeeList::print()
{
   Employee* current = head;

   int n = 0;

   // check if the list is empty
   if (EmployeeList::getSize() == 0)
   {
       std::cout << "List is empty!\n";
   }
   else {
       for (int i = 0; i < size; i++)
       {
           std::cout << current->ID << " " << current->name << " " << current->department << " " << current->salary << "\n";
           current = current->next;
       }
       std::cout << std::endl;
   }
}

// prints employees by department
void EmployeeList::print(std::string department)
{
   Employee* current = head;

   // check if the list is empty
   if (EmployeeList::getSize() == 0)
   {
       std::cout << "List is empty!\n";
   }
   else {

       // convert the input to uppercase for direct comparison
       for (size_t i = 0; i < department.length(); i++)
       {
           department[i] = toupper(department[i]);
       }

       // check if the department exists
       if (department == "TRAINING" || department == "MARKETING" || department == "SERVICES" || department == "SUPPORT" ||
           department == "ACCOUNTING" || department == "LEGAL" || department == "RESEARCH&DEVELOPMENT" || department == "ENGINEERING" ||
           department == "MANAGEMENT" || department == "DEVELOPMENT")
       {
           for (int i = 0; i < size; i++)
           {
               // convert the node's department to uppercase
               std::string checkDepartment = current->department;

               for (size_t i = 0; i < checkDepartment.length(); i++)
               {
                   checkDepartment[i] = toupper(checkDepartment[i]);
               }

               // print by department if they are equal
               if (checkDepartment == department)
               {
                   std::cout << current->ID << " " << current->name << " " << current->department << " " << current->salary << "\n";
               }
               current = current->next;
           }
       }
       else
       {
           std::cout << "This department doesn't exist!" << std::endl;
       }
       std::cout << std::endl;
   }
}

// prints employees by ID
void EmployeeList::print(int ID)
{
   Employee* current = head;
   bool iDFound = false;

   // check if the list is empty
   if (EmployeeList::getSize() == 0)
   {
       std::cout << "List is empty!\n";
   }
   else {
       for (int i = 0; i < size; i++)
       {
           if (current->ID == ID)
           {
               std::cout << current->ID << " " << current->name << " " << current->department << " " << current->salary << "\n";
               iDFound = true;
               break;
           }
           current = current->next;
       }

       // if the ID does exist in the list
       if (iDFound == false)
       {
           std::cout << "This ID doesn't not exist!" << std::endl;
       }
       std::cout << std::endl;
   }
}

// returns the size of the list
int EmployeeList::getSize()
{
   return size;
}

// returns if the list is empty or not
bool EmployeeList::isEmpty()
{
   if (size == 0)
   {
       return true;
   }
   else
   {
       return false;
   }
}


EmployeeList.h:

#include <string>

class Employee {

private:
   int ID;
   std::string name;
   std::string department;
   int salary;

   Employee* next;

   friend class EmployeeList;

public:
   // default empty constructor
   Employee() {

       next = NULL;
   }

   // loaded constructor of all values
   Employee(int _ID, std::string _name, std::string _department, int _salary) {
       ID = _ID;
       name = _name;
       department = _department;
       salary = _salary;

       next = NULL;
   }
};

class EmployeeList
{

private:
   Employee * head;
   int size;

public:
   EmployeeList();
   void addEmployee(int ID, std::string name, std::string department, int salary);
   void removeEmployee(int ID);
   void print();
   void print(std::string department);
   void print(int ID);
   int getSize();
   bool isEmpty();
};

main.cpp:

#include <iostream>
#include <fstream>
#include <string>
#include "EmployeeList.h"

using namespace std;

EmployeeList myCompany;

//Read the dataset file and loads myCompany list
void loadDataset(string fileName) {
   ifstream inFile;
   inFile.open(fileName.c_str());

   //Add all employees to the list
   int ID, salary;
   string name, dept;
   while (inFile >> ID >> name >> dept >> salary)
       myCompany.addEmployee(ID, name, dept, salary);

   inFile.close();
}

//Just for convenience...
void continueMessage(string message) {
   cout << message << endl;
   cout << "Press Enter to continue.." << endl; cin.get();
}

int main() {

   string fileName = "dataset.txt";
   loadDataset(fileName);

   continueMessage("Dataset file is loaded to the program!");

   //---------------------------------------------------------------------------
   myCompany.print();
   continueMessage("All employees in the company are listed!");

   //---------------------------------------------------------------------------
   int ID = 1;
   string name = "Mike";
   string dept = "Training";
   int salary = 94123;
   myCompany.addEmployee(ID, name, dept, salary);

   continueMessage("New employee is added to the company.");
   //---------------------------------------------------------------------------

   myCompany.print(ID);
   continueMessage("The employee having ID:1 listed.");
   //---------------------------------------------------------------------------

   myCompany.print();
   continueMessage("All employees in the company are listed!");
   //---------------------------------------------------------------------------

   myCompany.print(dept);
   continueMessage("The employees in " + dept + " are listed.");
   //---------------------------------------------------------------------------

   myCompany.removeEmployee(ID);
   continueMessage("The employee having ID:1 is removed from the company.");
   //---------------------------------------------------------------------------

   if (myCompany.isEmpty())
       cout << "The company does not have any employees" << endl;
   else {
       int size = myCompany.getSize();
       cout << "There are " << size << " employees in the company." << endl;
   }
   //---------------------------------------------------------------------------

   system("pause");

   return 0;
}

Sample Output Screenshots:


Related Solutions

(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the...
In this assignment, you will implement a Polynomial linked list, the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined.
Assume that you want to implement binary search with a linked list. What would be the...
Assume that you want to implement binary search with a linked list. What would be the performance of this algorithm? Compare and contrast this algorithm with the implementation of binary search on traditional sorted array and give a discussion. Your discussion and analysis must explain what the possibilities, issues and consequences of such design are, and then explain whether these issues would exist in the traditional array approach. Your answer can be around 1-2 paragraph of writing backed-up with algorithmic...
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT