dsa3.
cpp Fri Aug 08 10:25:52 2025 1
#include<iostream>
using namespace std;
const int MAX_SIZE = 100; // Maximum size for character arrays
struct Employee
{
int employeeId;
char employeeName[MAX_SIZE];
char fatherName[MAX_SIZE];
char motherName[MAX_SIZE];
char position[MAX_SIZE];
char department[MAX_SIZE];
char dateOfJoining[MAX_SIZE];
char address[MAX_SIZE];
char houseNo[MAX_SIZE];
char streetName[MAX_SIZE];
char city[MAX_SIZE];
char state[MAX_SIZE];
char country[MAX_SIZE];
char pin[MAX_SIZE];
Employee *next;
};
class EmployeeList
{
private:
Employee *head;
bool isDuplicate(int employeeId);
void displayEmployee(Employee *emp);
static bool isValidChar(char c);
void getValidString(char *str, const char *prompt);
public:
EmployeeList() : head(nullptr) {}
void insertRecord();
void deleteRecord();
void findRecord();
void updateRecord();
void displayRecordsByLocation();
void generateCityWiseReport();
void showMenu();
};
// Function to check for duplicate employee records
bool EmployeeList::isDuplicate(int employeeId)
{
Employee *temp = head;
while (temp != nullptr)
{
if (temp->employeeId == employeeId)
{
return true; // Duplicate record found
}
temp = temp->next;
}
return false;
}
/
bool EmployeeList::isValidChar(char c)
{
return (c >= ’a’ && c <= ’z’) || (c >= ’A’ && c <= ’Z’) || c == ’.’;
}
void EmployeeList::getValidString(char *str, const char *prompt)
dsa3.cpp Fri Aug 08 10:25:52 2025 2
{
bool valid;
char temp[MAX_SIZE];
do
{
valid = true;
cout << prompt;
cin.getline(temp, MAX_SIZE);
int i = 0;
while (temp[i])
{
if (!isValidChar(temp[i])) // Check for invalid characters
{
valid = false;
cout << "Invalid character found. Please enter again." << endl;
break;
}
i++;
}
if (valid)
{
strncpy(str, temp, MAX_SIZE - 1);
str[MAX_SIZE - 1] = ’\0’;
}
} while (!valid);
}
// Function to display the details of a specific employee
void EmployeeList::displayEmployee(Employee *emp)
{
cout << "Employee ID: " << emp->employeeId << endl;
cout << "Employee Name: " << emp->employeeName << endl;
cout << "Father’s Name: " << emp->fatherName << endl;
cout << "Mother’s Name: " << emp->motherName << endl;
cout << "Position: " << emp->position << endl;
cout << "Department: " << emp->department << endl;
cout << "Date of Joining: " << emp->dateOfJoining << endl;
cout << "Address: " << emp->address << endl;
cout << "House No: " << emp->houseNo << endl;
cout << "Street Name: " << emp->streetName << endl;
cout << "City: " << emp->city << endl;
cout << "State: " << emp->state << endl;
cout << "Country: " << emp->country << endl;
cout << "Pin: " << emp->pin << endl;
cout << "----------------------------" << endl;
}
// Function to insert a new employee record
void EmployeeList::insertRecord()
{
int employeeId;
char employeeName[MAX_SIZE];
char fatherName[MAX_SIZE];
char motherName[MAX_SIZE];
char position[MAX_SIZE];
char department[MAX_SIZE];
char dateOfJoining[MAX_SIZE];
char address[MAX_SIZE];
char houseNo[MAX_SIZE];
char streetName[MAX_SIZE];
char city[MAX_SIZE];
char state[MAX_SIZE];
char country[MAX_SIZE];
char pin[MAX_SIZE];
cout << "Enter Employee ID: ";
cin >> employeeId;
cin.ignore();
if (isDuplicate(employeeId))
dsa3.cpp Fri Aug 08 10:25:52 2025 3
{
cout << "Duplicate record found. Cannot insert the record." << endl;
return;
}
getValidString(employeeName, "Enter Employee Name: ");
getValidString(fatherName, "Enter Father’s Name: ");
getValidString(motherName, "Enter Mother’s Name: ");
getValidString(position, "Enter Position: ");
getValidString(department, "Enter Department: ");
getValidString(dateOfJoining, "Enter Date of Joining: ");
getValidString(address, "Enter Address: ");
getValidString(houseNo, "Enter House No: ");
getValidString(streetName, "Enter Street Name: ");
getValidString(city, "Enter City: ");
getValidString(state, "Enter State: ");
getValidString(country, "Enter Country: ");
getValidString(pin, "Enter Pin: ");
Employee *newEmployee = new (nothrow) Employee;
if (newEmployee == nullptr)
{
cout << "Memory allocation failed. Cannot insert the record." << endl;
return;
}
newEmployee->employeeId = employeeId;
strncpy(newEmployee->employeeName, employeeName, MAX_SIZE - 1);
strncpy(newEmployee->fatherName, fatherName, MAX_SIZE - 1);
strncpy(newEmployee->motherName, motherName, MAX_SIZE - 1);
strncpy(newEmployee->position, position, MAX_SIZE - 1);
strncpy(newEmployee->department, department, MAX_SIZE - 1);
strncpy(newEmployee->dateOfJoining, dateOfJoining, MAX_SIZE - 1);
strncpy(newEmployee->address, address, MAX_SIZE - 1);
strncpy(newEmployee->houseNo, houseNo, MAX_SIZE - 1);
strncpy(newEmployee->streetName, streetName, MAX_SIZE - 1);
strncpy(newEmployee->city, city, MAX_SIZE - 1);
strncpy(newEmployee->state, state, MAX_SIZE - 1);
strncpy(newEmployee->country, country, MAX_SIZE - 1);
strncpy(newEmployee->pin, pin, MAX_SIZE - 1);
newEmployee->next = head;
head = newEmployee;
cout << "Record inserted successfully." << endl;
}
// Function to delete an employee record
void EmployeeList::_deleteRecord()
{
int employeeId;
cout << "Enter Employee ID to delete: ";
cin >> employeeId;
cin.ignore();
if (head == nullptr)
{
cout << "List is empty. Cannot delete the record." << endl;
return;
}
Employee *temp = head;
Employee *prev = nullptr;
if (temp != nullptr && temp->employeeId == employeeId)
{
head = temp->next;
delete temp;
cout << "Record deleted successfully." << endl;
return;
dsa3.cpp Fri Aug 08 10:25:52 2025 4
}
while (temp != nullptr && temp->employeeId != employeeId)
{
prev = temp;
temp = temp->next;
}
if (temp == nullptr)
{
cout << "Record not found." << endl;
return;
}
prev->next = temp->next;
delete temp;
cout << "Record deleted successfully." << endl;
}
// Function to find and display an employee record by ID
void EmployeeList::findRecord()
{
int employeeId;
cout << "Enter Employee ID to find: ";
cin >> employeeId;
cin.ignore();
Employee *temp = head;
while (temp != nullptr)
{
if (temp->employeeId == employeeId)
{
displayEmployee(temp);
return;
}
temp = temp->next;
}
cout << "Record not found." << endl;
}
// Function to update an existing employee record
void EmployeeList::updateRecord()
{
int employeeId;
cout << "Enter Employee ID to update: ";
cin >> employeeId;
cin.ignore(); // To ignore the newline character left by cin
Employee *temp = head;
while (temp != nullptr)
{
if (temp->employeeId == employeeId)
{
// Update record details
getValidString(temp->employeeName, "Enter new Employee Name: ");
getValidString(temp->fatherName, "Enter new Father’s Name: ");
getValidString(temp->motherName, "Enter new Mother’s Name: ");
getValidString(temp->position, "Enter new Position: ");
getValidString(temp->department, "Enter new Department: ");
getValidString(temp->dateOfJoining, "Enter new Date of Joining: ");
getValidString(temp->address, "Enter new Address: ");
getValidString(temp->houseNo, "Enter new House No: ");
getValidString(temp->streetName, "Enter new Street Name: ");
getValidString(temp->city, "Enter new City: ");
getValidString(temp->state, "Enter new State: ");
getValidString(temp->country, "Enter new Country: ");
getValidString(temp->pin, "Enter new Pin: ");
cout << "Record updated successfully." << endl;
dsa3.cpp Fri Aug 08 10:25:52 2025 5
return;
}
temp = temp->next;
}
cout << "Record not found." << endl;
}
// Function to display employee records based on location criteria
void EmployeeList::displayRecordsByLocation()
{
char city[MAX_SIZE];
char state[MAX_SIZE];
char country[MAX_SIZE];
getValidString(city, "Enter city to filter: ");
getValidString(state, "Enter state to filter: ");
getValidString(country, "Enter country to filter: ");
Employee *temp = head;
while (temp != nullptr)
{
if (strcmp(temp->city, city) == 0 && strcmp(temp->state, state) == 0 && strcmp(temp->
country, country) == 0)
{
displayEmployee(temp);
}
temp = temp->next;
}
}
// Function to generate and display a city-wise employee report
void EmployeeList::generateCityWiseReport()
{
// Implementation depends on the specific requirements for the report
cout << "City-wise report functionality is not yet implemented." << endl;
}
// Function to display the menu and handle user choices
void EmployeeList::showMenu()
{
int choice;
do
{
cout << "Menu:" << endl;
cout << "1. Insert Record" << endl;
cout << "2. Delete Record" << endl;
cout << "3. Find Record" << endl;
cout << "4. Update Record" << endl;
cout << "5. Display Records by Location" << endl;
cout << "6. Generate City-Wise Report" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch (choice)
{
case 1:
insertRecord();
break;
case 2:
deleteRecord();
break;
case 3:
findRecord();
break;
case 4:
updateRecord();
break;
dsa3.cpp Fri Aug 08 10:25:52 2025 6
case 5:
displayRecordsByLocation();
break;
case 6:
generateCityWiseReport();
break;
case 7:
cout << "Exiting the program" << endl;
break;
default:
cout << "Invalid choice. Please enter a number between 1 and 7." << endl;
}
} while (choice != 7);
}
// Main function
int main()
{
EmployeeList empList;
empList.showMenu(); // Show menu and handle user input
return 0;
}