Question
Statement The goal is to implement a singly linked pointer based list using an employee class. The employee class should contain the employee information listed
Statement
The goal is to implement a singly linked pointer based list using an employee class. The employee class should contain the employee information listed in the "input" section below. The program should contain all functions listed in the functionality section below.
Input
Data about current employees should be on the file "Employee.txt". Each employee should have the following attributes: Employee_Number Integer Employee_Last_Name String Employee_First_Name String Employee_Years_of_Service Integer 3 employees must be loaded from the Employee.txt file on program start up.
Functionality
Command | Processing | |
ADD |
| |
REMOVE |
| |
COUNT |
| |
| ||
QUIT |
|
Output
All output should be on the console. Data Structures This program should utilize a singly linked pointer based list. Deliverables Neat and well commented program Complete zipped Visual Studio Project
I am struggling to split the code into multiple classes rather than just one. Curious if anyone knew how to do that without getting errors.
Main.cpp
#include
#include "Node.h" #include "List.h"
using namespace std;
int main() { // New list List list;
// Append nodes to the list
string line; ifstream fin; //for reading the data from file Employee.txt fin.open("Employee.txt"); if (fin.is_open()) { while (getline(fin, line)) { stringstream ss(line); int emp_number, emp_year_of_service; string first_name, last_name; ss >> emp_number; ss >> first_name; ss >> last_name; ss >> emp_year_of_service; //entering atleast three records from the file employee.txt to the list list.Append(emp_number, first_name, last_name, emp_year_of_service); } }
//here is the command utility
string command; cout << "Enter the command:"; cin >> command; while (command != "QUIT") { //PRINT COMMAND ACTION if (command == "PRINT")list.Print();
//ADD COMMAND ACTION if (command == "ADD") { int emp_number, emp_year_of_service; string first_name, last_name; cout << "Enter Employee Number: "; cin >> emp_number; cout << "Enter First Name: "; cin >> first_name; cout << "Enter Last Name: "; cin >> last_name; cout << "Enter Year of Service: "; cin >> emp_year_of_service; list.Append(emp_number, first_name, last_name, emp_year_of_service); }
//REMOVE COMMAND ACTION if (command == "REMOVE") { int emp_number, emp_year_of_service; string first_name, last_name; cout << "Enter Employee Number: "; cin >> emp_number; cout << "Enter First Name: "; cin >> first_name; cout << "Enter Last Name: "; cin >> last_name; cout << "Enter Year of Service: "; cin >> emp_year_of_service; list.Delete(emp_number, first_name, last_name, emp_year_of_service); }
//COUNT COMMAND ACTION if (command == "COUNT")cout << "No of employees in the list is: " << list.Count();
//AGAIN ENTER THE COMMAND cout << "Enter the command:"; cin >> command; }
}
Node.h
#include
// Node class class Node {
int emp_number; string emp_last_name; string emp_first_name; int emp_year_of_service; Node* next;
public: //constructor Node() {};
//function for assigning employees details void SetData(int e_no,string first_name,string last_name,int year_of_service){ emp_number=e_no; emp_first_name=first_name; emp_last_name=last_name; emp_year_of_service=year_of_service; };
//for setting the next node void SetNext(Node* aNext){ next = aNext; };
//for returning the data of the node int return_emp_no() { return emp_number; }; string return_first_name() { return emp_first_name; }; string return_last_name() { return emp_last_name; }; int year_of_service() { return emp_year_of_service; };
Node* Next() { return next; };
};
List.h
// List class #include
using namespace std;
class List { Node* head; public: List() { head = NULL; }; void Print(); int Count(); void Append(int emp_number, string first_name, string last_name, int emp_year_of_service); void Delete(int emp_number, string first_name, string last_name, int emp_year_of_service); };
/** * function for PRINT command */ void List::Print() {
// Temp pointer Node* tmp = head;
// No nodes if (tmp == NULL) { cout << "EMPTY" << endl; return; }
// One node in the list if (tmp->Next() == NULL) { cout << "EMPLOYEE NO IS:" << tmp->return_emp_no() << ", FIRST NAME: " << tmp->return_first_name() << ", LAST NAME: " << tmp->return_last_name() << ", EMPLOYEE year_of_service: " << tmp->year_of_service(); cout << endl; cout << "---->"; cout << "NULL" << endl; } else { // Parse and print the list do { cout << "EMPLOYEE NO IS:" << tmp->return_emp_no() << ", FIRST NAME: " << tmp->return_first_name() << ", LAST NAME: " << tmp->return_last_name() << ", EMPLOYEE year_of_service: " << tmp->year_of_service(); cout << endl; cout << "---->"; tmp = tmp->Next(); } while (tmp != NULL);
cout << "NULL" << endl; } }
//function for COUNT command int List::Count() {
// Temp pointer Node* tmp = head; int i = 0; // No nodes if (tmp == NULL) { cout << "EMPTY" << endl; return i; }
// One node in the list if (tmp->Next() == NULL) { return 1; } else { // Parse and print the list do { i++; tmp = tmp->Next(); } while (tmp != NULL);
return i; } }
/** * Append a node to the linked list */ void List::Append(int emp_number, string first_name, string last_name, int year_of_service) {
// Create a new node Node* newNode = new Node(); newNode->SetData(emp_number, first_name, last_name, year_of_service); newNode->SetNext(NULL);
// Create a temp pointer Node* tmp = head;
if (tmp != NULL) { // Nodes already present in the list // Parse to end of list while (tmp->Next() != NULL) { tmp = tmp->Next(); }
// Point the last node to the new node tmp->SetNext(newNode); } else { // First node in the list head = newNode; } }
/** * Delete a node from the list */ void List::Delete(int emp_number, string first_name, string last_name, int emp_year_of_service) {
// Create a temp pointer Node* tmp = head;
// No nodes if (tmp == NULL) return;
// first node of the list if (tmp->return_emp_no() == emp_number && first_name.compare(tmp->return_first_name()) == 0 && last_name.compare(tmp->return_last_name()) == 0 && tmp->year_of_service() == emp_year_of_service) { head = tmp->Next(); delete tmp; } else { // Parse thru the nodes Node* prev = NULL; do { if (tmp->return_emp_no() == emp_number && first_name.compare(tmp->return_first_name()) == 0 && last_name.compare(tmp->return_last_name()) == 0 && tmp->year_of_service() == emp_year_of_service) break; prev = tmp; tmp = tmp->Next(); } while (tmp != NULL);
// Adjust the pointers prev->SetNext(tmp->Next());
// Delete the current node delete tmp; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started