In: Computer Science
// CSE240 Fall 2020 HW 7 & 8
// Write your name here
// Write the compiler used: Visual studio or gcc
// READ BEFORE YOU START:
// You are given a partially completed program that creates a
linked list of employee information.
// The global linked list 'list' is a list of employees with each
node being struct 'employeeList'.
// 'employeeList' consists of struct 'employee' which has: employee
name, room number, and a linked list of 'supervisors'.
// The linked list of supervisors has each node containing simply
the name of the supervisor.
// HW7 ignores the 'supervisors' linked list since there is no
operation/manipulation to be done on 'supervisors' list in
HW7.
// HW8 has operations/manipulations to be done with 'supervisors'
list like add a supervisor, display last added supervisor.
// To begin, you should trace through the given code and
understand how it works.
// Please read the instructions above each required function and
follow the directions carefully.
// If you modify any of the given code, the return types, or the
parameters, you risk getting compile error.
// You are not allowed to modify main ().
// You can use string library functions.
// ***** WRITE COMMENTS FOR IMPORANT STEPS OF YOUR CODE.
*****
// ***** GIVE MEANINGFUL NAMES TO VARIABLES. *****
// ***** Before implementing any function, see how it is called in
executeAction() *****
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(disable: 4996) // for Visual Studio
#define MAX_NAME 30
// global linked list 'list' contains the list of
employees
struct employeeList {
struct employee* employee;
struct employeeList* next;
} *list = NULL;
// currently empty list
// structure "employee" contains the employee's name, room
number and linked list of supervisors
struct employee {
char name[MAX_NAME];
unsigned int roomNumber;
struct supervisor* supervisors;
// linked list 'supervisors' contains names of
supervisors
};
// structure 'supervisor' contains supervisor's name
struct supervisor {
char name[MAX_NAME];
struct supervisor* next;
};
// forward declaration of functions (already implmented)
void flushStdIn();
void executeAction(char);
// functions that need implementation:
// HW 7
void addEmployee(char* employeeNameInput, unsigned int
roomNumInput); // 20 points
void displayEmployeeList(struct employeeList*
tempList); // 15 points
struct employee* searchEmployee(char*
employeeNameInput); // 15 points
//HW 8
void addSupervisor(char* employeeNameInput, char*
supervisorNameInput); // 15 points
void displayEmployeeSupervisorList(struct employeeList*
tempList); // 15 points
void removeEmployee(char* employeeNameInput);
// 20 points
int main()
{
char selection = 'a';
// initialized to a dummy value
do
{
printf("\nCSE240 HW 7,8\n");
printf("Please enter your
selection:\n");
printf("HW7:\n");
printf("\t a: add a new employee to
the list\n");
printf("\t d: display employee list
(no supervisors)\n");
printf("\t b: search for an
employee on the list\n");
printf("\t q: quit\n");
printf("HW8:\n");
printf("\t c: add a supervisor of a
employee\n");
printf("\t l: display employees who
report to a specific supervisor\n");
printf("\t r: remove an
employee\n");
printf("\t q: quit\n");
selection = getchar();
flushStdIn();
executeAction(selection);
} while (selection != 'q');
return 0;
}
// flush out leftover '\n' characters
void flushStdIn()
{
char c;
do c = getchar();
while (c != '\n' && c != EOF);
}
// Ask for details from user for the given selection and perform
that action
// Read the function case by case
void executeAction(char c)
{
char employeeNameInput[MAX_NAME],
supervisorNameInput[MAX_NAME];
unsigned int roomNumInput;
struct employee* searchResult = NULL;
switch (c)
{
case 'a': // add employee
// input employee details from user
printf("\nPlease enter employee's
name: ");
fgets(employeeNameInput,
sizeof(employeeNameInput), stdin);
employeeNameInput[strlen(employeeNameInput) - 1] =
'\0'; // discard the trailing '\n' char
printf("Please enter room number:
");
scanf("%d",
&roomNumInput);
flushStdIn();
if
(searchEmployee(employeeNameInput) == NULL) //
un-comment this line after implementing
searchEmployee()
//if (1)
// comment out this line
after implementing searchEmployee()
{
addEmployee(employeeNameInput, roomNumInput);
printf("\nEmployee successfully added to the list!\n");
}
else
printf("\nThat
employee is already on the list!\n");
break;
case 'd': // display
the list
displayEmployeeList(list);
break;
case 'b': // search
for an employee on the list
printf("\nPlease enter employee's
name: ");
fgets(employeeNameInput,
sizeof(employeeNameInput), stdin);
employeeNameInput[strlen(employeeNameInput) - 1] =
'\0'; // discard the trailing '\n' char
if
(searchEmployee(employeeNameInput) == NULL) //
un-comment this line after implementing
searchEmployee()
//if (0)
// comment out this line
after implementing searchEmployee()
printf("\nEmployee name does not exist or the list is empty!
\n\n");
else
{
printf("\nEmployee name exists on the list! \n\n");
}
break;
case 'r': // remove
employee
printf("\nPlease enter employee's
name: ");
fgets(employeeNameInput,
sizeof(employeeNameInput), stdin);
employeeNameInput[strlen(employeeNameInput) - 1] =
'\0'; // discard the trailing '\n' char
if
(searchEmployee(employeeNameInput) == NULL) //
un-comment this line after implementing
searchEmployee()
//if (0)
// comment out this line
after implementing searchEmployee()
printf("\nEmployee name does not exist or the list is empty!
\n\n");
else
{
removeEmployee(employeeNameInput);
printf("\nEmployee successfully removed from the list!
\n\n");
}
break;
case 'c': // add
supervisor
printf("\nPlease enter employee's
name: ");
fgets(employeeNameInput,
sizeof(employeeNameInput), stdin);
employeeNameInput[strlen(employeeNameInput) - 1] =
'\0'; // discard the trailing '\n' char
if
(searchEmployee(employeeNameInput) == NULL) //
un-comment this line after implementing
searchEmployee()
//if (0)
// comment
out this line after implementing searchEmployee()
printf("\nEmployee name does not exist or the list is empty!
\n\n");
else
{
printf("\nPlease
enter supervisor's name: ");
fgets(supervisorNameInput, sizeof(supervisorNameInput),
stdin);
supervisorNameInput[strlen(supervisorNameInput) - 1] =
'\0'; // discard the trailing '\n' char
addSupervisor(employeeNameInput, supervisorNameInput);
printf("\nSupervisor added! \n\n");
}
break;
case 'l': // list
supervisor's employees
displayEmployeeSupervisorList(list);
break;
case 'q': //
quit
break;
default: printf("%c is invalid input!\n", c);
}
}
// HW7 Q1: addEmployee (20 points)
// This function is used to insert a new employee in the linked
list.
// You must insert the new employee to the head of linked list
'list'.
// You need NOT check if the employee already exists in the list
because that is taken care by searchEmployee() called in
executeAction(). Look at how this function is used in
executeAction().
// Don't bother to check how to implement searchEmployee() while
implementing this function. Simply assume that employee does not
exist in the list while implementing this function.
// NOTE: The function needs to add the employee to the head of the
list.
// NOTE: This function does not add supervisors to the employee
info. There is another function addSupervisor() in HW8 for
that.
// Hint: In this question, no supervisors means NULL
supervisors.
void addEmployee(char* employeeNameInput, unsigned int
roomNumInput)
{
// YOUR CODE HERE
}
// HW7 Q2: displayEmployeeList (15 points)
// This function displays the employee details (struct elements) of
each employee.
// Parse through the linked list 'list' and print the employee
details ( name and room number) one after the other. See expected
output screenshots in homework question file.
// You should not display supervisor names (because they are not
added in HW7).
// You MUST use recursion in the function to get full points.
Notice that 'list' is passed to the function argument. Use
recursion to keep calling this function till end of list.
void displayEmployeeList(struct employeeList* tempList)
{
// YOUR CODE HERE
}
// HW7 Q3: searchEmployee (15 points)
// This function searches the 'list' to check if the given employee
exists in the list. Search by employee name.
// If it exists then return that 'employee' node of the list.
Notice the return type of this function.
// If the employee does not exist in the list, then return
NULL.
// NOTE: After implementing this fucntion, go to executeAction() to
comment and un-comment the lines mentioned there which use
searchEmployee()
// in case 'a', case 'r', case 'l' (total 3 places)
struct employee* searchEmployee(char* employeeNameInput)
{
struct employeeList* tempList = list;
// work on a copy of
'list'
// YOUR CODE HERE
return NULL;
}
Help me complete the code thank you.
// I am attaching the code, didn't have time to compile. I'm sure you can follow along and make changes if required
// Do put the effort in understanding the comments.
void addEmployee(char *employeeNameInput, unsigned int roomNumInput)
{
// YOUR CODE HERE
// create new employee
employee* new_employee = malloc(sizeof(employee)); // allot new memory
strcpy(new_employee->name, employeeNameInput); // copy
new_employee->roomNumber = roomNumInput; // copy
// add to the 'list'
employeeList* new_node = malloc(sizeof(employeeList)); // allot new memory for node in the list
new_node->employee = new_employee; // copy pointer
if(list != NULL) // if list is not empty
new_node->next = list->next; // adding before the head
else
new_node->next = NULL; // first node in the list (so next node is null)
list = new_node; // create the new head
}
----------------------------------------------------------------------------------------------------------------------------------------------
void displayEmployeeList(struct employeeList *tempList)
{
// YOUR CODE HERE
if(tempList != NULL) // till list doesn't end
{
printf("%s\n",*(tempList->employee->name)); // display name
printf(("%d\n",*(tempList->employee->roomNumber))); // display room number
displayEmployeeList(tempList->next); // resursively display next node
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
struct employee *searchEmployee(char *employeeNameInput)
{
struct employeeList *tempList = list; // work on a copy of 'list'
// YOUR CODE HERE
while(tempList != NULL);
{
if(strcmp (tempList->employee->name, employeeNameInput) == 0 ) // if employee is found (compare the two strings)
{
return tempList->employee; // return employee pointer from the node
}
tempList=tempList->next; // go to next node if not found
}
return NULL;
}
--------------------------------------------------------------------------------------------------------------------------------------------