Question

In: Computer Science

[50%] Code Snippet Given snippet code below that you are required to complete. You are not...

  1. [50%] Code Snippet

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:

  1. [15%] isValid()

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.

  1. [10%] push()

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!

  1. [15%] pop()

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!

  1. [10%] printAll()

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

=======================================================

Solutions

Expert Solution

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:-


Related Solutions

Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
Write a code snippet for the following:   You need to control the maximum number of people...
Write a code snippet for the following:   You need to control the maximum number of people who can be in a   restaurant at any given time. A group cannot enter the restaurant if they   would make the number of people exceed 100 occupants. Use random numbers   between 1 and 20 to simulate groups arriving to the restaurant. After each   random number, display the size of the group trying to enter and the number   of current occupants. As soon as the...
Complete the below code so that your program generates a random walk on the given graph....
Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random. /******************************************************************/ #include <stdio.h> #include <stdlib.h> #include <time.h> typedef struct NODE_s *NODE; typedef struct NODE_s{ char name; NODE link[10]; }NODE_t[1]; #define nodeA 0 #define nodeB 1 #define nodeC 2 #define nodeD 3 #define nodeE 4 #define nodeF 5 int main() { srandom(time(NULL)); //printf("%d\n", random()); NODE_t node[6]; node[nodeA]->name = 'A'; node[nodeB]->name = 'B'; node[nodeC]->name = 'C'; node[nodeD]->name...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete...
Complete this question by entering your answers in the tabs below. Required 1 Required 2 Complete the above schedule of the company’s total costs and costs per unit. (Round the per unit variable cost and fixed cost to 2 decimal places.) Units Produced and Sold 69,000 89,000 109,000 Total costs: Variable costs $213,900 Fixed costs 390,000 Total costs $603,900 $0 $0 Cost per unit: Variable cost Fixed cost Total cost per unit $0.00 $0.00 $0.00
I'm having trouble understanding the following code (a snippet of a code). What does it do?...
I'm having trouble understanding the following code (a snippet of a code). What does it do? The whole code is about comparing efficiencies of different algorithms. def partition(list,first,last): piv = list[first] lmark = first+1 rmark = last done = False while not done: while lmark <= rmark and list[lmark]<=piv: lmark=lmark+1 while list[rmark]>=piv and rmark>=lmark: rmark=rmark-1 if rmark<lmark: done = True else: temp = list[lmark] list[lmark]=list[rmark] list[rmark]=temp temp = list[first] list[first]=list[rmark] list[rmark]=temp return rmark
What is the output of the following code snippet? (If there is some kind of syntax...
What is the output of the following code snippet? (If there is some kind of syntax error indicate this by marking "error" in your answer choice) 1.        int laps = 8;        if (laps++ > --laps)               laps += 2;        else if (--laps > (laps - 1))               laps += 4;        else if (++laps)               laps -= 3;        cout << laps << endl; 2. What is the output of the following code snippet?     int j = 47;     int i = 8;     j =...
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that....
Please complete the following code in challenge.c. The code for main.c and challenge.h is below that. (Don't edit main.c or challenge.h, only edit challenge.c) The instructions are in the comments. Hint: the_person is declared in main, so you need to define it in challenge.c using extern challenge.c #include "challenge.h" //return: struct //param: (struct person p1, struct person p2) //TODO: create a function that returns the person who has a higher GPA. // 1. if GPAs are equal, then return the...
The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
Please code by C++ The required week2.cpp file code is below Please ask if you have...
Please code by C++ The required week2.cpp file code is below Please ask if you have any questions instruction: 1. Implement the assignment operator: operator=(Vehicle &) This should make the numWheels and numDoors equal to those of the object that are being passed in. It is similar to a copy constructor, only now you are not initializing memory. Don’t forget that the return type of the function should be Vehicle& so that you can write something like: veh1 = veh2...
Please carefully review the code to fill in after the given instructions and complete the code...
Please carefully review the code to fill in after the given instructions and complete the code for me. Thank you in advance. In this problem, we will implement a simple dictionary of common words in the English language, represented as an array of words paired with their lengths. You will need to implement each of the below methods in the Dictionary class. In this problem, the first line of input represents the method to call. It will be one of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT