Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions

Question

In Exercises find the indefinite integral. csch(1/x) coth(1/x) dx x

Answered: 1 week ago