Hash Tables and functions

amadiya
LinkedListassignment5.pdf

// ASU CSE310 Assignment #5 Spring 2023 // Name of Author: // ASU ID: // Description: A simple linked list that implements a list of Employee objects. A user can // perform searching, insertion or deletion on the linked list. // //---- is where you should add your own code #include <iostream> #include <iomanip> #include <string> using namespace std; struct Employee { string firstName, lastName; int id; double salary; struct Employee* next; }; class LinkedList { private: struct Employee* head; int size; //a variable represents number of Employees inside the list public: LinkedList(); ~LinkedList(); Employee* getHead(); int getSize(); bool searchEmployee(int id); bool insertEmployee(string firstName, string lastName, int id, double salary); bool deleteEmployee(int id); void displayList(); };

//Constructor LinkedList::LinkedList() { head = NULL; //initialising it to null size = 0; } //Destructor LinkedList::~LinkedList() { Employee* current = head; while (current != nullptr) { Employee* next = current->next; delete current; current = next; } } Employee* LinkedList::getHead() { return head; } //Return number of Employees inside the Linked list int LinkedList::getSize() { Employee* temp = head;

int size = 0; while(temp != nullptr) { size ++; temp = temp->next; } return size; } //This function does a linear search on the Employee list with the given Employee id //it returns true if the corresponding Employee is found, otherwise it returns false. bool LinkedList::searchEmployee(int id) { Employee* curr = head; while(curr!= NULL) { if(curr->id == id) { return true; } else { curr = curr->next; } } return false;

} //Insert the parameter Employee at the head of the linked list. //return true if it is inserted successfully and false otherwise bool LinkedList::insertEmployee(string firstName, string lastName, int id, double salary) { Employee* newEmp = new Employee; //creating new Employee newEmp -> firstName = firstName; newEmp -> lastName = lastName; newEmp -> id = id; newEmp -> salary = salary; newEmp -> next = nullptr; if(head == nullptr) { head = newEmp; } else { newEmp -> next = head; head = newEmp; } size ++; return true; } //Delete the Employee with the given id from the linked list. //Return true if it is deleted successfully and false otherwise bool LinkedList::deleteEmployee(int id)

{ if(head == NULL) { return false; } if(head->id == id)//to delete the head where the head is the employee to be deleted { Employee* temp = head; head = head->next; delete temp; size--; return true; } Employee* curr = head; while((curr->next != nullptr) && (curr->next->id != id)) { curr = curr->next; } if(curr->next == NULL) { return false; } Employee* temp = curr->next; curr->next = temp->next;

delete temp; size--; return true; } //This function displays the content of the linked list. void LinkedList::displayList() { struct Employee *temp = head; if(head == NULL) { //empty linked list, print nothing here } else { while(temp != NULL) { cout << left << setw(18) << temp->firstName << left << setw(18) << temp->lastName << right << setw(8) << temp->id << setw(10) << fixed << setprecision(2) << temp->salary << "\n"; temp = temp->next; } } }