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...
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...
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...
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
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...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that...
*****IN JAVA***** A run is a sequence of adjacent repeated values. Write a code snippet that generates a sequence of 20 random die tosses in an array and that prints the die values, marking the runs by including them in parentheses, like this: 1 2 (5 5) 3 1 2 4 3 (2 2 2 2) 3 6 (5 5) 6 (3 3) Use the following pseudocode: inRun = false for each valid index i in the array If inRun...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Consider this code snippet: if (x < 512) { y = 73; } else { y...
Consider this code snippet: if (x < 512) { y = 73; } else { y = 42; } Which of these goto-style code snippets is equivalent to that if/else code? Consider this code snippet: if (x < 512) { y = 73; } else { y = 42; } Which of these goto-style code snippets is equivalent to that if/else code? a. if (x >= 512) goto Label1; y = 73; goto Label2; Label1: y = 42; Label2: b....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT