In: Computer Science
Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”.
Once you complete the snippet below, your output should have the same result with the given output below.
Descriptions:
This function is for checking that there is no duplicated employee data in the linked list. This function will check the name, jobPos, grade and age. It will return to 1 if no duplicated data found at those properties and return to 0 vice versa.
This function is built for inserting a new data in the linked list. You are required to check whether the inputted is novel data. If the inputted data is not valid, the program will print “ The inputted data is duplicated!”.
Note : please analyze how to push a data by given sequence of inserting in the main function and please compare it to the given output!
this function is built for deleting a data. If you are trying to delete a data when the list is empty, then the program will prompt you “the list is empty!”.
Note : please analyze how to pop a data by given sequence of deleting in the main function and please compare it to the given output!
this function is built for printing all the data in the list along with the total employee number. If you are trying to delete a data when the list is empty, then the program will prompt you “the list is empty!”.
// // Created by Hanry Ham on 2020-05-24. // #include<stdio.h> #include<string.h> #include<stdlib.h> struct Employee{ char name[20]; char jobPos[15]; int grade; int age; Employee *next; }*head = NULL, *tail = NULL; bool isValid(char *name, char *jobPos, int grade, int age){ // [15%] (1) INSERT YOUR CODE HERE } void push(char *name, char *jobPos, int grade, int age){ struct Employee *curr = (struct Employee *) malloc(sizeof(Employee)); // [10%] (2) INSERT YOUR CODE HERE if( ){ }else{ } } void pop(){ struct Employee * curr = head; // [15%] (3) INSERT YOUR CODE HERE } void printAll(){ printf("\n\n"); struct Employee * curr = head; int empCtr = 0; if(!curr){ printf("the list is empty!"); }else{ // [10%] (4) INSERT YOUR CODE HERE } int main(){ pop(); printAll(); push("Hanry", "Supervisor", 12, 27); push("Yen", "Manager", 13, 40); pop(); push("Derwin", "Manager", 15, 31); push("Andry", "Manager", 15, 30); pop(); push("Saka", "Manager", 15, 32); pop(); push("Afan", "Manager", 16, 35); push("Fredy", "Senior Manager", 18, 45); pop(); printAll(); return 0; } |
Output:
the list is empty the list is empty! ======================================================= | Name| Job Position| Grade| Age| ======================================================= | Fredy| Senior Manager| 18| 45| | Afan| Manager| 16| 35| | Saka| Manager| 15| 32| ======================================================= Total Employee : 3 ======================================================= |
NOTE: After reading the given code, I assumed that insertion occurs at the head and deletion occurs at the tail.
1. The required source code is given below:-
//-------------------------------------------------------------------------------------------
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct Employee{
char name[20];
char jobPos[15];
int grade;
int age;
Employee *next;
}*head = NULL, *tail = NULL;
bool isValid(char *name, char *jobPos, int grade, int age)
{
// [15%] (1) INSERT YOUR CODE HERE
struct Employee *emp=head;
while(emp!=NULL)
{
if((strcmp(name,emp->name)==0)&&(strcmp(jobPos,emp->jobPos)==0)&&(grade==emp->grade)&&(age==emp->age))
{
return 0;
}
emp = emp->next;
}
return 1;
}
void push(char *name, char *jobPos, int grade, int age)
{
struct Employee *curr = (struct Employee *)
malloc(sizeof(Employee));
// [10%] (2) INSERT YOUR CODE HERE
if(isValid(name,jobPos,grade,age))
{
strcpy(curr->name,name);
strcpy(curr->jobPos,jobPos);
curr->grade = grade;
curr->age = age;
if(head==NULL || tail==NULL)
{
curr->next =
NULL;
head=tail=curr;
}
else
{
curr->next =
head;
head =
curr;
}
}
else
{
free(curr);
printf("The inputted data is duplicated!\n");
}
}
void pop()
{
struct Employee * curr = tail;
// [15%] (3) INSERT YOUR CODE HERE
if(head==NULL || tail==NULL)
{
printf("the list is
empty!\n");
}
else
{
if(head==tail)
{
head=tail=NULL;
}
else
{
struct Employee
* tmp = head;
while(tmp->next!=tail)
{
tmp=tmp->next;
}
tail =
tmp;
tail->next=NULL;
}
free(curr);
}
}
void printAll()
{
printf("\n\n");
struct Employee * curr = head;
int empCtr = 0;
if(!curr)
{
printf("the list is empty!");
}
else
{
// [10%] (4) INSERT YOUR CODE HERE
printf("=======================================================\n");
printf("| Name| Job Position|
Grade| Age|\n");
printf("=======================================================\n");
while(curr!=NULL)
{
printf("| %s|
%s| %d|
%d|\n",curr->name,curr->jobPos,curr->grade,curr->age);
empCtr++;
curr =
curr->next;
}
printf("=======================================================\n");
printf("Total Employee :
%d\n",empCtr);
printf("=======================================================\n");
}
}
int main(){
pop();
printAll();
push("Hanry", "Supervisor", 12, 27);
push("Yen", "Manager", 13, 40);
pop();
push("Derwin", "Manager", 15, 31);
push("Andry", "Manager", 15, 30);
pop();
push("Saka", "Manager", 15, 32);
pop();
push("Afan", "Manager", 16, 35);
push("Fredy", "Senior Manager", 18, 45);
pop();
printAll();
return 0;
}
//-------------------------------------------------------------------------------------------
2. This is a screenshot of the output:-