Question
C++ PLEASE! Create an Employee struct with an int number, int salary, and a next - Create a class of Employees that are in a
C++ PLEASE!
Create an Employee struct with an int number, int salary, and a next - Create a class of Employees that are in a LinkedList - Make addAtBeginning(), addAtEnd(), deleteFromBeginning(), deleteFromEnd(), and print() methods -
Write a new function that adds another Employee to the list based on their salary (for example: if Employee X has a greater salary then Employee 2 but less than Employee 3, then X should go between them) (edited)
I have completed the first part of the problem, just need help creating a function that adds a new employee node based on salary.
HERES WHAT I HAVE SO FAR.
#include
#include
using namespace std;
struct node {
int number;
int salary;
node *next;
};
class Employee {
private:
node *head;
public:
Employee () {
head = NULL;
}
void addAtBeginning(int number, int salary) {
//allocate node
node* new_node = new node;
//put in data
new_node->number = number;
new_node->salary = salary;
//Make next of new node as head
new_node->next = head;
//move the head tp point to the new node
head = new_node;
};
void addAtEnd(int number,int salary){
node* new_node = new node;
//keep track of last node
node* last_node = head;
//data for new node
new_node->number = number;
new_node->salary = salary;
//new node point to null since it will be the last in list
new_node->next = NULL;
//check if the list is empty
//then make the new node as head
if(head == NULL){
head = new_node;
return;
}
//traverse the list till last node
while(last_node->next != NULL){
last_node = last_node->next;
}
//change next of last node to point to new node
last_node->next = new_node;
return;
};
void deleteFromBeginning() {
if( head != NULL) {
node* temp = head;
head = head->next;
free(temp);
}
}
** NEED HELP WITH THIS FUNCTION PLEASE ****
void addAtSalary(int number,int salary) {
};
void deleteFromEnd() {
if (head == NULL)
return;
if (head->next == NULL) {
delete head;
return;
}
// Find the second last node
node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
// Delete last node
delete (second_last->next);
// Change next of second last
second_last->next = NULL;
return;
};
void print() {
node *current =head;
while(current!=nullptr) {
cout
cout number
cout salary
cout
current = current->next;
}
};
};
int main() {
Employee list;
list.addAtBeginning(123,50000);
list.addAtEnd(456,20000);
list.addAtBeginning(789,30000);
list.print();
return 0;
}
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